EditProfile.js 6.3 KB

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