Redpacket.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_info
  17. *
  18. *
  19. * */
  20. public function get_info(CustomRedpacket $CustomRedpacket){
  21. // 检查登录
  22. $uid = $this->checkLogin();
  23. // 接收参数
  24. $customRedpacketId = request('custom_redpacket_id',0);
  25. $time = time();
  26. $where = [
  27. ['custom_redpacket.id','=',$customRedpacketId],
  28. ['custom_redpacket.custom_uid','=',$uid],
  29. ['redpacket.start_time','<=',$time],
  30. ['redpacket.end_time','>=',$time],
  31. ['redpacket.status','=',0],
  32. ];
  33. // 查询活动
  34. $list = $CustomRedpacket::query()
  35. ->join('redpacket','redpacket.id','=','custom_redpacket.redpacket_id')
  36. ->where($where)
  37. ->first(['custom_redpacket.*','redpacket.name as redpacket_name','redpacket.active_rule as active_rule']);
  38. // 判断活动
  39. if( !$list ) return json_send(['code'=>'error','msg'=>'红包已失效']);
  40. if ($list['status'] == 1) return json_send(['code'=>'error','msg'=>'红包已领取']);
  41. $data['money'] = $list['money'];
  42. $data['active_rule'] = $list['active_rule'];
  43. $data['custom_redpacket_id'] = $list['id'];
  44. $data['status'] = $list['status'];
  45. // 返回成功
  46. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  47. }
  48. /**
  49. * 获取红包活动 /api/redpacket/get_list
  50. *
  51. *
  52. * */
  53. public function get_list(CustomRedpacket $CustomRedpacket){
  54. // 检查登录
  55. $uid = $this->checkLogin();
  56. // 接收参数
  57. $limit = request('limit',10);
  58. $time = time();
  59. $where = [
  60. ['custom_redpacket.custom_uid','=',$uid],
  61. ['redpacket.start_time','<=',$time],
  62. ['redpacket.end_time','>=',$time],
  63. ['redpacket.status','=',0],
  64. ];
  65. // 查询活动
  66. $Paginator = $CustomRedpacket::query()
  67. ->join('redpacket','redpacket.id','=','custom_redpacket.redpacket_id')
  68. ->where($where)
  69. ->paginate($limit,['custom_redpacket.id as custom_redpacket_id',
  70. 'custom_redpacket.money',
  71. 'custom_redpacket.status',
  72. 'redpacket.active_rule',
  73. 'redpacket.name as active_name',
  74. 'redpacket.start_time',
  75. 'redpacket.end_time',
  76. ]);
  77. // 获取数据
  78. $data['total'] = $Paginator->total();
  79. $data['current_page'] = $Paginator->currentPage();
  80. $data['per_page'] = $Paginator->perPage();
  81. $data['last_page'] = $Paginator->lastPage();
  82. $data['data'] = $Paginator->items();
  83. // 判断活动
  84. if( !$data['data'] ) return json_send(['code'=>'error','msg'=>'您没有红包可领取']);
  85. foreach ($data['data'] as $key=>$v){
  86. switch ($v['status']){
  87. case 0:
  88. $data['data'][$key]['state'] = '待领取';
  89. break;
  90. case 1:
  91. $data['data'][$key]['state'] = '已领取';
  92. break;
  93. }
  94. }
  95. // 返回成功
  96. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  97. }
  98. /**
  99. * 领取红包 /api/redpacket/get_redpacket
  100. *
  101. *
  102. * */
  103. public function get_redpacket(Request $request,Model $Model,CustomRedpacket $CustomRedpacket,CustomAmountRecord $CustomAmountRecord,CustomAmount $CustomAmount){
  104. // 验证参数
  105. $request->scene('get_redpacket')->validate();
  106. // 检查登录
  107. $uid = $this->checkLogin();
  108. // 接收参数
  109. $customRedpacketId = request('custom_redpacket_id',0);
  110. // 查询红包信息
  111. $time = time();
  112. $where = [
  113. ['custom_redpacket.custom_uid','=',$uid],
  114. ['custom_redpacket.id','=',$customRedpacketId],
  115. ['redpacket.start_time','<=',$time],
  116. ['redpacket.end_time','>=',$time],
  117. ['redpacket.status','=',0],
  118. ];
  119. // 查询红包
  120. $info = $CustomRedpacket::query()
  121. ->join('redpacket','redpacket.id','=','custom_redpacket.redpacket_id')
  122. ->where($where)
  123. ->first(['custom_redpacket.*']);
  124. // 判断是否可以领取
  125. if( !$info ) return json_send(['code'=>'error','msg'=>'红包不存在']);
  126. // 判断是否可以领取
  127. if( $info['status'] ) return json_send(['code'=>'error','msg'=>'红包已领取']);
  128. // 时间
  129. DB::beginTransaction();
  130. try {
  131. // 获取余额信息
  132. $amountInfo = $CustomAmount::query()->where(['custom_uid'=>$uid])->first();
  133. if(!$amountInfo){
  134. $result = $CustomAmount::query()->insert(['custom_uid'=>$uid,'insert_time'=>time(),'update_time'=>time()]);
  135. if (!$result){
  136. return json_send(['code'=>'error','msg'=>'提现失败','data'=>['error'=>'获取余额信息失败']]);
  137. }
  138. $balance = 0;
  139. }else{
  140. $balance = $amountInfo['amount'];
  141. }
  142. //加入用户余额
  143. $result = $CustomAmount::query()->where(['custom_uid'=>$uid])->increment('amount',$info['money']);
  144. if (!$result) return json_send(['code'=>'error','msg'=>'领取红包失败']);
  145. //写入余额记录
  146. $recordInfo = [
  147. 'transfer_bill_no' => $info['id'],
  148. 'prefix' => 1,
  149. 'amount' => $info['money'],
  150. 'custom_uid' => $uid,
  151. 'buy_type' => 1,
  152. 'pay_type' => 1,
  153. 'balance' => $balance,
  154. 'insert_time' => time(),
  155. 'update_time' => time(),
  156. ];
  157. $resultId = $CustomAmountRecord::query()->insertGetId($recordInfo);
  158. if (!$resultId){
  159. DB::rollBack();
  160. return json_send(['code'=>'error','msg'=>'领取红包失败']);
  161. }
  162. //修改用户红包记录状态
  163. $result = $CustomRedpacket::query()->where(['id'=>$info['id']])
  164. ->update(['status'=>1,'update_time'=>time(),'amount_record_id'=>$resultId,'get_time'=>$time]);
  165. if (!$result){
  166. DB::rollBack();
  167. return json_send(['code'=>'error','msg'=>'领取红包失败']);
  168. }
  169. DB::commit();
  170. }catch (\Exception $e){
  171. // 回退数据
  172. DB::rollBack();
  173. return json_send(['code'=>'error','msg'=>'领取红包失败','data'=>$e->getMessage()]);
  174. }
  175. // 返回成功
  176. return json_send(['code'=>'success','msg'=>'领取成功']);
  177. }
  178. }