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

87 lines
2.5 KiB
TypeScript

import { post } from '../../utils/request/index'
/** 通过 OpenID 登录的用户信息 */
interface UserInfo {
id: string
userId: string
parentUserId: string | null
scaleDeviceId: string
deviceId: string | null
deviceType: string
dataType: number
userType: string | null
idCard: string
height: number
weight: number
birthday: string
sex: number
avatar: string | null
phone: string | null
thirdId: string
realname: string
bmi: number
createBy: string
createTime: string
updateBy: string | null
updateTime: string | null
delFlag: number
sn: string | null
workNo: string | null
remark: string | null
relationType: string | null
sex_dictText: string
connectDeviceInfo: string
}
/** 登录接口 result 结构 */
interface CheckOpenIdLoginResult {
user: UserInfo | null
token: string
}
Page({
data: {
loading: false,
},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
},
/* 微信一键登录 */
onLogin() {
if (this.data.loading) return
this.setData({ loading: true })
wx.login({
success: ({ code }) => {
post<CheckOpenIdLoginResult>('weighingScale/v2/checkOpenIdLogin', { code })
.then(({ result }) => {
wx.setStorageSync('token', result.token)
wx.setStorageSync('userInfo', result.user)
/* 上次连接设备不为空对象则直接进首页 */
const connectDeviceInfo = result.user?.connectDeviceInfo
if (connectDeviceInfo && connectDeviceInfo !== '{}') {
wx.setStorageSync('connectDeviceInfo', JSON.parse(connectDeviceInfo))
wx.reLaunch({ url: '/pages/home/home' })
} else {
wx.reLaunch({ url: '/pages/connectedDevice/connectedDevice' })
}
})
.catch(() => {
this.setData({ loading: false })
})
},
fail: () => {
wx.showToast({ title: '获取登录凭证失败', icon: 'none' })
this.setData({ loading: false })
},
})
},
})