引言:訂單列表中未支付的訂單判莉,需要設置一個過期時間有决,并顯示倒計時
具體步驟:
- 獲取未支付的訂單數(shù)據(jù)后腊脱,加載定時器
- 移除未支付已過期的訂單
- 退出頁面卸載定時器
- 后臺處理過期時間
小程序端
1狸剃、小程序中獲取當前時間:Date.parse(new Date())灭贷,但是注意枚抵,需要除以1000蹬耘,才能和php后端獲取的過期時間戳相減計算出剩余時間
2斥季、data中保存timer為定時器
3欺嗤、頁面渲染出來直接是遍歷過后的left_time
data: {
dzf_order_list: [],
dzf_curpage: 0,
timer: '',
},
/**
* 未支付訂單倒計時
*/
countDown:function(){
var that = this;
that.data.timer = setInterval(function () {
var orders = that.data.dzf_order_list;
console.log(orders)
for (var i = 0; i < orders.length; i++) {
var status = orders[i].order_state;
if (status == 1) {
//過期時間
var stop_time = orders[i].end_time;
//當前時間
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//計算剩余時間差值
var leftTime = stop_time - timestamp;
if (leftTime > 0) {
var h = Math.floor(leftTime / 3600);
var m = Math.floor((leftTime / 60 % 60));
var s = Math.floor((leftTime % 60));
var left_time = h + " : " + m + " : " + s;
orders[i].left_time = left_time;
}else{
//移除超時未支付的訂單
orders.splice(i, 1);
}
}
}
that.setData({
dzf_order_list:orders
});
}, 1000);
},
<view class="p" wx:if="{{order.order_state==1}}">付款還剩
<text class="color_r">{{order.left_time}}</text>
</view>
后臺
if($list){
foreach ($list as $key => $val){
if($val['order_state'] == 1){
//過期時間
$order_time = time() - $val['order_time'];
$d = floor($order_time / (3600 * 24));
$end = $val['order_time'] + (3600 * 24);
$list[$key]['end_time'] = $end;
if($d > 1){
unset($list[$key]);
}
}
if($val['order_state'] == 8 || $val['order_state'] == 6){
unset($list[$key]);
}
}
}