import dayjs from 'dayjs' import AuthApi from '../../services/auth' import Navi from '../../utils/navi' /* eslint-disable no-param-reassign */ /* eslint-disable no-nested-ternary */ //@ts-ignore import Toast from 'tdesign-miniprogram/toast/index' import { delay } from '../../utils/util' // pages/login/index.ts Page({ /** * 页面的初始数据 */ data: { providerKey: '', loading: false, returnUrl: '', prepare: '', }, /** * 生命周期函数--监听页面加载 */ onLoad(query: { returnUrl: string }) { if (query && query.returnUrl) { this.setData({ returnUrl: query.returnUrl, }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { wx.clearStorageSync() const self = this this.setData({ loading: true, prepare: 'preinit', }) wx.login({ async success(res) { if (res.code) { AuthApi.Authenticate({ providerAccessCode: res.code, }) .then((rsp) => { if (rsp.result) { if (rsp.result.providerKey) { self.setData({ providerKey: rsp.result.providerKey, prepare: 'need', }) } else if (rsp.result.signature) { self.onwork(rsp.result) } } self.setData({ loading: false, }) }) .catch(() => { self.setData( { loading: false, }, async () => { await delay(1000) self.onShow() }, ) }) } else { self.setData({ loading: false, }) } }, }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ // onShareAppMessage() { // let pages = getCurrentPages(); //获取所有页面栈实例列表 // let nowPage = pages[pages.length - 1]; //当前页页面实例 // return { // title: share.title, // path: `/${nowPage.route}`, // imageUrl: share.imageUrl, // success(res) { // console.log('success(res)==', res); // }, // fail(res) { // console.log('fail(res)==', res); // } // } // }, getPhoneNumber( e: WechatMiniprogram.CustomEvent<{ encryptedData: string; iv: string; errMsg: string }>, ) { console.log(e, this.data.providerKey) if (e.detail.errMsg !== 'getPhoneNumber:ok') { return } const self = this if (!this.data.providerKey || this.data.providerKey.length === 0) { this.setData({ loading: true, prepare: 'login', }) AuthApi.Entrance({ seCode: this.data.providerKey, encryptedData: e.detail.encryptedData, iv: e.detail.iv, }) .then((rsp2) => { if (rsp2.result) { self.onwork(rsp2.result) return } self.setData({ loading: false, prepare: 'need', }) }) .catch(() => { Toast({ context: this, selector: '#t-toast', message: '授权不成功,请重试', icon: '', duration: 1000, }) self.setData({ loading: false, prepare: 'need', }) }) return } this.setData({ loading: true, }) AuthApi.Entrance({ seCode: this.data.providerKey, encryptedData: e.detail.encryptedData, iv: e.detail.iv, }) .then((rsp) => { console.log(rsp) if (rsp.result) { this.onwork(rsp.result) } }) .finally(() => { this.setData({ loading: false, }) }) }, onwork(info: OpenUserInfo) { info.expiration = dayjs().add(info.expiration, 'second') as any wx.setStorageSync('__uvx', JSON.stringify(info)) let url = '' if (this.data.returnUrl && this.data.returnUrl.length > 0) { url = this.data.returnUrl } if (url.length > 0) { Navi.redirectTo({ url, }) return } const pages = getCurrentPages() //获取小程序页面栈 // const beforePage = pages[pages.length - 2] //获取上个页面的实例对象 // beforePage.research(keyword) //触发上个页面自定义的shuaxin方法 if (pages.length - 2 > -1) { const beforePage = pages[pages.length - 2] if (beforePage.init) { beforePage.init() } wx.navigateBack({ delta: 1, }) return } Navi.switchTab({ url: '/pages/mine/personal', }) }, })