104 lines
2.7 KiB
TypeScript
104 lines
2.7 KiB
TypeScript
// index.ts
|
|
|
|
import { createOrder, Goods, queryGoodsList } from "../../service/shop-api"
|
|
import message from "../../utils/message";
|
|
|
|
// 获取应用实例
|
|
const app = getApp<IAppOption>()
|
|
Page({
|
|
data: {
|
|
swiperList: [
|
|
{
|
|
image: 'https://m15.360buyimg.com/mobilecms/jfs/t1/160398/4/32302/103427/6374d1f3E5b1ecb32/a593b9982d8378cc.jpg!cr_1125x449_0_166!q70.jpg',
|
|
url: null,
|
|
},
|
|
{
|
|
image: 'https://m15.360buyimg.com/mobilecms/jfs/t1/203775/20/26428/95041/637fab4dEf6a4434d/3f8770efe691537b.jpg!cr_1053x420_4_0!q70.jpg',
|
|
url: null,
|
|
},
|
|
{
|
|
image: 'https://m15.360buyimg.com/mobilecms/jfs/t1/160398/4/32302/103427/6374d1f3E5b1ecb32/a593b9982d8378cc.jpg!cr_1125x449_0_166!q70.jpg',
|
|
url: null,
|
|
},
|
|
{
|
|
image: 'https://m15.360buyimg.com/mobilecms/jfs/t1/203775/20/26428/95041/637fab4dEf6a4434d/3f8770efe691537b.jpg!cr_1053x420_4_0!q70.jpg',
|
|
url: null,
|
|
}
|
|
],
|
|
goodsItems: [],
|
|
recommendItems: [],
|
|
page: 1,
|
|
hasMore: true,
|
|
pageSize: 10
|
|
},
|
|
onPullDownRefresh() {
|
|
this.setData({
|
|
page: 1
|
|
})
|
|
this.onLoad()
|
|
},
|
|
onReachBottom() {
|
|
if (!this.data.hasMore) return;
|
|
const page = this.data.page + 1;
|
|
this.setData({ page })
|
|
this.loadGoodsList(page)
|
|
},
|
|
loadGoodsList(page: number) {
|
|
|
|
// 所有其他的商品
|
|
queryGoodsList(1, page, this.data.pageSize).then((result) => {
|
|
//判断是否还有数据没有查询到
|
|
const count = page * this.data.pageSize;
|
|
if (count >= result.total) {
|
|
this.setData({
|
|
hasMore: false
|
|
})
|
|
}
|
|
const originList = page == 1 ? [] : this.data.goodsItems;
|
|
this.setData({
|
|
// @ts-ignore
|
|
goodsItems: [
|
|
...originList,
|
|
...result.records
|
|
]
|
|
})
|
|
})
|
|
},
|
|
onLoad() {
|
|
// 推荐商品
|
|
queryGoodsList(2, 1, 3).then((result) => {
|
|
wx.stopPullDownRefresh();
|
|
this.setData({
|
|
// @ts-ignore
|
|
recommendItems: result.records
|
|
})
|
|
})
|
|
// 所有其他的商品
|
|
this.loadGoodsList(this.data.page);
|
|
},
|
|
createOrder(e: any) {
|
|
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)
|
|
// 更新用户信息
|
|
app.updateUserInfo();
|
|
message.toast('兑换成功')
|
|
}).catch(e => {
|
|
message.toast(e.message || '兑换失败')
|
|
}).finally(() => {
|
|
message.hideLoading()
|
|
})
|
|
return;
|
|
}
|
|
})
|