personalCenter.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // personalCenter.js
  2. Page({
  3. data: {
  4. userInfo: {},
  5. hasUserInfo: false,
  6. phone: '',
  7. code: '',
  8. codeBtnText: '获取验证码',
  9. codeTimer: null,
  10. codeCountdown: 60
  11. },
  12. onLoad() {
  13. const userInfo = wx.getStorageSync('userInfo');
  14. if (userInfo) {
  15. this.setData({
  16. userInfo,
  17. hasUserInfo: true
  18. });
  19. }
  20. },
  21. getUserInfo() {
  22. wx.getUserProfile({
  23. desc: '用于完善个人信息',
  24. success: res => {
  25. this.setData({
  26. userInfo: res.userInfo,
  27. hasUserInfo: true
  28. });
  29. wx.setStorageSync('userInfo', res.userInfo);
  30. },
  31. fail: err => {
  32. console.error('获取用户信息失败', err);
  33. }
  34. });
  35. },
  36. chooseAvatar() {
  37. wx.chooseMedia({
  38. count: 1,
  39. mediaType: ['image'],
  40. sourceType: ['album', 'camera'],
  41. success: res => {
  42. const avatarUrl = res.tempFiles[0].tempFilePath;
  43. this.setData({
  44. 'userInfo.avatarUrl': avatarUrl
  45. });
  46. const userInfo = this.data.userInfo;
  47. wx.setStorageSync('userInfo', userInfo);
  48. }
  49. });
  50. },
  51. onNicknameInput(e) {
  52. this.setData({
  53. 'userInfo.nickName': e.detail.value
  54. });
  55. },
  56. saveNickname() {
  57. const userInfo = this.data.userInfo;
  58. wx.setStorageSync('userInfo', userInfo);
  59. wx.showToast({
  60. title: '保存成功',
  61. icon: 'success'
  62. });
  63. },
  64. onPhoneInput(e) {
  65. this.setData({
  66. phone: e.detail.value
  67. });
  68. },
  69. onCodeInput(e) {
  70. this.setData({
  71. code: e.detail.value
  72. });
  73. },
  74. getVerificationCode() {
  75. const { phone, codeTimer } = this.data;
  76. if (!phone) {
  77. wx.showToast({
  78. title: '请输入手机号',
  79. icon: 'none'
  80. });
  81. return;
  82. }
  83. if (codeTimer) {
  84. return;
  85. }
  86. // 模拟发送验证码
  87. wx.showToast({
  88. title: '验证码已发送',
  89. icon: 'success'
  90. });
  91. this.startCodeCountdown();
  92. },
  93. startCodeCountdown() {
  94. let { codeCountdown } = this.data;
  95. const codeTimer = setInterval(() => {
  96. codeCountdown--;
  97. if (codeCountdown <= 0) {
  98. clearInterval(codeTimer);
  99. this.setData({
  100. codeBtnText: '获取验证码',
  101. codeTimer: null,
  102. codeCountdown: 60
  103. });
  104. } else {
  105. this.setData({
  106. codeBtnText: `${codeCountdown}s后重试`,
  107. codeTimer,
  108. codeCountdown
  109. });
  110. }
  111. }, 1000);
  112. this.setData({
  113. codeTimer
  114. });
  115. },
  116. login() {
  117. const { phone, code } = this.data;
  118. if (!phone || !code) {
  119. wx.showToast({
  120. title: '请输入手机号和验证码',
  121. icon: 'none'
  122. });
  123. return;
  124. }
  125. // 模拟登录验证
  126. wx.showToast({
  127. title: '登录成功',
  128. icon: 'success'
  129. });
  130. this.setData({
  131. hasUserInfo: true,
  132. userInfo: {
  133. nickName: `用户_${phone}`,
  134. avatarUrl: 'default_avatar_url'
  135. }
  136. });
  137. wx.setStorageSync('userInfo', this.data.userInfo);
  138. },
  139. logout() {
  140. wx.removeStorageSync('userInfo');
  141. this.setData({
  142. hasUserInfo: false,
  143. userInfo: {}
  144. });
  145. wx.showToast({
  146. title: '登出成功',
  147. icon: 'success'
  148. });
  149. }
  150. });