Score.php 7.7 KB

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