|
@@ -1,8 +1,18 @@
|
|
|
<?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;
|
|
|
|
|
|
/**
|
|
|
* 微信接口
|
|
@@ -19,11 +29,12 @@ class Wechat extends Api{
|
|
|
* @param string $code 授权码
|
|
|
*
|
|
|
* */
|
|
|
- public function phone_number(Custom $Custom){
|
|
|
+ public function phone_number(Custom $Custom,Hashids $Hashids){
|
|
|
// 接口验签
|
|
|
// $this->verify_sign();
|
|
|
// 接收参数
|
|
|
$code = request('code','');
|
|
|
+ $shareUid = request('share_uid','');
|
|
|
// 授权结果
|
|
|
$result = Mini::getUserPhone($code);
|
|
|
// 如果所需字段不存在
|
|
@@ -42,12 +53,237 @@ class Wechat extends Api{
|
|
|
$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],
|
|
|
+ ];
|
|
|
+ $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));
|
|
|
+ }
|
|
|
+ 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){
|
|
|
+ $time = time();
|
|
|
+ $Coupon = new \App\Models\Coupon();
|
|
|
+ $CustomCoupon = new \App\Models\CustomCoupon();
|
|
|
+ // 获取优惠券的可用时间
|
|
|
+ $couponData = $Coupon->query()->where([['id','=',$prize],['status','=','0']])->first(['issue_total','status','exp_time']);
|
|
|
+ // 如果不存在数据,发送失败
|
|
|
+ if( !$couponData || $couponData['status'] ) return 0;
|
|
|
+ // 查询总共发放数量
|
|
|
+ $total = $this->query()->where([['coupon_id','=',$prize]])->count();
|
|
|
+ // 数量超过的话。不发
|
|
|
+ if( $total >= $couponData['issue_total'] ) return 0;
|
|
|
+ // 时间转时间
|
|
|
+ $expTime = $Coupon->getExpTime($couponData['exp_time']);
|
|
|
+ // 发送优惠券
|
|
|
+ $res = $CustomCoupon->add(['coupon_id'=>$prize,'custom_uid'=>$uid,'exp_time'=>$expTime]);
|
|
|
+ if (!$res) 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;
|
|
|
+ }
|
|
|
}
|