EditProfile.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. Page({
  2. data: {
  3. isLogin: false, // 登录状态
  4. userInfo: { // 用户信息
  5. nickName: '', // 用户昵称
  6. },
  7. },
  8. // 页面加载时从缓存获取用户信息
  9. onLoad() {
  10. this.loadUserInfo(); // 加载缓存中的用户信息
  11. },
  12. // 页面显示时确保用户信息刷新
  13. onShow() {
  14. this.loadUserInfo(); // 每次进入页面时刷新用户信息
  15. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  16. this.getTabBar().setData({
  17. selected: 3 // 设置当前tab为选中状态
  18. });
  19. }
  20. // 隐藏返回首页按钮
  21. if (wx.canIUse('hideHomeButton')) {
  22. wx.hideHomeButton();
  23. }
  24. },
  25. // 加载缓存的用户信息
  26. loadUserInfo() {
  27. // 获取缓存中的用户信息
  28. const storedUserInfo = wx.getStorageSync('userInfo') || {}; // 获取缓存的 userInfo 对象
  29. const storedAvatarUrl = storedUserInfo.avatarUrl || '/assets/taddar/me.png'; // 获取缓存的头像
  30. const storedPassword = storedUserInfo.password || ''; // 获取缓存的密码
  31. const storedUsername = storedUserInfo.username || ''; // 获取缓存的用户名
  32. const storedUserRole = wx.getStorageSync('userRole') || ''; // 获取缓存的用户角色
  33. // 合并缓存信息
  34. const userInfo = {
  35. nickName: storedUserInfo.nickName || '', // 如果没有昵称,设置为空
  36. avatarUrl: storedAvatarUrl, // 如果没有头像,使用默认头像
  37. username: storedUsername, // 设置用户名
  38. password: storedPassword, // 设置密码
  39. };
  40. // 设置页面数据
  41. this.setData({
  42. userInfo,
  43. userRole: storedUserRole, // 设置用户角色
  44. });
  45. // 调试信息,确认缓存是否加载成功
  46. console.log('Loaded userInfo from cache:', userInfo);
  47. console.log('Loaded avatarUrl from cache:', storedAvatarUrl);
  48. console.log('Loaded password from cache:', storedPassword);
  49. console.log('Loaded username from cache:', storedUsername);
  50. console.log('Loaded userRole from cache:', storedUserRole);
  51. },
  52. // 修改昵称
  53. onNicknameChange(e) {
  54. this.setData({
  55. 'userInfo.nickName': e.detail.value, // 更新昵称
  56. });
  57. },
  58. // 修改用户名
  59. onUsernameChange(e) {
  60. this.setData({
  61. 'userInfo.username': e.detail.value, // 更新用户名
  62. });
  63. },
  64. // 修改密码
  65. onPasswordChange(e) {
  66. this.setData({
  67. 'userInfo.password': e.detail.value, // 更新密码
  68. });
  69. },
  70. // 选择头像
  71. chooseAvatar() {
  72. wx.chooseMedia({
  73. count: 1, // 选择1个文件
  74. sizeType: ['original', 'compressed'], // 原图或压缩图
  75. sourceType: ['album', 'camera'], // 相册或拍照
  76. success: (res) => {
  77. const avatarUrl = res.tempFiles[0].tempFilePath;
  78. this.setData({
  79. 'userInfo.avatarUrl': avatarUrl, // 更新头像
  80. });
  81. // 更新缓存中的头像信息
  82. let userInfo = wx.getStorageSync('userInfo') || {};
  83. userInfo.avatarUrl = avatarUrl;
  84. wx.setStorageSync('userInfo', userInfo);
  85. },
  86. fail: () => {
  87. wx.showToast({
  88. title: '头像选择失败',
  89. icon: 'none',
  90. });
  91. },
  92. });
  93. },
  94. // 提交表单
  95. submitForm() {
  96. const { userInfo } = this.data;
  97. // 打印 userInfo 查看提交的数据
  98. console.log('Submitting userInfo:', userInfo);
  99. // 检查昵称和用户名是否同时为空
  100. if (!userInfo.nickName.trim() && !userInfo.username.trim()) {
  101. wx.showToast({
  102. title: '昵称和用户名至少填写一个',
  103. icon: 'none',
  104. });
  105. return;
  106. }
  107. // 检查昵称和用户名是否同时填写
  108. if (userInfo.nickName.trim() && userInfo.username.trim()) {
  109. wx.showToast({
  110. title: '只能填写一个:昵称或用户名',
  111. icon: 'none',
  112. });
  113. return;
  114. }
  115. // 检查密码是否为空
  116. if (!userInfo.password || !userInfo.password.trim()) {
  117. wx.showToast({
  118. title: '密码不能为空',
  119. icon: 'none',
  120. });
  121. return;
  122. }
  123. // 保存用户信息到缓存
  124. wx.setStorageSync('userInfo', userInfo); // 更新整个 userInfo 对象
  125. wx.setStorageSync('avatarUrl', userInfo.avatarUrl); // 更新头像
  126. wx.setStorageSync('username', userInfo.username); // 更新用户名
  127. wx.setStorageSync('password', userInfo.password); // 更新密码
  128. wx.setStorageSync('userRole', this.data.userRole); // 保存用户角色
  129. // 重新加载缓存并更新页面
  130. this.loadUserInfo();
  131. // 显示保存成功提示
  132. wx.showToast({
  133. title: '保存成功',
  134. icon: 'success',
  135. });
  136. // 返回上一页
  137. wx.navigateBack();
  138. },
  139. // 获取显示的用户名(优先显示昵称,如果没有则显示用户名)
  140. getDisplayName() {
  141. const { nickName, username } = this.data.userInfo;
  142. return nickName.trim() || username.trim() || '未设置'; // 如果昵称为空,则显示用户名,否则显示 '未设置'
  143. },
  144. // 显示登录弹窗
  145. goLogin() {
  146. this.setData({
  147. isHidden: false
  148. });
  149. },
  150. // 编辑个人资料
  151. EditProfile() {
  152. wx.navigateTo({
  153. url: '/shoping/EditProfile/EditProfile'
  154. });
  155. },
  156. // 取消登录弹窗
  157. potNo() {
  158. this.setData({
  159. isHidden: true
  160. });
  161. },
  162. // 确认登录弹窗
  163. popYes() {
  164. const { avatarUrl, nickName, username, password } = this.data.userInfo;
  165. if (!avatarUrl || !nickName || !username || !password) {
  166. wx.showToast({
  167. icon: 'error',
  168. title: '请填写头像、昵称、用户名和密码',
  169. });
  170. return;
  171. }
  172. // 保存头像、昵称、用户名和密码到缓存
  173. wx.setStorageSync('userInfo', this.data.userInfo);
  174. this.setData({
  175. isLogin: true, // 设置登录状态为 true
  176. isHidden: true, // 隐藏弹窗
  177. });
  178. },
  179. // 跳转到阈值页面
  180. goToThreshold() {
  181. wx.navigateTo({
  182. url: '/pages/threshold/threshold'
  183. });
  184. },
  185. // 点击退出登录
  186. tuichu() {
  187. // 清除缓存
  188. wx.clearStorageSync();
  189. // 重置登录状态
  190. this.setData({
  191. isLogin: false,
  192. userInfo: {}
  193. });
  194. // 跳转到登录页面
  195. wx.reLaunch({
  196. url: '/pages/admin/admin' // 登录页面的路径
  197. });
  198. }
  199. });