checkLogin(); // 接收参数 $customRedpacketId = request('custom_redpacket_id',0); $time = time(); $where = [ ['custom_redpacket.id','=',$customRedpacketId], ['custom_redpacket.custom_uid','=',$uid], ['redpacket.start_time','<=',$time], ['redpacket.end_time','>=',$time], ['redpacket.status','=',0], ]; // 查询活动 $list = $CustomRedpacket::query() ->join('redpacket','redpacket.id','=','custom_redpacket.redpacket_id') ->where($where) ->first(['custom_redpacket.*','redpacket.name as redpacket_name','redpacket.active_rule as active_rule']); // 判断活动 if( !$list ) return json_send(['code'=>'error','msg'=>'红包已失效']); if ($list['status'] == 1) return json_send(['code'=>'error','msg'=>'红包已领取']); $data['money'] = $list['money']; $data['active_rule'] = $list['active_rule']; $data['custom_redpacket_id'] = $list['id']; $data['status'] = $list['status']; // 返回成功 return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]); } /** * 获取红包活动 /api/redpacket/get_list * * * */ public function get_list(CustomRedpacket $CustomRedpacket){ // 检查登录 $uid = $this->checkLogin(); // 接收参数 $limit = request('limit',10); $time = time(); $where = [ ['custom_redpacket.custom_uid','=',$uid], ['redpacket.start_time','<=',$time], ['redpacket.end_time','>=',$time], ['redpacket.status','=',0], ]; // 查询活动 $Paginator = $CustomRedpacket::query() ->join('redpacket','redpacket.id','=','custom_redpacket.redpacket_id') ->where($where) ->paginate($limit,['custom_redpacket.id as custom_redpacket_id', 'custom_redpacket.money', 'custom_redpacket.status', 'redpacket.active_rule', 'redpacket.name as active_name', 'redpacket.start_time', 'redpacket.end_time', ]); // 获取数据 $data['total'] = $Paginator->total(); $data['current_page'] = $Paginator->currentPage(); $data['per_page'] = $Paginator->perPage(); $data['last_page'] = $Paginator->lastPage(); $data['data'] = $Paginator->items(); // 判断活动 if( !$data['data'] ) return json_send(['code'=>'error','msg'=>'您没有红包可领取']); foreach ($data['data'] as $key=>$v){ switch ($v['status']){ case 0: $data['data'][$key]['state'] = '待领取'; break; case 1: $data['data'][$key]['state'] = '已领取'; break; } } // 返回成功 return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]); } /** * 领取红包 /api/redpacket/get_redpacket * * * */ public function get_redpacket(Request $request,Model $Model,CustomRedpacket $CustomRedpacket,CustomAmountRecord $CustomAmountRecord,CustomAmount $CustomAmount){ // 验证参数 $request->scene('get_redpacket')->validate(); // 检查登录 $uid = $this->checkLogin(); // 接收参数 $customRedpacketId = request('custom_redpacket_id',0); // 查询红包信息 $time = time(); $where = [ ['custom_redpacket.custom_uid','=',$uid], ['custom_redpacket.id','=',$customRedpacketId], ['redpacket.start_time','<=',$time], ['redpacket.end_time','>=',$time], ['redpacket.status','=',0], ]; // 查询红包 $info = $CustomRedpacket::query() ->join('redpacket','redpacket.id','=','custom_redpacket.redpacket_id') ->where($where) ->first(['custom_redpacket.*']); // 判断是否可以领取 if( !$info ) return json_send(['code'=>'error','msg'=>'红包不存在']); // 判断是否可以领取 if( $info['status'] ) return json_send(['code'=>'error','msg'=>'红包已领取']); // 时间 DB::beginTransaction(); try { // 获取余额信息 $amountInfo = $CustomAmount::query()->where(['custom_uid'=>$uid])->first(); if(!$amountInfo){ $result = $CustomAmount::query()->insert(['custom_uid'=>$uid,'insert_time'=>time(),'update_time'=>time()]); if (!$result){ return json_send(['code'=>'error','msg'=>'提现失败','data'=>['error'=>'获取余额信息失败']]); } $balance = 0; }else{ $balance = $amountInfo['amount']; } //加入用户余额 $result = $CustomAmount::query()->where(['custom_uid'=>$uid])->increment('amount',$info['money']); if (!$result) return json_send(['code'=>'error','msg'=>'领取红包失败']); //写入余额记录 $recordInfo = [ 'transfer_bill_no' => $info['id'], 'prefix' => 1, 'amount' => $info['money'], 'custom_uid' => $uid, 'buy_type' => 1, 'pay_type' => 1, 'balance' => $balance, 'insert_time' => time(), 'update_time' => time(), ]; $resultId = $CustomAmountRecord::query()->insertGetId($recordInfo); if (!$resultId){ DB::rollBack(); return json_send(['code'=>'error','msg'=>'领取红包失败']); } //修改用户红包记录状态 $result = $CustomRedpacket::query()->where(['id'=>$info['id']]) ->update(['status'=>1,'update_time'=>time(),'amount_record_id'=>$resultId,'get_time'=>$time]); if (!$result){ DB::rollBack(); return json_send(['code'=>'error','msg'=>'领取红包失败']); } DB::commit(); }catch (\Exception $e){ // 回退数据 DB::rollBack(); return json_send(['code'=>'error','msg'=>'领取红包失败','data'=>$e->getMessage()]); } // 返回成功 return json_send(['code'=>'success','msg'=>'领取成功']); } }