// pages/personal/personal.ts import ActionSheet, { ActionSheetTheme, ActionSheetShowOption } from 'tdesign-miniprogram/action-sheet/index'; // import { // decrptPhone, // getUserInfo, // updateUserInfo, // updateAvatar // } from '../../api/user' import { decrptPhone, updateUserInfo } from '../../../utils/request' 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() { const userInfo = wx.getStorageSync('userInfo') this.setData({ userInfo: userInfo }) }, updateUserInfo(data) { this.setData({ userInfo: data }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ async onShow() { }, /** * 生命周期函数--监听页面隐藏 */ 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 // }) const userInfo = wx.getStorageSync('userInfo') console.log('decrptPhone', code) const res = await decrptPhone(code) console.log('手机号', res.data.phoneNumber) if (res.code === 0) { const res = await updateUserInfo({ phone: res.data.phoneNumber }) console.log('更新后', res) wx.showToast({ title: '更新成功!', }) this.syncUserInfo(res.data); } } } }, 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 res = await updateUserInfo({ gender: Number(index) + 1 }) if (res.code === 0) { this.syncUserInfo(res.data) } } }, async onChooseAvatar(e) { const { avatarUrl } = e.detail console.log('e', e) const url = await updateAvatar(avatarUrl) const res = await updateUserInfo({ avatarUrl: url }) if (res.code === 0) { this.syncUserInfo(res.data) } }, async updateNickName(e) { const { value } = e.detail console.log('value-1', value) if (value) { const res = await updateUserInfo({ nickName: value }) console.log('res', res) // if (res.code === 0) { // this.syncUserInfo(res.data) // } } }, syncUserInfo(data) { const userInfo = wx.getStorageSync('userInfo'); if (userInfo) { const mergeObj = { ...userInfo, ...data } console.log('syncUserInfo', mergeObj) wx.setStorageSync('userInfo', mergeObj) this.setData({ userInfo: mergeObj }) } }, handleCancel() { genderHandler && genderHandler.close(); } })