123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php namespace App\Http\Controllers\Api;
- use App\Facades\Servers\Logs\Log;
- use App\Http\Controllers\Api\Api;
- use App\Models\City;
- use App\Models\Custom;
- use App\Facades\Servers\WechatMini\Mini;
- use App\Models\RecruitmentActive;
- use App\Models\RecruitmentActivePrize;
- use App\Models\RecruitmentPrizeRecord;
- use App\Models\RecruitmentRecord;
- use App\Models\Score\Record;
- use App\Models\WeiBan\Tags;
- use App\Models\CustomScore;
- use Vinkla\Hashids\Facades\Hashids;
- /**
- * 微信接口
- *
- * @author 刘相欣
- *
- * */
- class Wechat extends Api{
-
-
- /**
- * 小程序手机号授权 /api/wechat/phone_number
- *
- * @param string $code 授权码
- *
- * */
- public function phone_number(Custom $Custom,Hashids $Hashids){
- // 接口验签
- // $this->verify_sign();
- // 接收参数
- $code = request('code','');
- $shareUid = request('share_uid','');
- // 授权结果
- $result = Mini::getUserPhone($code);
- // 如果所需字段不存在
- if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>'授权失败','data'=>['error'=>$result['error']]]);
- // 如果所需字段不存在
- if( empty($result['purePhoneNumber']) ) return json_send(['code'=>'error','msg'=>'未获取到手机号','data'=>['error'=>'未获取到手机号']]);
- // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
- $phone = $result['purePhoneNumber'];
- // 查询用户
- $custom = $Custom->getOneByPhone($phone);
- // 如果用户状态被拉黑,不允许登录
- if( !empty($custom['status']) ) return json_send(['code'=>'error','msg'=>'禁用账号','data'=>['error'=>'禁用账号']]);
- // 如果没有ID
- if( empty($custom['uid']) ) {
- // 注册账号
- $custom['uid'] = $Custom->add(['phone'=>$phone,'username'=>hide_phone($phone)]);
- // 注册失败
- if( empty($custom['uid']) ) return json_send(['code'=>'error','msg'=>'注册失败,请重试','data'=>['error'=>'注册失败,请重试']]);
- //绑定裂变邀请关系
- if($shareUid){
- $shareUid = $Hashids::decodeHex($shareUid);
- Log::error('recruitment','拉新活动:'.$shareUid);
- $this->addRecruitment($custom['uid'],$shareUid);
- }
- }
- // 进行登录
- $token = $Custom->createLoginAuthcode($custom['uid'],time());
- // 返回结果
- return json_send(['code'=>'success','msg'=>'登录成功','data'=>$token]);
- }
- /**
- * 拉新注册赠送奖励
- *
- * */
- public function addRecruitment($uid,$shareUid){
- $Custom = new Custom();
- $WeiBanTags = new Tags();
- $RecruitmentActive = new RecruitmentActive();
- // 获取客户城市ID
- $custom = $Custom->getOne($shareUid);
- //查询拉新活动
- if( !$custom['city_id'] ){
- Log::error('recruitment','拉新活动,缺少城市:'.json_encode($custom));
- return false;
- }
- // 获取城市ID
- $cityId = $custom['city_id'];
- // 查询用户标签
- $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
- $time = time();
- $select = [
- ['start_time','<=',$time],
- ['end_time','>',$time],
- ['status','=',0],
- ];
- try {
- $activeList = $RecruitmentActive::query()->whereRaw("find_in_set('$cityId', city_ids)")->where($select)->get();
- $activeInfo = [];
- $data = [];
- if ($activeList) {
- foreach ($activeList as $active) {
- $allowJoin = 0;
- if ($active['tag_scope']) {
- // 解析数组
- $tag_scope = explode(',', $active['tag_scope']);
- // 标签范围限定时,默认不能参与
- // 判断标签是不是存在
- if ($tags) {
- foreach ($tags as $v) {
- // 标签范围内,允许参加
- if (in_array($v['name'], $tag_scope)) $allowJoin = 1;
- }
- }
- }else{
- $allowJoin = 1;
- }
- if ($active['tag_except']) {
- // 解析数组
- $tag_except = explode(',', $active['tag_except']);
- // 标签范围限定时,默认不能参与
- $allowJoin = 0;
- // 判断标签是不是存在
- if ($tags) {
- foreach ($tags as $v) {
- // 标签范围内,允许参加
- if (in_array($v['name'], $tag_except)) $allowJoin = 0;
- }
- }
- }
- if ($allowJoin) {
- $activeInfo = $active;
- break;
- }
- }
- }else{
- Log::error('recruitment','custom_uid:'.$uid.';暂无拉新活动:');
- }
- if (!empty($activeInfo)){
- $data['active_id'] = $activeInfo['id'];
- $data['old_uid'] = $shareUid;
- $data['new_uid'] = $uid;
- $data['insert_time'] = $time;
- $data['update_time'] = $time;
- //拉新记录
- $recordId = RecruitmentRecord::query()->insertGetId($data);
- if ($recordId){
- Log::info('recruitment','custom_uid:'.$uid.';拉新活动赠送奖励:'.json_encode($activeInfo));
- //查询活动奖励配置
- $prizeInfo = RecruitmentActivePrize::query()->where('active_id','=',$activeInfo['id'])->first();
- if (!$prizeInfo) {
- Log::info('recruitment','custom_uid:'.$uid.';拉新活动赠送奖励失败,无奖励配置:'.json_encode($prizeInfo));
- return false;
- }
- //赠送拉新奖励
- if ($prizeInfo['old_prize']){
- switch ($prizeInfo['old_prize_type']) {
- case 1:
- //赠送老用户积分
- $res = $this->sendScore($shareUid,$prizeInfo['old_prize'],$recordId,1);
- if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送老用户奖励失败:'.json_encode($prizeInfo));
- break;
- case 2:
- //赠送老用户优惠卷
- $res = $this->sendCoupon($shareUid,$prizeInfo['old_prize'],$recordId,1);
- if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送老用户奖励失败:'.json_encode($prizeInfo));
- break;
- }
- }
- //赠送新用户奖励
- if ($prizeInfo['new_prize']){
- switch ($prizeInfo['new_prize_type']) {
- case 1:
- //赠送老用户积分
- $res = $this->sendScore($uid,$prizeInfo['new_prize'],$recordId,2);
- if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送新用户奖励失败:'.json_encode($prizeInfo));
- break;
- case 2:
- //赠送老用户优惠卷
- $res = $this->sendCoupon($uid,$prizeInfo['new_prize'],$recordId,2);
- if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送新用户奖励失败:'.json_encode($prizeInfo));
- break;
- }
- }
- //赠送上级奖励
- if ($prizeInfo['higher_prize']){
- //查询上级用户
- $higherInfo = RecruitmentRecord::query()->where('new_uid','=',$shareUid)->first();
- if (!$higherInfo) return true;
- switch ($prizeInfo['higher_prize_type']) {
- case 1:
- //赠送上级积分
- $res = $this->sendScore($higherInfo['old_uid'],$prizeInfo['higher_prize'],$recordId,3);
- if (!$res) Log::error('recruitment','custom_uid:'.$higherInfo['old_uid'].';拉新活动赠送上级奖励失败:'.json_encode($prizeInfo));
- break;
- case 2:
- //赠送上级优惠卷
- $res = $this->sendCoupon($higherInfo['old_uid'],$prizeInfo['higher_prize'],$recordId,3);
- if (!$res) Log::error('recruitment','custom_uid:'.$higherInfo['old_uid'].';拉新活动赠送上级奖励失败:'.json_encode($prizeInfo));
- break;
- }
- }
- }else{
- Log::error('recruitment','custom_uid:'.$uid.';拉新活动新增拉新记录失败:'.json_encode($data));
- }
- }else{
- Log::error('recruitment','custom_uid:'.$uid.';无拉新活动:'.json_encode($activeInfo));
- }
- }catch (\Exception $e){
- Log::error('recruitment','custom_uid:'.$uid.';拉新活动:'.json_encode($e));
- return false;
- }
- return true;
- }
- public function sendScore($uid,$prize,$recordId,$type=1){
- $time = time();
- $prize = (int)$prize;
- $scoreInfo = CustomScore::query()->where('custom_uid','=',$uid)->first();
- if ($scoreInfo){
- $res = CustomScore::query()->where('custom_uid','=',$uid)->update(['score'=>$scoreInfo['score']+$prize,'update_time'=>$time]);
- }else{
- $res = CustomScore::query()->where('custom_uid','=',$uid)->insert(['custom_uid'=>$uid,'score'=>$prize,'update_time'=>$time,'insert_time'=>$time]);
- }
- if (!$res){
- Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送老用户奖励失败:'.json_encode($prize));
- return false;
- }
- $balance = CustomScore::query()->where('custom_uid','=',$uid)->first('score');
- $recordInfo = [
- 'score' => $prize,
- 'balance' => $balance,
- 'buy_type' => 9,
- 'pay_type' => 1,
- 'description' => '拉新奖励',
- 'status' => '1',
- 'pay_time' => $time,
- 'insert_time' => $time,
- 'update_time' => $time,
- 'custom_uid' => $uid,
- ];
- //用户积分记录
- $res = Record::query()->insertGetId($recordInfo);
- if (!$res){
- Log::error('recruitment','积分记录失败;record:'.json_encode($recordInfo));
- return false;
- }
- //拉新奖励记录
- $prizeRecordInfo = [
- 'recruitment_record_id' => $recordId,
- 'custom_uid' => $uid,
- 'type' => $type,
- 'prize_type' => 1,
- 'prize' => $prize,
- 'insert_time' => $time,
- 'update_time' => $time,
- ];
- $res = RecruitmentPrizeRecord::query()->insertGetId($prizeRecordInfo);
- if (!$res){
- Log::error('recruitment','奖励记录失败;record:'.json_encode($prizeRecordInfo));
- return false;
- }
- return true;
- }
- public function sendCoupon($uid,$prize,$recordId,$type=1){
- Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送优惠卷:'.json_encode($prize));
- $time = time();
- $Coupon = new \App\Models\Coupon();
- $CustomCoupon = new \App\Models\CustomCoupon();
- try {
- // 获取优惠券的可用时间
- $couponData = $Coupon->query()->where([['id','=',$prize],['status','=','0']])->first(['issue_total','status','exp_time']);
- // 如果不存在数据,发送失败
- if( !$couponData || $couponData['status'] ){
- Log::error('recruitment','custom_uid:'.$uid.';优惠卷奖励不存在:'.json_encode($prize));
- return false;
- }
- // 查询总共发放数量
- $total = $this->query()->where([['coupon_id','=',$prize]])->count();
- // 数量超过的话。不发
- if( $total >= $couponData['issue_total'] ) {
- Log::error('recruitment','custom_uid:'.$uid.';优惠卷奖励超发:'.json_encode($prize));
- return false;
- }
- // 时间转时间
- $expTime = $Coupon->getExpTime($couponData['exp_time']);
- // 发送优惠券
- $res = $CustomCoupon->add(['coupon_id'=>$prize,'custom_uid'=>$uid,'exp_time'=>$expTime]);
- if (!$res){
- Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送优惠卷奖励失败:'.json_encode($prize));
- return false;
- }
- //拉新奖励记录
- $prizeRecordInfo = [
- 'recruitment_record_id' => $recordId,
- 'custom_uid' => $uid,
- 'type' => $type,
- 'prize_type' => 2,
- 'prize' => $prize,
- 'insert_time' => $time,
- 'update_time' => $time,
- ];
- $res = RecruitmentPrizeRecord::query()->insertGetId($prizeRecordInfo);
- if (!$res){
- Log::error('recruitment','奖励记录失败;record:'.json_encode($prizeRecordInfo));
- return false;
- }
- }catch (\Exception $e){
- Log::error('recruitment','奖励优惠卷失败;record:'.json_encode($e));
- return false;
- }
- return true;
- }
- }
|