|
@@ -0,0 +1,228 @@
|
|
|
+// pages/personal/personal.ts
|
|
|
+import ActionSheet, {
|
|
|
+ ActionSheetTheme,
|
|
|
+ ActionSheetShowOption
|
|
|
+} from 'tdesign-miniprogram/action-sheet/index';
|
|
|
+// import {
|
|
|
+// decrptPhone,
|
|
|
+// getUserInfo,
|
|
|
+// updateUserInfo,
|
|
|
+// updateAvatar
|
|
|
+// } from '../../api/user'
|
|
|
+
|
|
|
+let genderHandler = null
|
|
|
+
|
|
|
+const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
|
+Page({
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面的初始数据
|
|
|
+ */
|
|
|
+ data: {
|
|
|
+ avatar: 'https://4dkk.4dage.com/miniapp-source/daikan/avatar_default.png',
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面加载
|
|
|
+ */
|
|
|
+ onLoad() {
|
|
|
+
|
|
|
+ },
|
|
|
+ updateUserInfo(data) {
|
|
|
+ this.setData({
|
|
|
+ userInfo: data
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面初次渲染完成
|
|
|
+ */
|
|
|
+ onReady() {
|
|
|
+ // const app = getApp();
|
|
|
+ // app.watch('userInfo', this.updateUserInfo)
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面显示
|
|
|
+ */
|
|
|
+ async onShow() {
|
|
|
+ // const isLogin = wx.getStorageSync('isLogin')
|
|
|
+ // const app = getApp();
|
|
|
+ // this.setData({
|
|
|
+ // isLogin: isLogin
|
|
|
+ // })
|
|
|
+ // app.globalData.isLogin = isLogin
|
|
|
+ // await getUserInfo();
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面隐藏
|
|
|
+ */
|
|
|
+ onHide() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面卸载
|
|
|
+ */
|
|
|
+ onUnload() {
|
|
|
+ // const app = getApp();
|
|
|
+ // app.unwatch('userInfo', this.updateUserInfo)
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面相关事件处理函数--监听用户下拉动作
|
|
|
+ */
|
|
|
+ onPullDownRefresh() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面上拉触底事件的处理函数
|
|
|
+ */
|
|
|
+ onReachBottom() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户点击右上角分享
|
|
|
+ */
|
|
|
+ onShareAppMessage() {
|
|
|
+
|
|
|
+ },
|
|
|
+ handleLogout() {
|
|
|
+ const app = getApp();
|
|
|
+ this.setData({
|
|
|
+ isLogin: false
|
|
|
+ })
|
|
|
+ wx.setStorageSync('isLogin', false)
|
|
|
+ wx.setStorageSync('token', '')
|
|
|
+ app.setLogin(false)
|
|
|
+ app.resetUserInfo();
|
|
|
+ wx.showToast({
|
|
|
+ title: '登出成功!',
|
|
|
+
|
|
|
+ });
|
|
|
+ wx.switchTab({
|
|
|
+ url: "/pages/my/my"
|
|
|
+ })
|
|
|
+
|
|
|
+ },
|
|
|
+ async checkSession() {
|
|
|
+ let isExist = false
|
|
|
+ wx.checkSession({
|
|
|
+ success() {
|
|
|
+ isExist = true
|
|
|
+ },
|
|
|
+ fail() {
|
|
|
+ isExist = false
|
|
|
+ }
|
|
|
+ })
|
|
|
+ await sleep(1000)
|
|
|
+ return Promise.resolve(isExist)
|
|
|
+
|
|
|
+ },
|
|
|
+ async handleGetPhoneNumber(event) {
|
|
|
+ // debugger
|
|
|
+ console.log('event', event)
|
|
|
+ const isValid = await this.checkSession();
|
|
|
+ console.log('isValid', isValid)
|
|
|
+ if (isValid) {
|
|
|
+ const {
|
|
|
+ code,
|
|
|
+ iv,
|
|
|
+ encryptedData
|
|
|
+ } = event.detail
|
|
|
+ if (code && iv && encryptedData) {
|
|
|
+ const res = await decrptPhone({
|
|
|
+ iv: iv,
|
|
|
+ code: code,
|
|
|
+ encryptedData: encryptedData
|
|
|
+ })
|
|
|
+ console.log('res', res.phoneNumber)
|
|
|
+ if (res.phoneNumber) {
|
|
|
+ const wxUserId = wx.getStorageSync('wxUserId');
|
|
|
+ const result = await updateUserInfo({
|
|
|
+ wxUserId,
|
|
|
+ phoneNumber: res.phoneNumber
|
|
|
+ })
|
|
|
+ if (result.code === 0) {
|
|
|
+ await getUserInfo();
|
|
|
+ } else {
|
|
|
+ wx.showToast({
|
|
|
+ title: result.message
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ handleSexSelectShow() {
|
|
|
+ const basicListOption = {
|
|
|
+ theme: ActionSheetTheme.List,
|
|
|
+ selector: '#t-action-sheet',
|
|
|
+ items: [{
|
|
|
+ label: '男',
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: '女',
|
|
|
+ },
|
|
|
+
|
|
|
+ ],
|
|
|
+ };
|
|
|
+ genderHandler = ActionSheet.show(basicListOption);
|
|
|
+ },
|
|
|
+ async handleSelected(event) {
|
|
|
+ const {
|
|
|
+ index
|
|
|
+ } = event.detail
|
|
|
+ if (index > -1) {
|
|
|
+ // debugger
|
|
|
+ console.log('index', index)
|
|
|
+ const wxUserId = wx.getStorageSync('wxUserId');
|
|
|
+ await updateUserInfo({
|
|
|
+ wxUserId,
|
|
|
+ gender: index + 1
|
|
|
+ })
|
|
|
+ await getUserInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ async onChooseAvatar(e) {
|
|
|
+ const {
|
|
|
+ avatarUrl
|
|
|
+ } = e.detail
|
|
|
+
|
|
|
+ console.log('e', e)
|
|
|
+ const url = await updateAvatar(avatarUrl)
|
|
|
+ console.log('url', url)
|
|
|
+ const wxUserId = wx.getStorageSync('wxUserId');
|
|
|
+ await updateUserInfo({
|
|
|
+ wxUserId,
|
|
|
+ avatarUrl: url
|
|
|
+ })
|
|
|
+ await getUserInfo();
|
|
|
+ },
|
|
|
+
|
|
|
+ async updateNickName(e) {
|
|
|
+ const {
|
|
|
+ value
|
|
|
+ } = e.detail
|
|
|
+ console.log('value-1', value)
|
|
|
+ if (value) {
|
|
|
+ const wxUserId = wx.getStorageSync('wxUserId');
|
|
|
+ await updateUserInfo({
|
|
|
+ wxUserId,
|
|
|
+ nickName: value
|
|
|
+ })
|
|
|
+ await getUserInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+ handleCancel() {
|
|
|
+ genderHandler && genderHandler.close();
|
|
|
+ }
|
|
|
+})
|