基于element-ui实现待支付订单显示定时器

基于element-ui实现待支付订单显示定时器

表格显示内容:

<el-table-column label="订单状态" prop="state" align="center">
    <template slot-scope="scope">
         {{ _orderState[parseInt(scope.row.state)] }}
         <br>
         <span style="color: #f10215" v-if="scope.row.state===1 && scope.row.ext.isPreOrderPaid !== true">{{timeObj[scope.row.orderNo]}}</span>
     </template>
</el-table-column>

data中定义:

timeObj:{},

1、init()函数获取表格数据(dataList)后:

if(this.dataList){
    let that = this;
    this.dataList.forEach(elem =>{
        // 判断是否是待支付订单
        if(elem.state === 1 && elem.ext.isPreOrderPaid !== true){
            // 传递创建时间和订单编号参数
            that.deadLine(elem.createTime,elem.orderNo);
        }
    })
}

2、周期性执行状态函数

deadLine(createTime,id){
    let that = this;
    let interval = setInterval(() =>{
        that.countTime(createTime,id,interval)
    },1000)

},

3、过期的数据改变状态为’已过期’

async countTime(createTime,id,interval) {
    let startTime = this.$formatIntValue(createTime);
    let nowTime = Date.parse(new Date()) / 1000;   // 当前时间时间戳 精确到秒
    let endTime = startTime + 30 * 60;   // 结束时间戳
    let downTime = (endTime - nowTime) > 0 ? (endTime - nowTime) : 0;
    let that = this;
    if (downTime <= 0) {
        clearInterval(interval);
        that.timeObj = {}
        let params = {
            orderNo: id
        };
        // 调用接口改变订单状态(此处可以和后端协商:后端也用定时器到期后改变订单状态,这样就无需调用接口改变状态,也无需调用接口刷新表格数据,前端遍历表格数据将该订单状态改为过期即可)
        let res = await this.$api('Order.CancelOrder', params);
        // 刷新表格数据
        if(res) {that.init()};
        //超时 事件处理
    } else {
        downTime--;
        let min = Math.floor(downTime % 3600)
        let val = that.$formatBit(Math.floor(downTime / 3600)) + ':' + 	that.$formatBit(Math.floor(min / 60)) + ':' + that.$formatBit(downTime % 60);
        //响应式修改数据,确保数据改变时视图同步更新
        this.$nextTick( async ()=>{
            this.$set(that.timeObj,id,val)
        })
    }
},

说明:
$formatIntValue是自己封装的函数,用于将数据转为整型
$formatBit用于将1位数补0,如:1–>01

Vue.prototype.$formatBit = function (val) {
    val = +val
    return val > 9 ? val : '0' + val
}

注:原创代码,可能存在性能问题,有需要者可自行优化

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,可以按照如下步骤来实现一个简单的亚运会倒计时: 1. 安装 element-ui 和 moment.js 在终端中进入项目目录,输入以下命令安装 element-ui 和 moment.js: ``` npm install element-ui moment --save ``` 2. 在 main.js 中引入 element-ui 在 main.js 中添加以下代码: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建一个 Countdown.vue 组件 在 src/components 目录下创建 Countdown.vue 文件,并添加以下代码: ```vue <template> <div class="countdown"> <el-card> <h2>{{ title }}</h2> <div class="countdown-timer"> <div class="countdown-item"> <div class="countdown-value">{{ days }}</div> <div class="countdown-label">天</div> </div> <div class="countdown-item"> <div class="countdown-value">{{ hours }}</div> <div class="countdown-label">小时</div> </div> <div class="countdown-item"> <div class="countdown-value">{{ minutes }}</div> <div class="countdown-label">分</div> </div> <div class="countdown-item"> <div class="countdown-value">{{ seconds }}</div> <div class="countdown-label">秒</div> </div> </div> </el-card> </div> </template> <script> import moment from 'moment' export default { name: 'Countdown', props: { title: { type: String, default: '亚运会倒计时' }, endTime: { type: String, required: true } }, data() { return { intervalId: null, days: 0, hours: 0, minutes: 0, seconds: 0 } }, mounted() { // 获取倒计时时间 const endTime = moment(this.endTime) const now = moment() const duration = moment.duration(endTime.diff(now)) // 初始化倒计时数据 this.days = Math.floor(duration.asDays()) this.hours = duration.hours() this.minutes = duration.minutes() this.seconds = duration.seconds() // 启动定时器 this.intervalId = setInterval(() => { this.updateCountdown() }, 1000) }, methods: { updateCountdown() { // 获取倒计时时间 const endTime = moment(this.endTime) const now = moment() const duration = moment.duration(endTime.diff(now)) // 更新倒计时数据 this.days = Math.floor(duration.asDays()) this.hours = duration.hours() this.minutes = duration.minutes() this.seconds = duration.seconds() // 倒计时结束时清除定时器 if (duration <= 0) { clearInterval(this.intervalId) } } }, beforeUnmount() { clearInterval(this.intervalId) } } </script> <style scoped> .countdown { display: flex; justify-content: center; align-items: center; height: 100%; } .countdown-timer { display: flex; justify-content: space-between; } .countdown-item { display: flex; flex-direction: column; align-items: center; } .countdown-value { font-size: 3rem; font-weight: bold; } .countdown-label { font-size: 1.5rem; color: #999; } </style> ``` 4. 在 App.vue 中使用 Countdown 组件 在 App.vue 中添加以下代码: ```vue <template> <div id="app"> <Countdown endTime="2022-09-10 00:00:00" /> </div> </template> <script> import Countdown from './components/Countdown.vue' export default { name: 'App', components: { Countdown } } </script> ``` 在这里我们将倒计时时间设置为 2022 年 9 月 10 日的零点,你可以根据实际情况进行修改。 5. 运行项目并查看效果 在终端中输入以下命令运行项目: ``` npm run serve ``` 打开浏览器访问 http://localhost:8080/,即可看到亚运会倒计时。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值