order.js
6.52 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import wx from '../../../utils/wx';
import Yas from '../../../common/yas';
import orderHandle from './order-handle';
import commonModel from '../../../models/common';
import orderModel from '../../../models/home/order';
import formatImage from '../../../utils/formatImage';
// 获取应用实例
let app = getApp();
let yas;
let {windowHeight} = app.getSystemInfo();
let timer;
Page({
data: {
windowHeight,
orderType: 1,
ordersList: [],
reason: [],
orderCode: ''
},
onLoad: function(options) {
let type = options.type || 1;
this.setData({
orderType: type
});
if (!app.getUid()) {
wx.switchTab({
url: '/pages/home/home',
});
}
yas = new Yas(app);
yas.pageOpenReport();
},
onShow: function() {
this.getOrders(this.data.orderType);
},
startTimer: function() {
let that = this;
timer = setInterval(function() {
let orders = that.data.ordersList;
for (let i = 0; i < orders.length; i++) {
let item = orders[i];
let pay_lefttime = parseInt(item.pay_lefttime);
if (item.counter_flag === 'Y' && item.is_cancel !== 'Y' && pay_lefttime > 0) {
item.pay_lefttime = pay_lefttime - 1;
item.time_left = that.formatDate(pay_lefttime - 1);
if (item.pay_lefttime === 0) {
that.getOrders(this.data.orderType);
}
} else {
item.pay_lefttime = 0;
}
}
that.setData({
ordersList: orders,
});
}, 1000);
},
stopTimer: function() {
clearInterval(timer);
},
add0: function(m) {
return m < 10 ? '0' + m : m;
},
formatDate: function(second) {
var dateStr = '';
var hr = Math.floor(second / 3600);
var min = Math.floor((second - hr * 3600) / 60);
var sec = (second - hr * 3600 - min * 60);
dateStr = this.add0(hr) + ':' + this.add0(min) + ':' + this.add0(sec);
return dateStr;
},
formatImgUrl: function(res) {
res.data.order_list && res.data.order_list.map((item) => {
item.order_goods && item.order_goods.map(good => {
good.goods_image = formatImage.image(good.goods_image, 100, 134);
});
});
},
formatOrders: function(orders) {
let _orders = [];
let needTimer = false;
orders && orders.map((item)=>{
item.pay_lefttime = parseInt(item.pay_lefttime);
item.time_left = this.formatDate(parseInt(item.pay_lefttime));
if (item.counter_flag === 'Y' && item.is_cancel !== 'Y' && parseInt(item.pay_lefttime) > 0) {
needTimer = true;
}
for (let i = 0; i < item.order_goods.length; i++) {
if (Number(item.order_goods[i].real_pay_price).toFixed(2) === Number(item.order_goods[i].sales_price).toFixed(2) || !item.order_goods[i].real_pay_price) { // eslint-disable-line
item.order_goods[i].real_pay_price = item.order_goods[i].goods_price ? item.order_goods[i].goods_price : item.order_goods[i].sales_price; // eslint-disable-line
item.order_goods[i].sales_price = '';
}
if (item.order_goods[i].sales_price) {
item.order_goods[i].sales_price = Number(item.order_goods[i].sales_price).toFixed(2);
}
}
_orders.push(item);
});
if (needTimer) {
this.stopTimer();
this.startTimer();
} else {
this.stopTimer();
}
return _orders;
},
getOrders: function(type) {
let params = {
type,
uid: app.getUid(),
page: 1,
limit: 50
};
wx.showLoading({title: '加载中'});
orderModel.getOrders(params)
.then(res => {
if (res.code === 200) {
this.formatImgUrl(res);
let orders = res.data.order_list;
orders = this.formatOrders(orders);
this.setData({
ordersList: orders,
showEmptyContainer: res.data.order_list.length === 0
});
wx.hideLoading();
}
}).catch(() => {
wx.hideLoading();
});
},
loadRefundReason() {
if (this.hasSetReason) {
return;
}
orderHandle.getRefundReason(res => {
this.setData({reason: res});
this.hasSetReason = true;
});
},
navToOrderDetail(e) {
let code = e.currentTarget.dataset.order_code;
global.router.go('orderDetail', {orderCode: code});
},
delOrder: function(e) {
const code = e.target.dataset.code;
orderHandle.deleteOrder(code, res => {
if (res && res.code === 200) {
this.getOrders(this.data.orderType);
}
});
},
confirmReceive: function(e) {
let code = e.currentTarget.dataset.code;
orderHandle.confirmReceive(code, res => {
if (res && res.code === 200) {
this.getOrders(this.data.orderType);
}
});
},
cancelOrder: function(e) {
let code = e.currentTarget.dataset.code;
orderHandle.cancelOrder(code, res => {
if (res && res.code === 200) {
this.getOrders(this.data.orderType);
}
});
},
expressDetail: function(e) {
let code = e.currentTarget.dataset.code;
orderHandle.expressDetail(code);
},
payNow: function(e) {
let code = e.currentTarget.dataset.code;
let amount = e.currentTarget.dataset.amount;
orderHandle.payNow({ order_code: code, order_amount: amount});
},
goShopping: function() {
wx.switchTab({url: '/pages/index/index'});
},
changeType: function(e) {
const { type } = e.currentTarget.dataset;
this.setData({
orderType: type
});
this.getOrders(type);
},
refundOrder(e) {
this.refundOrderCode = e.currentTarget.dataset.code;
wx.showModal({
content: '申请退款后,本单享有的优惠可能会一并取消,确定申请吗?',
cancelText: '取消',
confirmText: '确定',
confirmColor: '#FF0000'
}).then(res => {
if (res.confirm) {
this.loadRefundReason();
this.setData({showReason: true});
}
});
},
hidePicker(e) {
if (e.target.id === 'reason-picker') {
this.setData({showReason: false});
}
},
reasonChange(e) {
this.reasonIndex = e.detail.value[0];
},
reasonSure() {
orderHandle.refundNow(this.refundOrderCode,
this.data.reason[this.reasonIndex || 0], res => {
if (res && res.code === 200) {
this.getOrders(this.data.orderType);
}
});
this.setData({showReason: false});
},
submitFormId: function(e) {
// 上报formID
commonModel.addWechatFormId({
order_code: e.detail.value.orderCode,
openId: app.getOpenID(),
miniapp_type: app.getMiniappType(),
formId: e.detail.formId
});
}
});