完成首页基本操作及用户信息初始化
This commit is contained in:
parent
374bbdd933
commit
e3927b546a
@ -1,3 +1,5 @@
|
||||
import { info } from "./service/user-api";
|
||||
|
||||
// app.ts
|
||||
App<IAppOption>({
|
||||
globalData: {},
|
||||
@ -6,6 +8,23 @@ App<IAppOption>({
|
||||
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;
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
})
|
@ -1,9 +1,10 @@
|
||||
// index.ts
|
||||
|
||||
import { queryGoodsList } from "../../service/shop-api"
|
||||
import { createOrder, Goods, queryGoodsList } from "../../service/shop-api"
|
||||
import message from "../../utils/message";
|
||||
|
||||
// 获取应用实例
|
||||
// const app = getApp<IAppOption>()
|
||||
const app = getApp<IAppOption>()
|
||||
Page({
|
||||
data: {
|
||||
swiperList: [
|
||||
@ -39,7 +40,7 @@ Page({
|
||||
onReachBottom() {
|
||||
if (!this.data.hasMore) return;
|
||||
const page = this.data.page + 1;
|
||||
this.setData({page})
|
||||
this.setData({ page })
|
||||
this.loadGoodsList(page)
|
||||
},
|
||||
loadGoodsList(page: number) {
|
||||
@ -53,10 +54,11 @@ Page({
|
||||
hasMore: false
|
||||
})
|
||||
}
|
||||
const originList = page == 1 ? [] : this.data.goodsItems;
|
||||
this.setData({
|
||||
// @ts-ignore
|
||||
goodsItems: [
|
||||
...this.data.goodsItems,
|
||||
...originList,
|
||||
...result.records
|
||||
]
|
||||
})
|
||||
@ -73,5 +75,26 @@ Page({
|
||||
})
|
||||
// 所有其他的商品
|
||||
this.loadGoodsList(this.data.page);
|
||||
},
|
||||
createOrder(e) {
|
||||
if(!app.globalData.token){
|
||||
// message.toast('请先完成登录','none',()=>{
|
||||
// })
|
||||
|
||||
wx.switchTab({
|
||||
url: '/pages/personal/personal'
|
||||
})
|
||||
return false;
|
||||
}
|
||||
const goods = e.target.dataset.data as Goods;
|
||||
message.showLoading({ message: '兑换中...' })
|
||||
createOrder(goods.id).then(result => {
|
||||
console.log(result)
|
||||
message.toast('兑换成功')
|
||||
}).catch(e => {
|
||||
message.toast(e.message || '兑换失败')
|
||||
}).finally(() => {
|
||||
message.hideLoading()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -68,7 +68,7 @@
|
||||
<text>{{item.price}}积分</text>
|
||||
</view>
|
||||
<view class="actions">
|
||||
<button>立即兑换</button>
|
||||
<button data-data="{{item}}" bindtap="createOrder">立即兑换</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
@ -1,66 +1,35 @@
|
||||
// pages/personal.ts
|
||||
|
||||
import message from "../../utils/message";
|
||||
|
||||
const app = getApp<IAppOption>();
|
||||
Page({
|
||||
|
||||
/**
|
||||
* 页面的初始数据
|
||||
*/
|
||||
data: {
|
||||
/**
|
||||
* 页面的初始数据
|
||||
*/
|
||||
data: {
|
||||
userinfo: app.globalData.userInfo
|
||||
},
|
||||
|
||||
},
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad() {
|
||||
this.setData({
|
||||
userinfo: app.globalData.userInfo
|
||||
})
|
||||
},
|
||||
onLogin() {
|
||||
if(this.data.userinfo) return;
|
||||
// TODO 完成登录
|
||||
message.toast("开始登录")
|
||||
}
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad() {
|
||||
/**
|
||||
* 用户点击右上角分享
|
||||
*/
|
||||
onShareAppMessage() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面初次渲染完成
|
||||
*/
|
||||
onReady() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面显示
|
||||
*/
|
||||
onShow() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面隐藏
|
||||
*/
|
||||
onHide() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面卸载
|
||||
*/
|
||||
onUnload() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面相关事件处理函数--监听用户下拉动作
|
||||
*/
|
||||
onPullDownRefresh() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面上拉触底事件的处理函数
|
||||
*/
|
||||
onReachBottom() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 用户点击右上角分享
|
||||
*/
|
||||
onShareAppMessage() {
|
||||
|
||||
}
|
||||
}
|
||||
})
|
@ -8,10 +8,10 @@
|
||||
<view class="user-avatar-wrapper">
|
||||
<open-data class="user-avatar" type="userAvatarUrl"></open-data>
|
||||
</view>
|
||||
<text class="nickname">张三</text>
|
||||
<text class="nickname" bindtap="onLogin">{{userinfo?userinfo.nickname:'请点击登录'}}</text>
|
||||
</view>
|
||||
<view class="user-score">
|
||||
<text>1,121</text>
|
||||
<text>{{userinfo?.pointInfo?.totalPoint}}</text>
|
||||
</view>
|
||||
</div>
|
||||
</view>
|
||||
|
@ -1,5 +1,5 @@
|
||||
import message from "../../utils/message"
|
||||
import { info, sign } from "../../service/user-api"
|
||||
import { signInfo, sign } from "../../service/user-api"
|
||||
|
||||
// pages/sign/index.ts
|
||||
Page({
|
||||
@ -25,7 +25,7 @@ Page({
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad() {
|
||||
info().then(res=>{
|
||||
signInfo().then(res=>{
|
||||
this.setData(res)
|
||||
})
|
||||
},
|
||||
|
27
miniprogram/pages/types.d.ts
vendored
27
miniprogram/pages/types.d.ts
vendored
@ -1,5 +1,24 @@
|
||||
type UserInfo = {
|
||||
id: number
|
||||
nickname: string
|
||||
headImage: string
|
||||
interface PointInfo {
|
||||
uid: number;
|
||||
totalPoint: number;
|
||||
validPoint: number;
|
||||
expirePoint: number;
|
||||
expireTime?: any;
|
||||
updateTime: string;
|
||||
}
|
||||
|
||||
interface UserInfo {
|
||||
id: number;
|
||||
openId?: string;
|
||||
nickname: string;
|
||||
headImage: string;
|
||||
gender: number;
|
||||
province: string;
|
||||
city: string;
|
||||
parentId: number;
|
||||
firstLoginTime: string;
|
||||
updateTime?: any;
|
||||
status: number;
|
||||
token?: string
|
||||
pointInfo: PointInfo;
|
||||
}
|
@ -18,6 +18,17 @@ export type Goods = {
|
||||
updateTime: string;
|
||||
status: number;
|
||||
}
|
||||
export type OrderInfo = {
|
||||
id: string;
|
||||
gid: number;
|
||||
price: number;
|
||||
count: number;
|
||||
uid: number;
|
||||
data?: any;
|
||||
createTime?: any;
|
||||
updateTime?: any;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export function queryGoodsList(category: number, page = 1, pageSize = 10) {
|
||||
return request<DataList<Goods>>('/shop/goods/query', {
|
||||
@ -25,4 +36,9 @@ export function queryGoodsList(category: number, page = 1, pageSize = 10) {
|
||||
page,
|
||||
pageSize
|
||||
}, 'GET');
|
||||
}
|
||||
export function createOrder(goodsId: number, buyCount = 1) {
|
||||
return request<OrderInfo>('/shop/order/create', {
|
||||
goodsId, buyCount
|
||||
});
|
||||
}
|
@ -8,12 +8,18 @@ export type SignInfo = {
|
||||
/**
|
||||
* 签到信息
|
||||
*/
|
||||
export function info(){
|
||||
export function signInfo() {
|
||||
return request<SignInfo>('/sign/info');
|
||||
}
|
||||
/**
|
||||
* 签到
|
||||
*/
|
||||
export function sign(){
|
||||
export function sign() {
|
||||
return request<SignInfo>('/sign/today');
|
||||
}
|
||||
/**
|
||||
* 用户信息
|
||||
*/
|
||||
export function info(code: string = '') {
|
||||
return request<UserInfo>('/user/info', { code }, "GET");
|
||||
}
|
@ -1,10 +1,13 @@
|
||||
import { BizError } from "./request";
|
||||
|
||||
type ToastIcon = 'success' | 'error' | 'none';
|
||||
export function toast(message: string|any, icon: ToastIcon = 'none') {
|
||||
if(message instanceof BizError){
|
||||
export function toast(message: string | any, icon: ToastIcon = 'none', callback: (() => void) | null = null) {
|
||||
if (message instanceof BizError) {
|
||||
message = message.message
|
||||
}
|
||||
if (callback) {
|
||||
setTimeout(callback, 2000);
|
||||
}
|
||||
return wx.showToast({
|
||||
title: message,
|
||||
duration: 2000,
|
||||
@ -17,7 +20,7 @@ export function showLoading({ message = '加载中...', mask = true }) {
|
||||
mask
|
||||
})
|
||||
}
|
||||
export function hideLoading(){
|
||||
export function hideLoading() {
|
||||
wx.hideLoading()
|
||||
}
|
||||
|
||||
|
@ -31,8 +31,9 @@ function request<T>(api: string, data: any = null, method: HttpMethod = 'POST')
|
||||
const header: {
|
||||
[key: string]: string
|
||||
} = {}
|
||||
const app = getApp();
|
||||
// 判断是否有token
|
||||
const token = getApp().globalData.token
|
||||
const token = app ? app.globalData.token : wx.getStorageSync('user-token')
|
||||
if (token) { // 如果有token 添加token头
|
||||
header.token = token
|
||||
}
|
||||
@ -61,8 +62,8 @@ function request<T>(api: string, data: any = null, method: HttpMethod = 'POST')
|
||||
// 验证接口是否正确
|
||||
if (result.code !== 0) {
|
||||
if (result.code === 403) {
|
||||
wx.navigateTo({
|
||||
url: '/pages/user/login'
|
||||
wx.switchTab({
|
||||
url: '/pages/personal/personal'
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
interface IAppOption {
|
||||
globalData: {
|
||||
userInfo?: WechatMiniprogram.UserInfo,
|
||||
userInfo?: UserInfo,
|
||||
token?: string
|
||||
}
|
||||
userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback,
|
||||
|
Loading…
x
Reference in New Issue
Block a user