From 92ede7aca3235e75fc0a1325342bd178be435f60 Mon Sep 17 00:00:00 2001 From: callmeyan Date: Mon, 28 Nov 2022 17:29:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=99=BB=E5=BD=95=E5=8F=8A?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=95=B0=E6=8D=AE=E7=9A=84=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/app.ts | 49 ++++++++------- miniprogram/pages/index/index.ts | 2 + miniprogram/pages/personal/personal.less | 15 +++++ miniprogram/pages/personal/personal.ts | 62 ++++++++++++++++--- miniprogram/pages/personal/personal.wxml | 76 +++++++++++------------- miniprogram/pages/sign/index.ts | 1 + typings/index.d.ts | 1 + 7 files changed, 136 insertions(+), 70 deletions(-) diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 0b4548a..7ef2a21 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -4,27 +4,34 @@ import { info } from "./service/user-api"; App({ 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((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) + } } - } - }) - } + }) + } + }) } }) \ No newline at end of file diff --git a/miniprogram/pages/index/index.ts b/miniprogram/pages/index/index.ts index 0c974d7..6b6a78d 100644 --- a/miniprogram/pages/index/index.ts +++ b/miniprogram/pages/index/index.ts @@ -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 || '兑换失败') diff --git a/miniprogram/pages/personal/personal.less b/miniprogram/pages/personal/personal.less index 41c1bae..a7fa0fa 100644 --- a/miniprogram/pages/personal/personal.less +++ b/miniprogram/pages/personal/personal.less @@ -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); + + } } \ No newline at end of file diff --git a/miniprogram/pages/personal/personal.ts b/miniprogram/pages/personal/personal.ts index 092f9ac..93b7a85 100644 --- a/miniprogram/pages/personal/personal.ts +++ b/miniprogram/pages/personal/personal.ts @@ -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(); 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('/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 + }) + } } }) \ No newline at end of file diff --git a/miniprogram/pages/personal/personal.wxml b/miniprogram/pages/personal/personal.wxml index a04c26f..f844261 100644 --- a/miniprogram/pages/personal/personal.wxml +++ b/miniprogram/pages/personal/personal.wxml @@ -3,49 +3,45 @@ -
- - - {{userinfo?.pointInfo?.totalPoint}} - -
+
+ + + {{userinfo?userinfo.pointInfo.totalPoint:''}} + +
- - - - - - 每次签到 - - - - - - - 兑换记录 - - - - - - - 积分游戏 - - - - - - - 我的收藏 - - + + + + + 每次签到 + + + + + 兑换记录 + + + + + + 积分游戏 + + + + + + 我的收藏 + + +
\ No newline at end of file diff --git a/miniprogram/pages/sign/index.ts b/miniprogram/pages/sign/index.ts index 1b5cad9..d0cf0bc 100644 --- a/miniprogram/pages/sign/index.ts +++ b/miniprogram/pages/sign/index.ts @@ -32,6 +32,7 @@ Page({ onSignTag(){ message.showLoading({}) sign().then(res=>{ + getApp().updateUserInfo(); message.hideLoading() this.setData(res) }).catch(err=>{ diff --git a/typings/index.d.ts b/typings/index.d.ts index 3011c86..5c1fc82 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -6,4 +6,5 @@ interface IAppOption { token?: string } userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback, + updateUserInfo:()=>Promise } \ No newline at end of file