Score.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php namespace App\Http\Controllers\Api\Lottery;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Lottery\Score as Model;
  4. use App\Models\Custom;
  5. use App\Models\CustomCoupon;
  6. use App\Models\CustomScore;
  7. use App\Models\Lottery\ScoreRecord;
  8. use App\Models\Lottery\ScoreReward as ScoreReward;
  9. use App\Models\WeiBan\Tags as WeiBanTags;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 积分抽奖
  13. *
  14. * @author 刘相欣
  15. *
  16. * */
  17. class Score extends Api{
  18. /**
  19. * 获取抽奖配置 /api/lottery_score/get_detail
  20. *
  21. *
  22. * */
  23. public function get_detail(Model $Model,Custom $Custom,ScoreReward $ScoreReward,WeiBanTags $WeiBanTags){
  24. // 接口验签
  25. // $this->verify_sign();
  26. // 检查登录
  27. $uid = $this->checkLogin();
  28. // 获取客户信息
  29. $custom = $Custom->getOne($uid);
  30. // 如果存在的话
  31. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  32. // 客户的城市ID
  33. $cityId = empty($custom['city_id']) ? 0 : $custom['city_id'];
  34. // 获取客户城市的数据
  35. $data = $Model->getOneByCity($cityId);
  36. // 如果存在的话
  37. if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
  38. // 奖品
  39. $reward = $ScoreReward->getListByLottery($data['id']);
  40. // 活动暂无奖品
  41. if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
  42. // 通过活动ID,查询奖品
  43. $data['rewardList'] = $reward;
  44. // logo
  45. $data['logo'] = $data['logo'] ? path_compat($data['logo']) : '';
  46. // 判断用户是不是活动期间注册的以判断新老用户,获取对应积分
  47. $data['need_score'] = ($custom['insert_time'] >= $data['start_time'] && $custom['insert_time'] <= $data['end_time']) ? $data['need_score'] : $data['need_old_score'];
  48. // 默认可以参加活动
  49. $data['allow_join'] = 1;
  50. // 判断是不是可以参与
  51. if( $data['tag_scope'] ) {
  52. // 解析数组
  53. $data['tag_scope'] = explode(',',$data['tag_scope']);
  54. // 查询用户标签
  55. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  56. // 标签范围限定时,默认不能参与
  57. $data['allow_join'] = 0;
  58. // 判断标签是不是存在
  59. foreach ($tags as $value) {
  60. // 标签范围内,允许参加
  61. if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
  62. }
  63. }
  64. // 删除不必要的数据
  65. unset($data['need_old_score'],$data['tag_scope'],$data['city_ids']);
  66. // 返回结果
  67. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  68. }
  69. /**
  70. * 积分抽奖 /api/lottery_score/get_reward
  71. *
  72. * */
  73. public function get_reward(Model $Model,Custom $Custom,ScoreReward $ScoreReward,WeiBanTags $WeiBanTags,CustomScore $CustomScore,CustomCoupon $CustomCoupon,ScoreRecord $ScoreRecord){
  74. // 接口验签
  75. // $this->verify_sign();
  76. // 检查登录
  77. $uid = $this->checkLogin();
  78. // 获取客户信息
  79. $lotteryId = request('lottery_id',0);
  80. // 如果存在的话
  81. if( !$lotteryId ) return json_send(['code'=>'error','msg'=>'请选择参与的活动','data'=>['error'=>"抽奖活动ID有误"]]);
  82. // 获取客户信息
  83. $custom = $Custom->getOne($uid);
  84. // 如果存在的话
  85. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  86. // 通过活动ID,查询奖品
  87. $data = $Model->getOne($lotteryId);
  88. // 如果存在的话
  89. if( !$data ) return json_send(['code'=>'error','msg'=>'活动不存在或未开始','data'=>$data]);
  90. // 活动时间判断
  91. if( $data['start_time'] > time() ) return json_send(['code'=>'error','msg'=>'活动暂未开始','data'=>$data]);
  92. // 活动时间判断
  93. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动已结束','data'=>$data]);
  94. // 奖品
  95. $reward = $ScoreReward->getListByLottery($data['id']);
  96. // 活动暂无奖品
  97. if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
  98. // 判断用户是不是活动期间注册的以判断新老用户,获取对应积分
  99. $data['need_score'] = ($custom['insert_time'] >= $data['start_time'] && $custom['insert_time'] <= $data['end_time']) ? $data['need_score'] : $data['need_old_score'];
  100. // 默认可以参加活动
  101. $data['allow_join'] = 1;
  102. // 判断是不是可以参与
  103. if( $data['tag_scope'] ) {
  104. // 解析数组
  105. $data['tag_scope'] = explode(',',$data['tag_scope']);
  106. // 查询用户标签
  107. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  108. // 标签范围限定时,默认不能参与
  109. $data['allow_join'] = 0;
  110. // 判断标签是不是存在
  111. foreach ($tags as $value) {
  112. // 标签范围内,允许参加
  113. if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
  114. }
  115. }
  116. // 如果不能参与
  117. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'不符合参与条件','data'=>['error'=>'不符合参与条件']]);
  118. // 组合数据,写入订单表,子表
  119. DB::beginTransaction();
  120. try{
  121. // 扣减积分
  122. $result = $CustomScore->trade($uid,$lotteryId,($data['need_score']*-1),7,1);
  123. // 如果积分扣减失败
  124. if( isset($result['error']) ) {
  125. // 回退数据
  126. DB::rollBack();
  127. return json_send(['code'=>'error','msg'=>$result['error'],'data'=>['error'=>$result['error']]]);
  128. }
  129. // 获取奖励结果
  130. $rewardIndex = $ScoreReward->getRewardResult($reward);
  131. // 如果中奖,下标不是0
  132. if( $rewardIndex ) {
  133. // 获取奖品
  134. $rewardResult = $reward[$rewardIndex];
  135. // 奖品记录ID
  136. if( !empty($rewardResult['id']) ){
  137. // 记录,默认状态为1,进行中
  138. $record = ['custom_uid'=>$uid,'lottery_id'=>$lotteryId,'reward_id'=>$rewardResult['id'],'reward_name'=>$rewardResult['reward_name'],'status'=>1];
  139. // 如果是积分
  140. if( $rewardResult['reward_type'] == 1 ){
  141. // 积分大于0
  142. if( $rewardResult['reward_info'] > 0 ){
  143. // 积分发放
  144. $result = $CustomScore->trade($uid,$lotteryId,$rewardResult['reward_info'],7,2);
  145. // 发放失败,改为未中奖
  146. if( isset($result['error']) ) $rewardIndex = 0;
  147. // 发放成功,状态为已完成
  148. $record['status']= 8;
  149. }
  150. }
  151. // 优惠券,先进行发放
  152. if( $rewardResult['reward_type'] == 2 ){
  153. // 优惠券存在ID
  154. if( $rewardResult['reward_info'] > 0 ){
  155. // 积分给与
  156. $result = $CustomCoupon->giveCoupon($rewardResult['reward_info'],$uid);
  157. // 发放失败,改为未中奖
  158. if( !$result ) $rewardIndex = 0;
  159. // 发放成功,状态为已完成
  160. $record['status']= 8;
  161. }
  162. }
  163. // 如果是实物,要求填写地址,状态设置为0
  164. if( $rewardResult['reward_type'] == 5 ) $record['status'] = 0;
  165. // 中奖才进行记录
  166. if( $rewardIndex ) {
  167. // 奖品数量减少
  168. $ScoreReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1')]);
  169. // 扣减数量
  170. $ScoreRecord->add($record);
  171. }
  172. }
  173. }
  174. // 提交事务
  175. DB::commit();
  176. // 返回结果
  177. return json_send(['code'=>'success','msg'=>'抽奖成功','data'=>['rewardList'=>$reward,'rewardIndex'=>$rewardIndex]]);
  178. // 异常处理
  179. } catch (\Throwable $th) {
  180. // 回退数据
  181. DB::rollBack();
  182. // 下单失败提示
  183. return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  184. }
  185. }
  186. }