Score.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. $rewardResult = $ScoreReward->getRewardResult($lotteryId);
  118. // 未抽中
  119. if( !$rewardResult ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>['error'=>'活动暂未配置奖品']]);
  120. // 奖品索引
  121. $rewardIndex = $rewardResult['index'];
  122. // 奖品列表
  123. $reward = $rewardResult['reward_list'];
  124. // 获取奖品信息
  125. $rewardResult = $reward[$rewardIndex];
  126. // 是否记录结果
  127. $record = ['custom_uid'=>$uid,'lottery_id'=>$lotteryId,'reward_id'=>$rewardResult['id'],'reward_name'=>$rewardResult['reward_name'],'status'=>(in_array($rewardResult['reward_type'],[4,5,6]) ? 1 : 8)];
  128. // 实物状态需要填写地址
  129. if( $rewardResult['reward_type'] == 5 ) $record['status'] = 0;
  130. // 组合数据,写入订单表,子表
  131. DB::beginTransaction();
  132. try{
  133. // 扣减积分
  134. $result = $CustomScore->trade($uid,$lotteryId,($data['need_score']*-1),7,1);
  135. // 如果积分扣减失败
  136. if( isset($result['error']) ){
  137. // 回退数据
  138. DB::rollBack();
  139. return json_send(['code'=>'error','msg'=>$result['error'],'data'=>['error'=>$result['error']]]);
  140. }
  141. // 如果存在奖品
  142. if( $rewardResult['id'] ){
  143. // 奖品数量减少
  144. $result = $ScoreReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1'),'lottery_id'=>$rewardResult['lottery_id']]);
  145. // 如果奖品数量减少失败
  146. if( !$result ) {
  147. // 回退数据
  148. DB::rollBack();
  149. // 提醒重试
  150. return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>'奖品数量扣减失败']]);
  151. }
  152. }
  153. // 如果是积分
  154. if( $rewardResult['reward_type'] == 1 ){
  155. // 积分大于0
  156. if( $rewardResult['reward_info'] > 0 ){
  157. // 积分发放
  158. $result = $CustomScore->trade($uid,$lotteryId,$rewardResult['reward_info'],7,2);
  159. // 发放失败,改为未中奖
  160. if( isset($result['error']) ) {
  161. // 回退数据
  162. DB::rollBack();
  163. // 通知重试
  164. return json_send(['code'=>'error','msg'=>'出了点小差,请重新抽奖','data'=>$result]);
  165. }
  166. }
  167. }
  168. // 优惠券,先进行发放
  169. if( $rewardResult['reward_type'] == 2 ){
  170. // 优惠券存在ID
  171. if( $rewardResult['reward_info'] > 0 ){
  172. // 积分给与
  173. $result = $CustomCoupon->giveCoupon($rewardResult['reward_info'],$uid);
  174. // 发放失败,改为未中奖
  175. if( !$result ) {
  176. // 回退数据
  177. DB::rollBack();
  178. // 通知重试
  179. return json_send(['code'=>'error','msg'=>'出了点小差,请重新抽奖','data'=>$result]);
  180. }
  181. }
  182. }
  183. // 红包
  184. if( $rewardResult['reward_type'] == 3 ){
  185. // 积分大于0
  186. if( $rewardResult['reward_info'] > 0 ){
  187. // 积分发放
  188. $result = $CustomAmount->trade($uid,$lotteryId,$rewardResult['reward_info'],5,1,'积分抽奖');
  189. // 发放失败,改为未中奖
  190. if( isset($result['error']) ) {
  191. // 回退数据
  192. DB::rollBack();
  193. // 通知重试
  194. return json_send(['code'=>'error','msg'=>'出了点小差,请重新抽奖','data'=>$result]);
  195. }
  196. }
  197. }
  198. // 扣减数量
  199. $result = $ScoreRecord->add($record);
  200. // 记录失败
  201. if( !$result ) {
  202. // 回退数据
  203. DB::rollBack();
  204. // 下单失败提示
  205. return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>'奖品记录失败']]);
  206. }
  207. // 提交事务
  208. DB::commit();
  209. // 通过活动ID,查询奖品
  210. $rewardList = [];
  211. // 奖品数据
  212. foreach ($reward as $value) {
  213. $rewardList[] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
  214. }
  215. // 获取积分信息
  216. $customScore = $CustomScore->getCustomScore($uid);
  217. // 返回结果
  218. return json_send(['code'=>'success','msg'=>'抽奖成功','data'=>['reward_list'=>$rewardList,'reward_index'=>$rewardIndex,'custom_score'=>$customScore]]);
  219. // 异常处理
  220. } catch (\Throwable $th) {
  221. // 回退数据
  222. DB::rollBack();
  223. // 下单失败提示
  224. return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  225. }
  226. }
  227. }