personal.js 5.0 KB

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