admin.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. Page({
  2. data: {
  3. isLogin: false, // 登录状态
  4. isHidden: true, // 弹窗状态
  5. avatarUrl: '', // 用户头像
  6. nickName: '', // 用户昵称
  7. username: '', // 用户名(用户名密码登录)
  8. password: '', // 密码(用户名密码登录)
  9. errorMessage: '', // 错误信息
  10. },
  11. // 获取用户头像
  12. getAvatar(e) {
  13. const avatarUrl = e.detail.avatarUrl; // 从事件中获取头像URL
  14. if (avatarUrl) {
  15. this.setData({
  16. avatarUrl: avatarUrl
  17. });
  18. // 授权登录后,自动设置默认密码到缓存
  19. wx.setStorageSync('password', '123');
  20. // 创建userInfo对象,保存头像和昵称到缓存
  21. let userInfo = {
  22. avatarUrl: avatarUrl,
  23. nickName: this.data.nickName
  24. };
  25. wx.setStorageSync('userinfo', userInfo); // 保存到缓存
  26. } else {
  27. wx.showToast({
  28. title: '头像获取失败',
  29. icon: 'error',
  30. duration: 2000
  31. });
  32. }
  33. },
  34. // 获取用户昵称
  35. getName(e) {
  36. const nickName = e.detail.value; // 获取昵称
  37. this.setData({
  38. nickName: nickName
  39. });
  40. // 在授权登录时,将昵称更新到缓存的 userInfo 对象中
  41. let userInfo = wx.getStorageSync('userinfo') || {};
  42. userInfo.nickName = nickName;
  43. wx.setStorageSync('userinfo', userInfo); // 更新缓存
  44. },
  45. // 显示登录弹窗
  46. gologin() {
  47. this.setData({
  48. isHidden: false
  49. });
  50. },
  51. // 取消登录弹窗
  52. potNo() {
  53. this.setData({
  54. isHidden: true
  55. });
  56. },
  57. // 确认登录弹窗
  58. popYes() {
  59. const { avatarUrl, nickName } = this.data;
  60. if (!avatarUrl || !nickName) {
  61. wx.showToast({
  62. icon: 'error',
  63. title: '请获取头像和昵称',
  64. });
  65. return;
  66. }
  67. this.setData({
  68. isLogin: true,
  69. isHidden: true,
  70. });
  71. // 登录成功后跳转到指定页面
  72. wx.switchTab({
  73. url: '/pages/Regular User/Home/Home',
  74. });
  75. },
  76. // 用户名密码登录
  77. inputUsername(e) {
  78. this.setData({
  79. username: e.detail.value
  80. });
  81. },
  82. inputPassword(e) {
  83. this.setData({
  84. password: e.detail.value
  85. });
  86. },
  87. // 登录验证
  88. login() {
  89. const { username, password } = this.data;
  90. if (!username || !password) {
  91. this.setData({
  92. errorMessage: '用户名和密码不能为空'
  93. });
  94. return;
  95. }
  96. wx.showLoading({
  97. title: '登录中...'
  98. });
  99. setTimeout(() => {
  100. wx.hideLoading();
  101. // 假设用户名和密码为123时登录成功
  102. if (username === '123' && password === '123') {
  103. wx.showToast({
  104. title: '登录成功',
  105. icon: 'success',
  106. duration: 2000
  107. });
  108. this.setData({
  109. isLogin: true,
  110. errorMessage: '',
  111. });
  112. // 登录成功后跳转到指定页面
  113. wx.switchTab({
  114. url: '/pages/Regular User/Home/Home',
  115. });
  116. } else {
  117. this.setData({
  118. errorMessage: '用户名或密码错误'
  119. });
  120. wx.showToast({
  121. title: '用户名或密码错误',
  122. icon: 'none',
  123. duration: 2000
  124. });
  125. }
  126. }, 1500);
  127. },
  128. // 页面加载时,检查是否有缓存的密码
  129. onLoad() {
  130. const storedPassword = wx.getStorageSync('password');
  131. const storedUserInfo = wx.getStorageSync('userinfo');
  132. // 如果有缓存的密码,则自动填充
  133. if (storedPassword) {
  134. this.setData({
  135. password: storedPassword // 自动填充缓存的密码
  136. });
  137. }
  138. // 如果有缓存的用户信息,自动填充头像和昵称
  139. if (storedUserInfo) {
  140. this.setData({
  141. avatarUrl: storedUserInfo.avatarUrl || '/assets/taddar/授权管理.png', // 默认头像
  142. nickName: storedUserInfo.nickName || '未登录', // 默认昵称
  143. isLogin: true, // 设置已登录状态
  144. });
  145. }
  146. // 如果没有设置头像,使用默认头像
  147. if (!this.data.avatarUrl) {
  148. this.setData({
  149. avatarUrl: '/assets/taddar/授权管理.png' // 默认头像
  150. });
  151. }
  152. }
  153. });