index.js
4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// packageMine/pages/order/index/index.js
import {
ORDER_STATUS,
OrderStatusTypes,
OrderStatusEnum,
} from "~/constants/mine.js";
import { calcOrderProp } from "~/packageMine/pages/order/utils";
Page({
/**
* 页面的初始数据
*/
data: {
pageNum: 1,
tab: Object.freeze(
Object.values(ORDER_STATUS).filter(
(item) =>
item.type === OrderStatusTypes.QUERY_TAB ||
item.type === OrderStatusTypes.MY_ORDER
)
),
list: [],
listFlag: true,
hasUserInfo: true,
inputDebounceTimer: null,
curTab: { index: 0, requestType: "0", requestMethod: "getOrderList" },
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
// 清理防抖定时器,避免内存泄漏
if (this.data.inputDebounceTimer) {
clearTimeout(this.data.inputDebounceTimer);
}
},
navbarReady(e) {
wx.p.wxApi.getViewInfo(".tab", (res) => {
this.setData({
padding_bottom: e.detail.height,
margin_top: e.detail.height + res.height,
});
});
},
back() {
let { length } = getCurrentPages();
if (length == 1) wx.p.router.switchTab("/pages/tab/mine/index");
else wx.p.router.back();
},
changeTab(e) {
const tab = e.detail.item;
this.setIndexAndRequest(tab);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.p.router.getRouterParams(options).then((option) => {
let tab = option.tab;
if (!tab || !tab.index) {
tab = ORDER_STATUS[OrderStatusEnum.ALL];
}
this.setIndexAndRequest(tab);
});
},
setIndexAndRequest(tab) {
this.setData(
{
curTab: tab,
},
() => this.clearAndGetList()
);
},
onShow() {
if (this.refresh) this.clearAndGetList();
},
onHide() {
this.refresh = true;
},
async clearData(cb) {
const res = !wx.p.isLogin;
if (res) return;
this.setData(
{
listFlag: false,
list: [],
nothing: false,
pageNum: 1,
},
cb
);
},
clearAndGetList() {
this.clearData(() => this.getOrderList());
},
search() {
this.clearAndGetList();
},
clear() {
this.setData(
{
keyword: "",
},
() => this.clearAndGetList()
);
},
input(e) {
if (this.data.inputDebounceTimer) {
clearTimeout(this.data.inputDebounceTimer);
}
const timer = setTimeout(() => {
this.setData(
{
keyword: e.detail.value,
},
() => this.clearAndGetList()
);
}, 500);
this.setData({
inputDebounceTimer: timer,
});
},
blur() {
let { keyword } = this.data;
this.setData({
keyword,
});
},
getOrderList() {
wx.p.wxApi.showLoading("加载中...");
let {
curTab: { requestType, requestMethod },
pageNum,
keyword,
} = this.data;
const userInfo = wx.p.storage.get("userInfo") || {};
let { merchantId, channelSource, phone } = userInfo;
//如果 merchantId, channelSource, phone 这三个参数有一个未获取到,给sentry上报异常,并且带上具体的值
if (!merchantId || !channelSource || !phone) {
// 设置自定义上下文信息
wx.p.sentry.reportParamError(
"订单列表异常:merchantId, channelSource, phone 参数异常",
userInfo
);
}
let obj = {
requestType,
merchantId,
phone,
pageSize: 99999,
currentPage: pageNum,
scoreSource: channelSource,
};
if (keyword) {
keyword = keyword.replace(/\ /g, "");
if (keyword.length) obj.goodsInfo = keyword;
}
let list = [];
wx.p.api.mineApi[requestMethod](obj)
.then((data) => {
data?.list?.forEach((e) => {
const { ___qty, ___price, ___totalPrice } = calcOrderProp(e);
list.push({
...e,
___price,
___totalPrice,
___qty,
});
});
this.setData({
list,
listFlag: true,
});
}).catch((err) => {
wx.p.sentry.reportError(`订单列表异常:${err.message}`, {...obj,err});
})
.finally(() => {
wx.hideLoading();
});
},
onPullDownRefresh: function () {
this.clearAndGetList();
},
});