完善登录及用户数据的更新
This commit is contained in:
parent
e3927b546a
commit
92ede7aca3
@ -4,27 +4,34 @@ import { info } from "./service/user-api";
|
|||||||
App<IAppOption>({
|
App<IAppOption>({
|
||||||
globalData: {},
|
globalData: {},
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
// 展示本地存储能力
|
this.updateUserInfo();
|
||||||
const token = wx.getStorageSync('user-token')
|
},
|
||||||
if (token) {
|
updateUserInfo() {
|
||||||
this.globalData.token = token;
|
return new Promise<UserInfo>((resolve, reject) => {
|
||||||
info().then(userinfo => {
|
// 展示本地存储能力
|
||||||
this.globalData.userInfo = userinfo;
|
const token = wx.getStorageSync('user-token')
|
||||||
})
|
if (token) {
|
||||||
}else{
|
this.globalData.token = token;
|
||||||
wx.login({
|
info().then(userinfo => {
|
||||||
success:(e)=>{
|
this.globalData.userInfo = userinfo;
|
||||||
if(e.code){
|
resolve(userinfo);
|
||||||
console.log(e)
|
}).catch(reject)
|
||||||
info(e.code).then(userinfo => {
|
} else {
|
||||||
if(userinfo.token){
|
wx.login({
|
||||||
wx.setStorageSync("user-token",userinfo.token)
|
fail: reject,
|
||||||
}
|
success: (e) => {
|
||||||
this.globalData.userInfo = userinfo;
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
@ -90,6 +90,8 @@ Page({
|
|||||||
message.showLoading({ message: '兑换中...' })
|
message.showLoading({ message: '兑换中...' })
|
||||||
createOrder(goods.id).then(result => {
|
createOrder(goods.id).then(result => {
|
||||||
console.log(result)
|
console.log(result)
|
||||||
|
// 更新用户信息
|
||||||
|
app.updateUserInfo();
|
||||||
message.toast('兑换成功')
|
message.toast('兑换成功')
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
message.toast(e.message || '兑换失败')
|
message.toast(e.message || '兑换失败')
|
||||||
|
@ -6,9 +6,24 @@
|
|||||||
padding: 10px;
|
padding: 10px;
|
||||||
display: flex;
|
display: flex;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
|
||||||
.grid-item{
|
.grid-item{
|
||||||
flex:1;
|
flex:1;
|
||||||
width: 25%;
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
// pages/personal.ts
|
// pages/personal.ts
|
||||||
|
|
||||||
import message from "../../utils/message";
|
import message from "../../utils/message";
|
||||||
|
import { getUserProfile, login } from "../../utils/promise-hooks";
|
||||||
|
import request from "../../utils/request";
|
||||||
|
|
||||||
const app = getApp<IAppOption>();
|
const app = getApp<IAppOption>();
|
||||||
Page({
|
Page({
|
||||||
@ -9,27 +11,69 @@ Page({
|
|||||||
* 页面的初始数据
|
* 页面的初始数据
|
||||||
*/
|
*/
|
||||||
data: {
|
data: {
|
||||||
userinfo: app.globalData.userInfo
|
code: '',
|
||||||
|
userinfo: {}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生命周期函数--监听页面加载
|
* 生命周期函数--监听页面加载
|
||||||
*/
|
*/
|
||||||
onLoad() {
|
async onLoad() {
|
||||||
this.setData({
|
if (!app.globalData.token) {
|
||||||
userinfo: app.globalData.userInfo
|
// 发起登录的授权
|
||||||
})
|
const code = await login()
|
||||||
|
this.setData({
|
||||||
|
code
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onLogin() {
|
onShow() {
|
||||||
if(this.data.userinfo) return;
|
if (app.globalData.userInfo) {
|
||||||
|
this.setData({
|
||||||
|
userinfo: app.globalData.userInfo
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async onLogin() {
|
||||||
|
if (app.globalData.userInfo) return;
|
||||||
// TODO 完成登录
|
// 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() {
|
onShareAppMessage() {
|
||||||
|
|
||||||
|
},
|
||||||
|
openPage(e: any) {
|
||||||
|
const dataset = e.currentTarget.dataset
|
||||||
|
if (dataset.url) {
|
||||||
|
wx.navigateTo({
|
||||||
|
url: dataset.url
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
@ -3,49 +3,45 @@
|
|||||||
<view class="content-bg"></view>
|
<view class="content-bg"></view>
|
||||||
<view class="content-wrapper">
|
<view class="content-wrapper">
|
||||||
<view class="header">
|
<view class="header">
|
||||||
<div class="content-container">
|
<div class="content-container">
|
||||||
<view class="user-info">
|
<view class="user-info">
|
||||||
<view class="user-avatar-wrapper">
|
<view class="user-avatar-wrapper">
|
||||||
<open-data class="user-avatar" type="userAvatarUrl"></open-data>
|
<open-data class="user-avatar" type="userAvatarUrl"></open-data>
|
||||||
</view>
|
</view>
|
||||||
<text class="nickname" bindtap="onLogin">{{userinfo?userinfo.nickname:'请点击登录'}}</text>
|
<text class="nickname" bindtap="onLogin">{{userinfo && userinfo.nickname?userinfo.nickname:'请点击登录'}}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="user-score">
|
<view class="user-score">
|
||||||
<text>{{userinfo?.pointInfo?.totalPoint}}</text>
|
<text>{{userinfo?userinfo.pointInfo.totalPoint:''}}</text>
|
||||||
</view>
|
</view>
|
||||||
</div>
|
</div>
|
||||||
</view>
|
</view>
|
||||||
<view>
|
<view>
|
||||||
<view class="grid">
|
<view class="grid">
|
||||||
<view class="grid-item">
|
<view class="grid-item" bindtap="openPage" data-url="/pages/sign/index">
|
||||||
<view class="icon">
|
<view class="icon icon-sign"></view>
|
||||||
</view>
|
<view class="text">
|
||||||
<view class="text">
|
<text>每次签到</text>
|
||||||
<text>每次签到</text>
|
</view>
|
||||||
</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>
|
</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>
|
||||||
</view>
|
</view>
|
@ -32,6 +32,7 @@ Page({
|
|||||||
onSignTag(){
|
onSignTag(){
|
||||||
message.showLoading({})
|
message.showLoading({})
|
||||||
sign().then(res=>{
|
sign().then(res=>{
|
||||||
|
getApp<IAppOption>().updateUserInfo();
|
||||||
message.hideLoading()
|
message.hideLoading()
|
||||||
this.setData(res)
|
this.setData(res)
|
||||||
}).catch(err=>{
|
}).catch(err=>{
|
||||||
|
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@ -6,4 +6,5 @@ interface IAppOption {
|
|||||||
token?: string
|
token?: string
|
||||||
}
|
}
|
||||||
userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback,
|
userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback,
|
||||||
|
updateUserInfo:()=>Promise<UserInfo>
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user