Redpacket.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Requests\Api\Redpacket as Request;
  3. use App\Models\CustomAmount;
  4. use App\Models\CustomAmountRecord;
  5. use App\Models\CustomRedpacket;
  6. use App\Models\RedpacketActive as Model;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * 客户红包接口
  10. *
  11. * @author jun
  12. *
  13. * */
  14. class Redpacket extends Api{
  15. /**
  16. * 获取红包活动 /api/redpacket/get_list
  17. *
  18. *
  19. * */
  20. public function get_list(CustomRedpacket $CustomRedpacket){
  21. // 检查登录
  22. $uid = $this->checkLogin();
  23. // 接收参数
  24. $limit = request('limit',10);
  25. $time = time();
  26. $where = [
  27. ['custom_uid','=',$uid],
  28. ['start_time','<=',$time],
  29. ['end_time','>=',$time],
  30. ];
  31. // 查询活动
  32. $Paginator = $CustomRedpacket::query()->where($where)->paginate($limit);
  33. // 获取数据
  34. $data['total'] = $Paginator->total();
  35. $data['current_page'] = $Paginator->currentPage();
  36. $data['per_page'] = $Paginator->perPage();
  37. $data['last_page'] = $Paginator->lastPage();
  38. $list = $Paginator->items();
  39. // 判断活动
  40. if( !$list ) return json_send(['code'=>'error','msg'=>'您没有红包可领取']);
  41. $data = [];
  42. foreach ($list as $key=>$v){
  43. $data['data'][$key]['money'] = $v['money'];
  44. $data['data'][$key]['custom_redpacket_id'] = $v['id'];
  45. $data['data'][$key]['status'] = $v['status'];
  46. switch ($v['status']){
  47. case 0:
  48. $data['data'][$key]['state'] = '待领取';
  49. break;
  50. case 1:
  51. $data['data'][$key]['state'] = '已领取';
  52. break;
  53. }
  54. }
  55. // 返回成功
  56. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  57. }
  58. /**
  59. * 领取红包 /api/redpacket/get_redpacket
  60. *
  61. *
  62. * */
  63. public function get_redpacket(Request $request,Model $Model,CustomRedpacket $CustomRedpacket,CustomAmountRecord $CustomAmountRecord,CustomAmount $CustomAmount){
  64. // 验证参数
  65. $request->scene('get_redpacket')->validate();
  66. // 检查登录
  67. $uid = $this->checkLogin();
  68. // 接收参数
  69. $customRedpacketId = request('custom_redpacket_id',0);
  70. // 查询红包信息
  71. $info = $CustomRedpacket::query()->where(['id'=>$customRedpacketId,'custom_uid'=>$uid])->first();
  72. // 判断是否可以领取
  73. if( !$info ) return json_send(['code'=>'error','msg'=>'红包不存在']);
  74. // 判断是否可以领取
  75. if( $info['status'] ) return json_send(['code'=>'error','msg'=>'红包已领取']);
  76. // 时间
  77. if( $info['start_time'] > time() ) return json_send(['code'=>'error','msg'=>'还没到发放时间哦']);
  78. // 时间
  79. if( $info['end_time'] <= time() ) return json_send(['code'=>'error','msg'=>'发放时间已经结束了']);
  80. DB::beginTransaction();
  81. try {
  82. // 获取余额信息
  83. $amountInfo = $CustomAmount::query()->where(['custom_uid'=>$uid])->first();
  84. if(!$amountInfo){
  85. $result = $CustomAmountRecord::query()->insert(['custom_uid'=>$uid,'insert_time'=>time(),'update_time'=>time()]);
  86. if (!$result){
  87. return json_send(['code'=>'error','msg'=>'提现失败','data'=>['error'=>'提现失败']]);
  88. }
  89. $balance = 0;
  90. }else{
  91. $balance = $amountInfo['amount'];
  92. }
  93. //加入用户余额
  94. $result = $CustomAmount::query()->where(['custom_uid'=>$uid])->increment('amount',$info['money']);
  95. if (!$result) return json_send(['code'=>'error','msg'=>'领取红包失败']);
  96. //写入余额记录
  97. $recordInfo = [
  98. 'transfer_bill_no' => $info['id'],
  99. 'prefix' => 1,
  100. 'amount' => $info['money'],
  101. 'buy_type' => 1,
  102. 'pay_type' => 1,
  103. 'balance' => $balance,
  104. 'insert_time' => time(),
  105. 'update_time' => time(),
  106. ];
  107. $resultId = $CustomAmountRecord::query()->insertGetId($recordInfo);
  108. if (!$resultId){
  109. DB::rollBack();
  110. return json_send(['code'=>'error','msg'=>'领取红包失败']);
  111. }
  112. //修改用户红包记录状态
  113. $result = $CustomRedpacket::query()->where(['id'=>$info['id']])->update(['status'=>1,'update_time'=>time(),'amount_record_id'=>$resultId]);
  114. if (!$result){
  115. DB::rollBack();
  116. return json_send(['code'=>'error','msg'=>'领取红包失败']);
  117. }
  118. DB::commit();
  119. }catch (\Exception $e){
  120. // 回退数据
  121. DB::rollBack();
  122. return json_send(['code'=>'error','msg'=>'领取红包失败','data'=>$e->getMessage()]);
  123. }
  124. // 返回成功
  125. return json_send(['code'=>'success','msg'=>'领取成功']);
  126. }
  127. }