EditProfile.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. Page({
  2. data: {
  3. userInfo: {
  4. name: '', // 用户名
  5. },
  6. oldPassword: '', // 输入的旧密码
  7. newPassword: '', // 输入的新密码
  8. errorMessage: '', // 错误提示信息
  9. isSaveDisabled: true, // 控制保存按钮是否禁用
  10. },
  11. // 页面加载时从缓存获取用户信息
  12. onLoad() {
  13. this.loadUserInfo(); // 加载缓存中的用户信息
  14. },
  15. // 页面显示时确保更新按钮状态
  16. onShow() {
  17. this.updateSaveButtonState(); // 更新保存按钮的状态
  18. },
  19. // 加载缓存的用户信息
  20. loadUserInfo() {
  21. // 从缓存中获取 currentUser(用户名)和 userId(用户ID)
  22. const currentUser = wx.getStorageSync('currentUser') || ''; // 获取缓存的用户名,若无则为空
  23. const userId = wx.getStorageSync('userId') || ''; // 获取缓存的用户ID,若无则为空
  24. // 设置用户名和用户ID
  25. this.setData({
  26. userInfo: {
  27. name: currentUser, // 设置用户名
  28. }
  29. });
  30. this.updateSaveButtonState(); // 更新保存按钮状态
  31. },
  32. // 获取用户名输入
  33. inputName(e) {
  34. this.setData({
  35. 'userInfo.name': e.detail.value, // 更新用户名
  36. });
  37. this.updateSaveButtonState(); // 更新保存按钮状态
  38. },
  39. // 获取旧密码输入
  40. inputOldPassword(e) {
  41. this.setData({
  42. oldPassword: e.detail.value, // 更新旧密码
  43. });
  44. this.updateSaveButtonState(); // 更新保存按钮状态
  45. },
  46. // 获取新密码输入
  47. inputNewPassword(e) {
  48. this.setData({
  49. newPassword: e.detail.value, // 更新新密码
  50. });
  51. this.updateSaveButtonState(); // 更新保存按钮状态
  52. },
  53. // 更新保存按钮的状态
  54. updateSaveButtonState() {
  55. const { oldPassword, newPassword } = this.data;
  56. // 判断如果旧密码和新密码都有值,则启用保存按钮,否则禁用
  57. if (oldPassword.trim() && newPassword.trim()) {
  58. this.setData({
  59. isSaveDisabled: false, // 启用保存按钮
  60. });
  61. } else {
  62. this.setData({
  63. isSaveDisabled: true, // 禁用保存按钮
  64. });
  65. }
  66. },
  67. // 保存修改后的用户名和密码
  68. saveChanges() {
  69. const { userInfo, oldPassword, newPassword } = this.data;
  70. // 校验用户名
  71. if (!userInfo.name.trim()) {
  72. this.setData({ errorMessage: '用户名不能为空' });
  73. return;
  74. }
  75. // 校验新密码是否为空
  76. if (newPassword && !newPassword.trim()) {
  77. this.setData({ errorMessage: '新密码不能为空' });
  78. return;
  79. }
  80. // 从缓存中获取 userId
  81. const userId = wx.getStorageSync('userId');
  82. if (!userId) {
  83. this.setData({ errorMessage: '用户ID不可用' });
  84. return;
  85. }
  86. // 调用后端API来保存用户名和密码
  87. this.updateUserInfo(userId, userInfo.name, oldPassword, newPassword);
  88. },
  89. // 调用后端API更新用户信息
  90. updateUserInfo(userId, name, oldPassword, newPassword) {
  91. wx.request({
  92. url: 'https://soilgd.com:5000/update_user', // 替换为实际的API地址
  93. method: 'POST',
  94. data: {
  95. userId: userId, // 传递 userId
  96. name: name,
  97. oldPassword: oldPassword,
  98. newPassword: newPassword,
  99. },
  100. header: {
  101. 'Content-Type': 'application/json' // 确保 Content-Type 设置为 application/json
  102. },
  103. success: (res) => {
  104. if (res.data.success) {
  105. wx.setStorageSync('currentUser', name); // 更新缓存中的用户名
  106. this.setData({ errorMessage: '' });
  107. // 显示成功提示
  108. wx.showToast({
  109. title: '保存成功',
  110. icon: 'success',
  111. });
  112. // 修改成功后跳转到 Staff 页面
  113. wx.switchTab({
  114. url: '/pages/Staff/Staff', // 跳转到 Staff 页面
  115. });
  116. } else {
  117. this.setData({ errorMessage: res.data.message || '保存失败' });
  118. }
  119. },
  120. fail: (err) => {
  121. console.error('数据库更新失败', err);
  122. this.setData({ errorMessage: '更新失败,请稍后重试' });
  123. }
  124. });
  125. }
  126. });