123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // pages/personal/personal.ts
- import ActionSheet, { ActionSheetTheme, ActionSheetShowOption } from 'tdesign-miniprogram/action-sheet/index';
- import { decrptPhone, getUserInfo, updateUserInfo, updateAvatar } from '../../api/user'
- let genderHandler: { close: () => void; } | null = null
- const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- avatar: 'https://4dkk.4dage.com/miniapp-source/daikan/avatar_default.png',
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad() {
- },
- updateUserInfo(data: GlobalUserInfo) {
- this.setData({
- userInfo: data
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- const app = getApp<IAppOption>();
- app.watch('userInfo', this.updateUserInfo)
- },
- /**
- * 生命周期函数--监听页面显示
- */
- async onShow() {
- const isLogin = wx.getStorageSync('isLogin')
- const app = getApp<IAppOption>();
- this.setData({
- isLogin: isLogin
- })
- app.globalData.isLogin = isLogin
- await getUserInfo();
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- const app = getApp<IAppOption>();
- app.unwatch('userInfo', this.updateUserInfo)
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- handleLogout() {
- const app = getApp<IAppOption>();
- 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(): Promise<boolean> {
- let isExist = false
- wx.checkSession({
- success() {
- isExist = true
- },
- fail() {
- isExist = false
- }
- })
- await sleep(1000)
- return Promise.resolve(isExist)
- },
- async handleGetPhoneNumber(event: WechatMiniprogram.ButtonGetPhoneNumber) {
- // 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: ActionSheetShowOption = {
- theme: ActionSheetTheme.List,
- selector: '#t-action-sheet',
- items: [
- {
- label: '男',
- },
- {
- label: '女',
- },
- ],
- };
- genderHandler = ActionSheet.show(basicListOption);
- },
- async handleSelected(event: WechatMiniprogram.TouchEvent) {
- 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: WechatMiniprogram.TouchEvent) {
- 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: WechatMiniprogram.TouchEvent) {
- 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();
- }
- })
|