my.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // pages/my/my.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. buttonText: '发送验证码',
  6. phone: "",
  7. code: "",
  8. avatar: "/images/头像.png",
  9. userInfo: null,
  10. countDown: 60,
  11. timer: null,
  12. sendCodeDisabled: false,
  13. },
  14. onPhoneInput(e) {
  15. this.setData({
  16. phone: e.detail.value || e.detail
  17. });
  18. },
  19. onCodeInput(e) {
  20. this.setData({
  21. code: e.detail.value || e.detail
  22. });
  23. },
  24. sendCode() {
  25. if (this.data.sendCodeDisabled) return;
  26. // 手机号验证
  27. if (this.data.phone.length < 11) {
  28. wx.showToast({
  29. title: '手机号长度错误',
  30. icon: "none",
  31. });
  32. return;
  33. }
  34. var reg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
  35. if (!reg.test(this.data.phone)) {
  36. wx.showToast({
  37. title: '手机号格式错误',
  38. icon: "none",
  39. });
  40. return;
  41. }
  42. wx.request({
  43. url: app.globalData.apiBaseUrl || 'http://127.0.0.1:8000' + '/api/message/',
  44. data: { phone: this.data.phone },
  45. method: 'GET',
  46. success: (res) => {
  47. if (res.data && res.data.status) {
  48. this.setData({
  49. sendCodeDisabled: true,
  50. timer: setInterval(() => this.hcountDown(), 1000)
  51. });
  52. wx.showToast({
  53. title: res.data.message || '验证码发送成功',
  54. icon: "none",
  55. });
  56. } else {
  57. wx.showToast({
  58. title: (res.data && res.data.message) || '验证码发送失败',
  59. icon: "none",
  60. });
  61. }
  62. },
  63. fail: (err) => {
  64. wx.showToast({
  65. title: '网络错误,请重试',
  66. icon: 'none'
  67. });
  68. }
  69. });
  70. },
  71. tuichu(e) {
  72. wx.showModal({
  73. title: '提示',
  74. content: '确定要退出登录吗?',
  75. success: (res) => {
  76. if (res.confirm) {
  77. // 清除所有相关存储
  78. wx.removeStorageSync('token');
  79. wx.removeStorageSync('userInfo');
  80. // 重置全局数据
  81. app.globalData.token = null;
  82. app.globalData.userInfo = null;
  83. // 更新页面数据
  84. this.setData({
  85. userInfo: null,
  86. phone: "",
  87. code: ""
  88. });
  89. // 可选:跳转到登录页面
  90. wx.navigateTo({ url: '/pages/login/login' });
  91. }
  92. }
  93. });
  94. },
  95. denglu(e) {
  96. const pagePath = e.currentTarget.dataset.url;
  97. if (pagePath) {
  98. wx.navigateTo({ url: pagePath });
  99. } else {
  100. console.error('页面路径未定义');
  101. }
  102. },
  103. hcountDown() {
  104. var countDown = this.data.countDown - 1;
  105. if (countDown <= 0) {
  106. clearInterval(this.data.timer);
  107. this.setData({
  108. buttonText: '发送验证码',
  109. sendCodeDisabled: false,
  110. countDown: 60
  111. });
  112. return;
  113. }
  114. this.setData({
  115. buttonText: countDown + 's',
  116. countDown: countDown
  117. });
  118. },
  119. // 在login2方法中添加token验证
  120. // 确保登录成功后正确存储token
  121. login2(e) {
  122. wx.request({
  123. url: app.globalData.apiBaseUrl + '/api/login/',
  124. success: (res) => {
  125. if (res.data.status && res.data.data.token) {
  126. // 统一存储到storage和globalData
  127. wx.setStorageSync('token', res.data.data.token);
  128. app.globalData.token = res.data.data.token;
  129. // 测试token是否存储成功
  130. setTimeout(() => {
  131. console.log('Stored token:', wx.getStorageSync('token'));
  132. }, 500);
  133. }
  134. }
  135. });
  136. },
  137. denglu1(e) {
  138. const pagePath = e.currentTarget.dataset.url;
  139. const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
  140. if (userInfo && userInfo.token) {
  141. pagePath && wx.navigateTo({ url: pagePath });
  142. } else {
  143. wx.showToast({ title: '请先登录', icon: 'none' });
  144. wx.navigateTo({ url: '/pages/my/my' });
  145. }
  146. },
  147. login() {
  148. if (!this.data.phone || !this.data.code) {
  149. wx.showToast({ title: '请输入手机号和验证码', icon: 'none' });
  150. return;
  151. }
  152. wx.request({
  153. url: (app.globalData.apiBaseUrl || 'http://127.0.0.1:8000') + '/api/login/',
  154. method: 'POST',
  155. header: { 'Content-Type': 'application/json' },
  156. data: { phone: this.data.phone, code: this.data.code },
  157. success: (res) => {
  158. if (res.data && res.data.status) {
  159. const userData = res.data.data;
  160. const userInfo = {
  161. token: userData.token,
  162. id: userData.id,
  163. nickName: userData.nickName || '用户' + userData.id,
  164. avatarUrl: userData.avatarUrl || '/images/头像.png'
  165. };
  166. // 存储到storage和globalData
  167. wx.setStorageSync('token', userData.token);
  168. wx.setStorageSync('userInfo', userInfo);
  169. app.globalData.token = userData.token;
  170. app.globalData.userInfo = userInfo;
  171. this.setData({
  172. userInfo: userInfo,
  173. phone: "",
  174. code: ""
  175. });
  176. wx.showToast({
  177. title: '登录成功',
  178. icon: 'success'
  179. });
  180. } else {
  181. wx.showToast({
  182. title: (res.data && res.data.message) || '登录失败',
  183. icon: "none",
  184. });
  185. }
  186. },
  187. fail: (err) => {
  188. wx.showToast({ title: '网络错误', icon: 'none' });
  189. }
  190. });
  191. },
  192. onShow() {
  193. // 从storage中读取而不是globalData,因为globalData可能在页面刷新时丢失
  194. const token = wx.getStorageSync('token');
  195. const userInfo = wx.getStorageSync('userInfo');
  196. if (token && userInfo) {
  197. // 确保globalData同步
  198. app.globalData.token = token;
  199. app.globalData.userInfo = userInfo;
  200. this.setData({ userInfo: userInfo });
  201. } else {
  202. // 确保所有状态已清除
  203. this.setData({ userInfo: null });
  204. app.globalData.token = null;
  205. app.globalData.userInfo = null;
  206. }
  207. },
  208. onUnload() {
  209. clearInterval(this.data.timer);
  210. },
  211. });