83 lines
1.6 KiB
TypeScript
83 lines
1.6 KiB
TypeScript
// pages/personal.ts
|
|
|
|
import message from "../../utils/message";
|
|
import { getUserProfile, login } from "../../utils/promise-hooks";
|
|
import request from "../../utils/request";
|
|
|
|
const app = getApp<IAppOption>();
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
code: '',
|
|
userinfo: {}
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
async onLoad() {
|
|
if (!app.globalData.token) {
|
|
// 发起登录的授权
|
|
const code = await login()
|
|
this.setData({
|
|
code
|
|
})
|
|
}
|
|
},
|
|
onShow() {
|
|
if (app.globalData.userInfo) {
|
|
this.setData({
|
|
userinfo: app.globalData.userInfo
|
|
})
|
|
}
|
|
},
|
|
async onLogin() {
|
|
if (app.globalData.userInfo) return;
|
|
// TODO 完成登录
|
|
if (!this.data.code) {
|
|
this.onLoad()
|
|
message.toast('初始化错误,请重新登录')
|
|
return;
|
|
}
|
|
try {
|
|
const data = await getUserProfile()
|
|
data.code = this.data.code
|
|
message.showLoading({ message: '登录中...' })
|
|
const res = await request<string>('/user/login', data)
|
|
//
|
|
console.log(res);
|
|
wx.setStorageSync("user-token", res)
|
|
app.updateUserInfo().then(userinfo => {
|
|
this.setData({
|
|
userinfo
|
|
})
|
|
});
|
|
|
|
} finally {
|
|
message.hideLoading()
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
|
|
},
|
|
openPage(e: any) {
|
|
if(!app.globalData.userInfo){
|
|
// 没有登录
|
|
return;
|
|
}
|
|
const dataset = e.currentTarget.dataset
|
|
if (dataset.url) {
|
|
wx.navigateTo({
|
|
url: dataset.url
|
|
})
|
|
}
|
|
}
|
|
}) |