Score.php 8.4 KB

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