这边不讲思路只是部分代码实现过程
首先进入秒杀详情
<l-countdown time-type="second" time="{{time}}" bind:linend="stop" />
<l-card type="primary"
l-img-class="right-card"
position="left"
image="{{details.detail.pic}}"
title="{{details.detail.name}}">
<view class="content">
<view>{{details.detail.pice}}</view>
<l-button type="error" disabled="{{disabled}}" bind:lintap="go" data-goods_id="{{details.goods_id}}">去抢</l-button>
</view>
</l-card>
渲染的值就自己改就好了
用了一些样式库 变量名和属性觉得不对就换成自己的见名思意就好
js
data: {
details:[],
time:0,
disabled:false
},
let id=options.id
requestReq.request(
'show',
{id}
).then(res=>{
this.setData({
details:res.data.data,
})
this.times()
})
按钮:(防抖动格式)网上一大堆封装引入就不写了
go:debounce(e=> {
console.log(e[0])
let goods_id=e[0].currentTarget.dataset.goods_id
requestReq.request('go',{goods_id}).then(res=>{
console.log(res);
let code = res.statusCode.toString()
if (!code.startsWith('2')){
wx.showToast({
title: '异常!',
icon:1
})
}
if(res.data.code == 200){
wx.redirectTo({
url: '/pages/order/order',
})
}else{
wx.showToast({
title: res.data.msg,
})
}
})
}),
按钮接口:
public function go(Request $request)
{
//获取token
$token=explode(' ',$request->header('authorization'))[1];
//进行查看
$user=Cache::get($token);
//商品id
$goods_id=$request->input('goods_id');
//对应商品库存队列
$redisKey = 'goods_list_'.$goods_id;
//对应商品抢购成功用户集合 {1,3,4}
$successUser='success_user'.$user['id'];
//进行判断当前用户是否在抢成功的队列里面
$res=Redis::sismember($successUser,$user['id']);
if ($res){
return response()->json(['code'=>203,'data'=>'','msg'=>'已经抢购过了']);
}
$count=Redis::lpop($redisKey);
if (!$count){
return response()->json(['code'=>202,'data'=>'','msg'=>'已经抢光了哦']);
}
$success = Redis::sadd($successUser, $user['id']);
if (!$success){
Redis::lpuch($redisKey,1);
return response()->json(['code'=>201,'data'=>'','msg'=>'已经抢购过了']);
}
return response()->json(['code' => 200, 'data' => '', 'msg' => '秒杀成功']);
}
倒计时:
times(e){
var starttime=this.data.details.start_time;
console.log(starttime);
var times = Date.parse(new Date())/1000;
console.log(times);
var timestamp=starttime-times
console.log(timestamp);
if(timestamp>0){
this.setData({
time:timestamp,
disabled:true
})
}
if(timestamp<=0){
this.setData({
disabled:false
})
}
},
时间结束触发事件:
stop(){
this.setData({
disabled:false
})
},
秒杀成功后的生成订单
public function order(Request $request)
{
//获取token
$token=explode(' ',$request->header('authorization'))[1];
//商品id
$user=Cache::get($token);
//商品id
$goods_id=$request->input('goods_id');
//对应商品抢购成功用户集合 {1,3,4}
$successUser='success_user'.$user['id'];
//进行判断当前用户是否在抢成功的队列里面
$res=Redis::sismember($successUser,$user['id']);
//如果你在这里面,就抢完了
if (!$res){
//如果抢购成功 返回状态码,进行下单
return response()->json(['code' => 2003, 'data' => '', 'msg' => '手慢了!']);
}
DB::beginTransaction();
try{
//减库存
//生成订单
DB::commit();
//下单成功,跳转支付页面
return response()->json(['code' => 200, 'data' => '', 'msg' => '下单成功!']);
}catch (\Exception $e){
DB::rollBack();
}
}
后台接口
库存预热:
在后台添加秒杀时的库存预热
public function createList(Request $request)
{
//查出所有参与秒杀活动列表
$data=Details::with(['detail'])->where('goods_id',1)->first()->toArray();
//库存
$count = $data['num'];
//生成对应商品库存队列
$redisKey = 'goods_list_'.$data['goods_id'];
for ($i = 1; $i <= $count; $i++) {
Redis::rpush($redisKey, 1);
}
}
|