完善登录及用户数据的更新

This commit is contained in:
LittleBoy 2022-11-28 17:29:10 +08:00
parent e3927b546a
commit 92ede7aca3
7 changed files with 136 additions and 70 deletions

View File

@ -4,27 +4,34 @@ import { info } from "./service/user-api";
App<IAppOption>({
globalData: {},
onLaunch() {
// 展示本地存储能力
const token = wx.getStorageSync('user-token')
if (token) {
this.globalData.token = token;
info().then(userinfo => {
this.globalData.userInfo = userinfo;
})
}else{
wx.login({
success:(e)=>{
if(e.code){
console.log(e)
info(e.code).then(userinfo => {
if(userinfo.token){
wx.setStorageSync("user-token",userinfo.token)
}
this.globalData.userInfo = userinfo;
})
this.updateUserInfo();
},
updateUserInfo() {
return new Promise<UserInfo>((resolve, reject) => {
// 展示本地存储能力
const token = wx.getStorageSync('user-token')
if (token) {
this.globalData.token = token;
info().then(userinfo => {
this.globalData.userInfo = userinfo;
resolve(userinfo);
}).catch(reject)
} else {
wx.login({
fail: reject,
success: (e) => {
if (e.code) {
info(e.code).then(userinfo => {
if (userinfo.token) {
wx.setStorageSync("user-token", userinfo.token)
}
this.globalData.userInfo = userinfo;
resolve(userinfo);
}).catch(reject)
}
}
}
})
}
})
}
})
}
})

View File

@ -90,6 +90,8 @@ Page({
message.showLoading({ message: '兑换中...' })
createOrder(goods.id).then(result => {
console.log(result)
// 更新用户信息
app.updateUserInfo();
message.toast('兑换成功')
}).catch(e => {
message.toast(e.message || '兑换失败')

View File

@ -6,9 +6,24 @@
padding: 10px;
display: flex;
text-align: center;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
.grid-item{
flex:1;
width: 25%;
}
.icon{
width: 50px;
height: 50px;
border-radius: 50%;
background: linear-gradient(to right,#91b2ff,#136dff);
margin: 10px auto;
}
.icon-sign{
background: linear-gradient(to right,#ffdfa5,#ff9a31);
}
.icon-game{
background: linear-gradient(to right,#ffaf8f,#ff6931);
}
}

View File

@ -1,6 +1,8 @@
// 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({
@ -9,27 +11,69 @@ Page({
*
*/
data: {
userinfo: app.globalData.userInfo
code: '',
userinfo: {}
},
/**
* --
*/
onLoad() {
this.setData({
userinfo: app.globalData.userInfo
})
async onLoad() {
if (!app.globalData.token) {
// 发起登录的授权
const code = await login()
this.setData({
code
})
}
},
onLogin() {
if(this.data.userinfo) return;
onShow() {
if (app.globalData.userInfo) {
this.setData({
userinfo: app.globalData.userInfo
})
}
},
async onLogin() {
if (app.globalData.userInfo) return;
// TODO 完成登录
message.toast("开始登录")
}
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) {
const dataset = e.currentTarget.dataset
if (dataset.url) {
wx.navigateTo({
url: dataset.url
})
}
}
})

View File

@ -3,49 +3,45 @@
<view class="content-bg"></view>
<view class="content-wrapper">
<view class="header">
<div class="content-container">
<view class="user-info">
<view class="user-avatar-wrapper">
<open-data class="user-avatar" type="userAvatarUrl"></open-data>
</view>
<text class="nickname" bindtap="onLogin">{{userinfo?userinfo.nickname:'请点击登录'}}</text>
</view>
<view class="user-score">
<text>{{userinfo?.pointInfo?.totalPoint}}</text>
</view>
</div>
<div class="content-container">
<view class="user-info">
<view class="user-avatar-wrapper">
<open-data class="user-avatar" type="userAvatarUrl"></open-data>
</view>
<text class="nickname" bindtap="onLogin">{{userinfo && userinfo.nickname?userinfo.nickname:'请点击登录'}}</text>
</view>
<view class="user-score">
<text>{{userinfo?userinfo.pointInfo.totalPoint:''}}</text>
</view>
</div>
</view>
<view>
<view class="grid">
<view class="grid-item">
<view class="icon">
</view>
<view class="text">
<text>每次签到</text>
</view>
</view>
<view class="grid-item">
<view class="icon">
</view>
<view class="text">
<text>兑换记录</text>
</view>
</view>
<view class="grid-item">
<view class="icon">
</view>
<view class="text">
<text>积分游戏</text>
</view>
</view>
<view class="grid-item">
<view class="icon">
</view>
<view class="text">
<text>我的收藏</text>
</view>
</view>
<view class="grid">
<view class="grid-item" bindtap="openPage" data-url="/pages/sign/index">
<view class="icon icon-sign"></view>
<view class="text">
<text>每次签到</text>
</view>
</view>
<view class="grid-item">
<view class="icon icon-history"></view>
<view class="text">
<text>兑换记录</text>
</view>
</view>
<view class="grid-item">
<view class="icon icon-game"></view>
<view class="text">
<text>积分游戏</text>
</view>
</view>
<view class="grid-item">
<view class="icon icon-collection"></view>
<view class="text">
<text>我的收藏</text>
</view>
</view>
</view>
</view>
</view>
</view>

View File

@ -32,6 +32,7 @@ Page({
onSignTag(){
message.showLoading({})
sign().then(res=>{
getApp<IAppOption>().updateUserInfo();
message.hideLoading()
this.setData(res)
}).catch(err=>{

1
typings/index.d.ts vendored
View File

@ -6,4 +6,5 @@ interface IAppOption {
token?: string
}
userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback,
updateUserInfo:()=>Promise<UserInfo>
}