personal.js 4.8 KB

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