|
@@ -0,0 +1,259 @@
|
|
|
+<?php namespace App\Http\Controllers\Api\Lottery;
|
|
|
+
|
|
|
+use App\Http\Controllers\Api\Api;
|
|
|
+use App\Models\Lottery\Recruitment as Model;
|
|
|
+use App\Models\Lottery\RecruitmentUsable;
|
|
|
+use App\Models\Custom;
|
|
|
+use App\Models\CustomCoupon;
|
|
|
+use App\Models\CustomScore;
|
|
|
+use App\Models\CustomAmount;
|
|
|
+use App\Models\Lottery\RecruitmentRecord;
|
|
|
+use App\Models\Lottery\RecruitmentReward as RecruitmentReward;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use App\Models\WeiBan\Tags as WeiBanTags;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 积分抽奖
|
|
|
+ *
|
|
|
+ * @author 刘相欣
|
|
|
+ *
|
|
|
+ * */
|
|
|
+class Recruitment extends Api{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取抽奖配置 /api/lottery_recruitment/get_detail
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function get_detail(Model $Model,Custom $Custom,RecruitmentReward $RecruitmentReward,RecruitmentUsable $RecruitmentUsable,WeiBanTags $WeiBanTags){
|
|
|
+ // 接口验签
|
|
|
+ // $this->verify_sign();
|
|
|
+ // 检查登录
|
|
|
+ $uid = $this->checkLogin();
|
|
|
+ // 获取客户信息
|
|
|
+ $custom = $Custom->getOne($uid);
|
|
|
+ // 如果存在的话
|
|
|
+ if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
|
|
|
+ // 接收参数
|
|
|
+ $id = request('id',0);
|
|
|
+ // 获取活动
|
|
|
+ $data = $Model->getOne($id);
|
|
|
+ // 如果存在的话
|
|
|
+ if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
|
|
|
+ // 默认可以参加活动
|
|
|
+ $data['allow_join'] = 1;
|
|
|
+ // 判断是不是可以参与
|
|
|
+ if( $data['tag_scope'] ) {
|
|
|
+ // 解析数组
|
|
|
+ $data['tag_scope'] = explode(',',$data['tag_scope']);
|
|
|
+ // 查询用户标签
|
|
|
+ $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
|
|
|
+ // 标签范围限定时,默认不能参与
|
|
|
+ $data['allow_join'] = 0;
|
|
|
+ // 判断标签是不是存在
|
|
|
+ foreach ($tags as $value) {
|
|
|
+ // 标签范围内,允许参加
|
|
|
+ if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
|
|
|
+ }
|
|
|
+ // 如果不能参与
|
|
|
+ if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
|
|
|
+ }
|
|
|
+ // 判断是不是可以参与
|
|
|
+ if( $data['city_ids'] ) {
|
|
|
+ // 解析数组
|
|
|
+ $data['city_ids'] = explode(',',$data['city_ids']);
|
|
|
+ // 如果不在城市范围
|
|
|
+ if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
|
|
|
+ // 如果不能参与
|
|
|
+ if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
|
|
|
+ }
|
|
|
+ // 奖品
|
|
|
+ $reward = $RecruitmentReward->getListByLottery($data['id']);
|
|
|
+ // 活动暂无奖品
|
|
|
+ if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
|
|
|
+ // logo
|
|
|
+ $data['logo'] = $data['logo'] ? path_compat($data['logo']) : '';
|
|
|
+ // 通过活动ID,查询奖品
|
|
|
+ $data['reward_list'] = [];
|
|
|
+ // 奖品数据
|
|
|
+ foreach ($reward as $value) {
|
|
|
+ // 奖项
|
|
|
+ $data['reward_list'][] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
|
|
|
+ }
|
|
|
+ // 查询用户可用抽奖次数
|
|
|
+ $number = $RecruitmentUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$id]])->value('number');
|
|
|
+ // 最少为0,避免显示异常
|
|
|
+ $data['number'] = $number < 0 ? 0 : $number;
|
|
|
+ // 时间处理
|
|
|
+ $data['start_date'] = date('Y/m/d H:i',$data['start_time']);
|
|
|
+ // 时间处理
|
|
|
+ $data['end_date'] = date('Y/m/d H:i',$data['end_time']);
|
|
|
+ // 返回结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 抽奖 /api/lottery_recruitment/get_reward
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function get_reward(Model $Model,Custom $Custom,RecruitmentRecord $RecruitmentRecord,RecruitmentReward $RecruitmentReward,CustomCoupon $CustomCoupon,CustomScore $CustomScore,CustomAmount $CustomAmount,WeiBanTags $WeiBanTags){
|
|
|
+ // 接口验签
|
|
|
+ // $this->verify_sign();
|
|
|
+ // 检查登录
|
|
|
+ $uid = $this->checkLogin();
|
|
|
+ // 获取活动
|
|
|
+ $lotteryId = request('lottery_id',0);
|
|
|
+ // 如果存在的话
|
|
|
+ if( !$lotteryId ) return json_send(['code'=>'error','msg'=>'请选择参与的活动','data'=>['error'=>"抽奖活动ID有误"]]);
|
|
|
+ // 获取客户信息
|
|
|
+ $custom = $Custom->getOne($uid);
|
|
|
+ // 如果存在的话
|
|
|
+ if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
|
|
|
+ // 通过活动ID,查询奖品
|
|
|
+ $data = $Model->getOne($lotteryId);
|
|
|
+ // 如果存在的话
|
|
|
+ if( !$data ) return json_send(['code'=>'error','msg'=>'活动不存在或未开始','data'=>$data]);
|
|
|
+ // 活动时间判断
|
|
|
+ if( $data['start_time'] > time() ) return json_send(['code'=>'error','msg'=>'活动暂未开始','data'=>$data]);
|
|
|
+ // 活动时间判断
|
|
|
+ if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动已结束','data'=>$data]);
|
|
|
+ // 默认可以参加活动
|
|
|
+ $data['allow_join'] = 1;
|
|
|
+ // 判断是不是可以参与
|
|
|
+ if( $data['tag_scope'] ) {
|
|
|
+ // 解析数组
|
|
|
+ $data['tag_scope'] = explode(',',$data['tag_scope']);
|
|
|
+ // 查询用户标签
|
|
|
+ $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
|
|
|
+ // 标签范围限定时,默认不能参与
|
|
|
+ $data['allow_join'] = 0;
|
|
|
+ // 判断标签是不是存在
|
|
|
+ foreach ($tags as $value) {
|
|
|
+ // 标签范围内,允许参加
|
|
|
+ if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
|
|
|
+ }
|
|
|
+ // 如果不能参与
|
|
|
+ if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'不符合参与条件','data'=>['error'=>'不符合参与条件']]);
|
|
|
+ }
|
|
|
+ // 判断是不是可以参与
|
|
|
+ if( $data['city_ids'] ) {
|
|
|
+ // 解析数组
|
|
|
+ $data['city_ids'] = explode(',',$data['city_ids']);
|
|
|
+ // 如果不在城市范围
|
|
|
+ if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
|
|
|
+ // 如果不能参与
|
|
|
+ if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
|
|
|
+ }
|
|
|
+ // 奖品
|
|
|
+ $reward = $RecruitmentReward->getListByLottery($data['id']);
|
|
|
+ // 活动暂无奖品
|
|
|
+ if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
|
|
|
+ // 查询用户已参与次数
|
|
|
+ $data['join_num'] = (int)$RecruitmentRecord->query()->where([['custom_uid','=',$uid],['lottery_id','=',$lotteryId]])->count();
|
|
|
+ // 计算剩余次数
|
|
|
+ $data['join_num'] = $data['lucky_num'] - $data['join_num'];
|
|
|
+ // 如果次数不够
|
|
|
+ if( $data['join_num'] <= 0 ) return json_send(['code'=>'error','msg'=>'抽奖次数已用完','data'=>['error'=>'抽奖次数已用完']]);
|
|
|
+ // 抽奖记录
|
|
|
+ $record = ['custom_uid'=>$uid,'lottery_id'=>$lotteryId,'reward_id'=>0,'reward_name'=>0,'status'=>1];
|
|
|
+ // 开启事务
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ // 查询条件
|
|
|
+ $map = [];
|
|
|
+ // 判断周期
|
|
|
+ if( !empty($data['freq']) ) {
|
|
|
+ if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]];
|
|
|
+ if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]];
|
|
|
+ if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]];
|
|
|
+ }
|
|
|
+ // 是否限制中奖次数字段,没有默认中奖一次
|
|
|
+ $data['max_reward'] = isset($data['max_reward']) ? 1 : $data['max_reward'];
|
|
|
+ // 限制中奖则获取中奖次数
|
|
|
+ $rewarTotal = $data['max_reward'] ? $RecruitmentRecord->query()->where([['lottery_id','=',$data['id']],['custom_uid','=',$uid],['reward_id','>',0]])->where($map)->count() : 0;
|
|
|
+ // 中奖上限以后不再中奖
|
|
|
+ $rewardIndex = ($data['max_reward'] && $rewarTotal >= $data['max_reward']) ? 0 : $RecruitmentReward->getRewardResult($reward);
|
|
|
+ // 如果中奖,下标不是0
|
|
|
+ if( $rewardIndex ) {
|
|
|
+ // 获取奖品
|
|
|
+ $rewardResult = $reward[$rewardIndex];
|
|
|
+ // 奖品记录ID
|
|
|
+ if( !empty($rewardResult['id']) ){
|
|
|
+ // 如果是积分
|
|
|
+ if( $rewardResult['reward_type'] == 1 ){
|
|
|
+ // 积分大于0
|
|
|
+ if( $rewardResult['reward_info'] > 0 ){
|
|
|
+ // 积分发放
|
|
|
+ $result = $CustomScore->trade($uid,$lotteryId,$rewardResult['reward_info'],7,5);
|
|
|
+ // 发放失败,改为未中奖
|
|
|
+ if( isset($result['error']) ) $rewardIndex = 0;
|
|
|
+ // 发放成功,状态为已完成
|
|
|
+ $record['status']= 8;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 优惠券,先进行发放
|
|
|
+ if( $rewardResult['reward_type'] == 2 ){
|
|
|
+ // 优惠券存在ID
|
|
|
+ if( $rewardResult['reward_info'] > 0 ){
|
|
|
+ // 积分给与
|
|
|
+ $result = $CustomCoupon->giveCoupon($rewardResult['reward_info'],$uid);
|
|
|
+ // 发放失败,改为未中奖
|
|
|
+ if( !$result ) $rewardIndex = 0;
|
|
|
+ // 发放成功,状态为已完成
|
|
|
+ $record['status']= 8;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 红包
|
|
|
+ if( $rewardResult['reward_type'] == 3 ){
|
|
|
+ // 积分大于0
|
|
|
+ if( $rewardResult['reward_info'] > 0 ){
|
|
|
+ // 积分发放
|
|
|
+ $result = $CustomAmount->trade($uid,$lotteryId,$rewardResult['reward_info'],4,1,'灯谜抽奖');
|
|
|
+ // 发放失败,改为未中奖
|
|
|
+ if( isset($result['error']) ) $rewardIndex = 0;
|
|
|
+ // 发放成功,状态为已完成
|
|
|
+ $record['status']= 8;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果是实物,要求填写地址,状态设置为0
|
|
|
+ if( $rewardResult['reward_type'] == 5 ) $record['status'] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 中奖记录奖品
|
|
|
+ if( $rewardIndex ) {
|
|
|
+ // 记录,默认状态为1,进行中
|
|
|
+ $record['reward_id'] = $reward[$rewardIndex]['id'];
|
|
|
+ $record['reward_name'] = $reward[$rewardIndex]['reward_name'];
|
|
|
+ // 扣减次数
|
|
|
+ $RecruitmentReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1')]);
|
|
|
+ }
|
|
|
+ // 写入记录
|
|
|
+ $result = $RecruitmentRecord->add($record);
|
|
|
+ // 写入失败
|
|
|
+ if( !$result ) {
|
|
|
+ // 回退数据
|
|
|
+ DB::rollBack();
|
|
|
+ // 提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'抽奖记录失败,请重试']);
|
|
|
+ }
|
|
|
+ // 提交事务
|
|
|
+ DB::commit();
|
|
|
+ // 通过活动ID,查询奖品
|
|
|
+ $rewardList = [];
|
|
|
+ // 奖品数据
|
|
|
+ foreach ($reward as $value) {
|
|
|
+ $rewardList[] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
|
|
|
+ }
|
|
|
+ // 返回结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'抽奖成功','data'=>['reward_list'=>$rewardList,'reward_index'=>$rewardIndex,'join_num'=>$data['join_num']-1]]);
|
|
|
+ // 异常处理
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ // 回退数据
|
|
|
+ DB::rollBack();
|
|
|
+ // 下单失败提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|