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