123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- // 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,
- updateAvatar
- } 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)
- const phone = res.data.phoneNumber;
- if (res.code === 0) {
- const res = await updateUserInfo({
- phone: phone
- })
- console.log('更新后', res)
- wx.showToast({
- title: '更新成功!',
- })
- this.syncUserInfo({
- phone: phone
- });
- }
- }
- }
- },
- 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({
- gender: Number(index) + 1
- })
- }
- }
- },
- async onChooseAvatar(e) {
- const {
- avatarUrl
- } = e.detail
- console.log("微信tmp-file", avatarUrl)
- const url = await updateAvatar(avatarUrl)
- console.log('上传后URL',
- url)
- if (url) {
- const res = await updateUserInfo({
- avatarUrl: url
- })
- if (res.code === 0) {
- this.syncUserInfo({
- avatarUrl: url
- })
- }
- }
- },
- 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({
- nickName: value
- })
- }
- }
- },
- 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();
- }
- })
|