Files
bodyWeight/miniprogram/pages/login/login.ts
T

42 lines
1.1 KiB
TypeScript

import { checkOpenIdLogin } from '../../api/index'
const app = getApp<IAppOption>()
Page({
data: {
loading: false,
},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/login/login',
imageUrl: '/images/share/share.jpg',
}
},
/* 微信一键登录 */
onLogin() {
if (this.data.loading) return
this.setData({ loading: true })
// 获取微信临时 code → 调用 OpenID 登录接口
app.getWxLoginCode().then((code) => checkOpenIdLogin(code)).then((res) => {
const { token, user } = res.result
// 持久化登录凭证和用户信息
wx.setStorageSync('token', token)
if (user) {
wx.setStorageSync('userInfo', user)
wx.reLaunch({ url: '/pages/home/home' })
} else {
wx.reLaunch({ url: '/pages/supplementPersonal/supplementPersonal' })
}
}).catch((err) => {
console.error('[login] 登录失败:', err)
wx.showToast({ title: err.msg || '登录失败,请重试', icon: 'none' })
}).finally(() => {
this.setData({ loading: false })
})
},
})