Score.php 8.6 KB

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