personal.js 4.5 KB

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