前端秒杀+倒计时

1.通过商品id和用户id进行查询
2.rabbitmq进行中间件连接
3.webSocket进行异步通知

new Vue({
		el:"#app",
		data:{
			coupon:{},
			Ntime:null,
			dis:false,
			btn:''
		},
		methods:{
			pay:function (id) {
				let user = JSON.parse(sessionStorage.getItem("user"));
				let uid = user.id;
				axios.get("/api/order/buyCoupon/"+id+"/"+uid).then((res)=>{
					if (res.data.success) {
						//和订单服务连接
						webSocket=new WebSocket("ws://localhost:8080/msg/"+uid);
						alert(res.data.message);
						webSocket.onmessage = function (event) {
							var res = JSON.parse(event.data);
							if (res.success) {
								location.href = "../success.html";
							}else {
								alert(event.data.message);
							}
							webSocket.close();
						}

					}else {
						alert(res.data.message);
						location.href = "cpayfail.html";
					}
				})
			},
			search:function () {
				var url = window.location.search;//获取的是浏览器路径?后边的值
				var id = url.split("=")[1];//或得id等于的值
				axios.get("/api/coupon/getCouponList/"+id).then((res)=>{
					this.coupon = res.data.result;
				})
			},
			Stime:function () {
				let nowTime = new Date().getTime();//获得当前时间
				let startTime = new Date(this.coupon.stime).getTime();//或得开始时间
				let endTime = new Date(this.coupon.etime).getTime();//获得结束时间
				if (nowTime<=startTime) {
					this.btn = '准备秒杀,距离活动开始:'+this.diffTime(nowTime-startTime);
					this.dis = true;
				}else if ((nowTime>startTime) && (nowTime<endTime)) {
					this.btn = '秒杀中,距离活动结束:'+this.diffTime(endTime-nowTime);
					this.dis = false;
				}else {
					this.btn = '活动已经结束'
					this.dis = true;
				}
			},
			diffTime:function (diff) {//时间差
				// 计算相差天数
				var days = Math.floor(diff / (24 * 3600 * 1000));
				// 计算天数后剩余的毫秒数
				var leave1 = diff % (24 * 3600 * 1000);
				// 计算出小时数
				var hours = Math.floor(leave1 / (3600 * 1000));
				// 计算小时数后剩余的毫秒数
				var leave2 = leave1 % (3600 * 1000);
				// 计算相差分钟数
				var minutes = Math.floor(leave2 / (60 * 1000));
				// 计算相差秒数
				var leave3 = leave2 % (60 * 1000);
				var seconds = Math.floor(leave3 / 1000);

				var returnStr = seconds + '秒';
				if (minutes>0) {
					var returnStr = minutes + '分' + returnStr;
				}
				if (hours>0) {
					var returnStr = hours + '小时' + returnStr;
				}
				if (days>0) {
					var returnStr = days + '天' + returnStr;
				}
				return returnStr;
			}
		},
		filters:{
			typFit:function (val) {
				if (val==0) {
					return "满减";
				}
				if (val==1) {
					return "打折";
				}
			}
		},
		mounted() {
			this.search();
			setInterval(this.Stime,1000);
		}
	})
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值