123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Requests\Api\Redpacket as Request;
- use App\Models\CustomAmount;
- use App\Models\CustomAmountRecord;
- use App\Models\CustomRedpacket;
- use App\Models\RedpacketActive as Model;
- use Illuminate\Support\Facades\DB;
- /**
- * 客户红包接口
- *
- * @author jun
- *
- * */
- class Redpacket extends Api{
- /**
- * 获取红包详情 /api/redpacket/get_info
- *
- *
- * */
- public function get_info(CustomRedpacket $CustomRedpacket){
- // 检查登录
- $uid = $this->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'=>'领取成功']);
- }
- }
|