personal.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // pages/personal/personal.ts
  2. import ActionSheet, { ActionSheetTheme, ActionSheetShowOption } from 'tdesign-miniprogram/action-sheet/index';
  3. import { decrptPhone, getUserInfo, updateUserInfo, updateAvatar } from '../../api/user'
  4. let genderHandler: { close: () => void; } | null = null
  5. const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. avatar: 'https://4dkk.4dage.com/miniapp-source/daikan/avatar_default.png',
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad() {
  17. },
  18. updateUserInfo(data: GlobalUserInfo) {
  19. this.setData({
  20. userInfo: data
  21. })
  22. },
  23. /**
  24. * 生命周期函数--监听页面初次渲染完成
  25. */
  26. onReady() {
  27. const app = getApp<IAppOption>();
  28. app.watch('userInfo', this.updateUserInfo)
  29. },
  30. /**
  31. * 生命周期函数--监听页面显示
  32. */
  33. async onShow() {
  34. const isLogin = wx.getStorageSync('isLogin')
  35. const app = getApp<IAppOption>();
  36. this.setData({
  37. isLogin: isLogin
  38. })
  39. app.globalData.isLogin = isLogin
  40. await getUserInfo();
  41. },
  42. /**
  43. * 生命周期函数--监听页面隐藏
  44. */
  45. onHide() {
  46. },
  47. /**
  48. * 生命周期函数--监听页面卸载
  49. */
  50. onUnload() {
  51. const app = getApp<IAppOption>();
  52. app.unwatch('userInfo', this.updateUserInfo)
  53. },
  54. /**
  55. * 页面相关事件处理函数--监听用户下拉动作
  56. */
  57. onPullDownRefresh() {
  58. },
  59. /**
  60. * 页面上拉触底事件的处理函数
  61. */
  62. onReachBottom() {
  63. },
  64. /**
  65. * 用户点击右上角分享
  66. */
  67. onShareAppMessage() {
  68. },
  69. handleLogout() {
  70. const app = getApp<IAppOption>();
  71. this.setData({
  72. isLogin: false
  73. })
  74. wx.setStorageSync('isLogin', false)
  75. wx.setStorageSync('token', '')
  76. app.setLogin(false)
  77. app.resetUserInfo();
  78. wx.showToast({
  79. title: '登出成功!',
  80. });
  81. wx.switchTab({
  82. url: "/pages/my/my"
  83. })
  84. },
  85. async checkSession(): Promise<boolean> {
  86. let isExist = false
  87. wx.checkSession({
  88. success() {
  89. isExist = true
  90. },
  91. fail() {
  92. isExist = false
  93. }
  94. })
  95. await sleep(1000)
  96. return Promise.resolve(isExist)
  97. },
  98. async handleGetPhoneNumber(event: WechatMiniprogram.ButtonGetPhoneNumber) {
  99. // debugger
  100. console.log('event', event)
  101. const isValid = await this.checkSession();
  102. console.log('isValid', isValid)
  103. if (isValid) {
  104. const { code, iv, encryptedData } = event.detail
  105. if (code && iv && encryptedData) {
  106. const res = await decrptPhone({
  107. iv: iv,
  108. code: code,
  109. encryptedData: encryptedData
  110. })
  111. console.log('res', res.phoneNumber)
  112. if (res.phoneNumber) {
  113. const wxUserId = wx.getStorageSync('wxUserId');
  114. const result = await updateUserInfo({
  115. wxUserId,
  116. phoneNumber: res.phoneNumber
  117. })
  118. if (result.code === 0) {
  119. await getUserInfo();
  120. } else {
  121. wx.showToast({
  122. title: result.message
  123. });
  124. }
  125. }
  126. }
  127. }
  128. },
  129. handleSexSelectShow() {
  130. const basicListOption: ActionSheetShowOption = {
  131. theme: ActionSheetTheme.List,
  132. selector: '#t-action-sheet',
  133. items: [
  134. {
  135. label: '男',
  136. },
  137. {
  138. label: '女',
  139. },
  140. ],
  141. };
  142. genderHandler = ActionSheet.show(basicListOption);
  143. },
  144. async handleSelected(event: WechatMiniprogram.TouchEvent) {
  145. const { index } = event.detail
  146. if (index > -1) {
  147. // debugger
  148. console.log('index', index)
  149. const wxUserId = wx.getStorageSync('wxUserId');
  150. await updateUserInfo({
  151. wxUserId,
  152. gender: index + 1
  153. })
  154. await getUserInfo();
  155. }
  156. },
  157. async onChooseAvatar(e: WechatMiniprogram.TouchEvent) {
  158. const {
  159. avatarUrl
  160. } = e.detail
  161. console.log('e', e)
  162. const url = await updateAvatar(avatarUrl)
  163. console.log('url', url)
  164. const wxUserId = wx.getStorageSync('wxUserId');
  165. await updateUserInfo({
  166. wxUserId,
  167. avatarUrl: url
  168. })
  169. await getUserInfo();
  170. },
  171. async updateNickName(e: WechatMiniprogram.TouchEvent) {
  172. const { value } = e.detail
  173. console.log('value-1', value)
  174. if (value) {
  175. const wxUserId = wx.getStorageSync('wxUserId');
  176. await updateUserInfo({
  177. wxUserId,
  178. nickName: value
  179. })
  180. await getUserInfo();
  181. }
  182. },
  183. handleCancel() {
  184. genderHandler && genderHandler.close();
  185. }
  186. })