123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 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() {},
- 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',
- })
- },
- })
|