Wechat.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Facades\Servers\Logs\Log;
  3. use App\Http\Controllers\Api\Api;
  4. use App\Models\City;
  5. use App\Models\Custom;
  6. use App\Facades\Servers\WechatMini\Mini;
  7. use App\Models\RecruitmentActive;
  8. use App\Models\RecruitmentActivePrize;
  9. use App\Models\RecruitmentPrizeRecord;
  10. use App\Models\RecruitmentRecord;
  11. use App\Models\Score\Record;
  12. use App\Models\WeiBan\Tags;
  13. use App\Models\CustomScore;
  14. use Vinkla\Hashids\Facades\Hashids;
  15. /**
  16. * 微信接口
  17. *
  18. * @author 刘相欣
  19. *
  20. * */
  21. class Wechat extends Api{
  22. /**
  23. * 小程序手机号授权 /api/wechat/phone_number
  24. *
  25. * @param string $code 授权码
  26. *
  27. * */
  28. public function phone_number(Custom $Custom,Hashids $Hashids){
  29. // 接口验签
  30. // $this->verify_sign();
  31. // 接收参数
  32. $code = request('code','');
  33. $shareUid = request('share_uid','');
  34. // 授权结果
  35. $result = Mini::getUserPhone($code);
  36. // 如果所需字段不存在
  37. if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>'授权失败','data'=>['error'=>$result['error']]]);
  38. // 如果所需字段不存在
  39. if( empty($result['purePhoneNumber']) ) return json_send(['code'=>'error','msg'=>'未获取到手机号','data'=>['error'=>'未获取到手机号']]);
  40. // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
  41. $phone = $result['purePhoneNumber'];
  42. // 查询用户
  43. $custom = $Custom->getOneByPhone($phone);
  44. // 如果用户状态被拉黑,不允许登录
  45. if( !empty($custom['status']) ) return json_send(['code'=>'error','msg'=>'禁用账号','data'=>['error'=>'禁用账号']]);
  46. // 如果没有ID
  47. if( empty($custom['uid']) ) {
  48. // 注册账号
  49. $custom['uid'] = $Custom->add(['phone'=>$phone,'username'=>hide_phone($phone)]);
  50. // 注册失败
  51. if( empty($custom['uid']) ) return json_send(['code'=>'error','msg'=>'注册失败,请重试','data'=>['error'=>'注册失败,请重试']]);
  52. //绑定裂变邀请关系
  53. if($shareUid){
  54. $shareUid = $Hashids::decodeHex($shareUid);
  55. Log::error('recruitment','拉新活动:'.$shareUid);
  56. $this->addRecruitment($custom['uid'],$shareUid);
  57. }
  58. }
  59. // 进行登录
  60. $token = $Custom->createLoginAuthcode($custom['uid'],time());
  61. // 返回结果
  62. return json_send(['code'=>'success','msg'=>'登录成功','data'=>$token]);
  63. }
  64. /**
  65. * 拉新注册赠送奖励
  66. *
  67. * */
  68. public function addRecruitment($uid,$shareUid){
  69. $Custom = new Custom();
  70. $WeiBanTags = new Tags();
  71. $RecruitmentActive = new RecruitmentActive();
  72. // 获取客户城市ID
  73. $custom = $Custom->getOne($shareUid);
  74. //查询拉新活动
  75. if( !$custom['city_id'] ){
  76. Log::error('recruitment','拉新活动,缺少城市:'.json_encode($custom));
  77. return false;
  78. }
  79. // 获取城市ID
  80. $cityId = $custom['city_id'];
  81. // 查询用户标签
  82. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  83. $time = time();
  84. $select = [
  85. ['start_time','<=',$time],
  86. ['end_time','>',$time],
  87. ['status','=',0],
  88. ];
  89. $activeList = $RecruitmentActive::query()->whereRaw("find_in_set('$cityId', city_ids)")->where($select)->get();
  90. $activeInfo = [];
  91. $data = [];
  92. if ($activeList) {
  93. foreach ($activeList as $active) {
  94. $allowJoin = 0;
  95. if ($active['tag_scope']) {
  96. // 解析数组
  97. $tag_scope = explode(',', $active['tag_scope']);
  98. // 标签范围限定时,默认不能参与
  99. // 判断标签是不是存在
  100. if ($tags) {
  101. foreach ($tags as $v) {
  102. // 标签范围内,允许参加
  103. if (in_array($v['name'], $tag_scope)) $allowJoin = 1;
  104. }
  105. }
  106. }else{
  107. $allowJoin = 1;
  108. }
  109. if ($active['tag_except']) {
  110. // 解析数组
  111. $tag_except = explode(',', $active['tag_except']);
  112. // 标签范围限定时,默认不能参与
  113. $allowJoin = 0;
  114. // 判断标签是不是存在
  115. if ($tags) {
  116. foreach ($tags as $v) {
  117. // 标签范围内,允许参加
  118. if (in_array($v['name'], $tag_except)) $allowJoin = 0;
  119. }
  120. }
  121. }
  122. if ($allowJoin) {
  123. $activeInfo = $active;
  124. break;
  125. }
  126. }
  127. }else{
  128. Log::error('recruitment','custom_uid:'.$uid.';暂无拉新活动:');
  129. }
  130. if (!empty($activeInfo)){
  131. $data['active_id'] = $activeInfo['id'];
  132. $data['old_uid'] = $shareUid;
  133. $data['new_uid'] = $uid;
  134. $data['insert_time'] = $time;
  135. $data['update_time'] = $time;
  136. //拉新记录
  137. $recordId = RecruitmentRecord::query()->insertGetId($data);
  138. if ($recordId){
  139. Log::info('recruitment','custom_uid:'.$uid.';拉新活动赠送奖励:'.json_encode($activeInfo));
  140. //查询活动奖励配置
  141. $prizeInfo = RecruitmentActivePrize::query()->where('active_id','=',$activeInfo['id'])->first();
  142. if (!$prizeInfo) {
  143. Log::info('recruitment','custom_uid:'.$uid.';拉新活动赠送奖励失败,无奖励配置:'.json_encode($prizeInfo));
  144. return false;
  145. }
  146. //赠送拉新奖励
  147. if ($prizeInfo['old_prize']){
  148. switch ($prizeInfo['old_prize_type']) {
  149. case 1:
  150. //赠送老用户积分
  151. $res = $this->sendScore($shareUid,$prizeInfo['old_prize'],$recordId,1);
  152. if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送老用户奖励失败:'.json_encode($prizeInfo));
  153. break;
  154. case 2:
  155. //赠送老用户优惠卷
  156. $res = $this->sendCoupon($shareUid,$prizeInfo['old_prize'],$recordId,1);
  157. if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送老用户奖励失败:'.json_encode($prizeInfo));
  158. break;
  159. }
  160. }
  161. //赠送新用户奖励
  162. if ($prizeInfo['new_prize']){
  163. switch ($prizeInfo['new_prize_type']) {
  164. case 1:
  165. //赠送老用户积分
  166. $res = $this->sendScore($uid,$prizeInfo['new_prize'],$recordId,2);
  167. if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送新用户奖励失败:'.json_encode($prizeInfo));
  168. break;
  169. case 2:
  170. //赠送老用户优惠卷
  171. $res = $this->sendCoupon($uid,$prizeInfo['new_prize'],$recordId,2);
  172. if (!$res) Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送新用户奖励失败:'.json_encode($prizeInfo));
  173. break;
  174. }
  175. }
  176. //赠送上级奖励
  177. if ($prizeInfo['higher_prize']){
  178. //查询上级用户
  179. $higherInfo = RecruitmentRecord::query()->where('new_uid','=',$shareUid)->first();
  180. if (!$higherInfo) return true;
  181. switch ($prizeInfo['higher_prize_type']) {
  182. case 1:
  183. //赠送上级积分
  184. $res = $this->sendScore($higherInfo['old_uid'],$prizeInfo['higher_prize'],$recordId,3);
  185. if (!$res) Log::error('recruitment','custom_uid:'.$higherInfo['old_uid'].';拉新活动赠送上级奖励失败:'.json_encode($prizeInfo));
  186. break;
  187. case 2:
  188. //赠送上级优惠卷
  189. $res = $this->sendCoupon($higherInfo['old_uid'],$prizeInfo['higher_prize'],$recordId,3);
  190. if (!$res) Log::error('recruitment','custom_uid:'.$higherInfo['old_uid'].';拉新活动赠送上级奖励失败:'.json_encode($prizeInfo));
  191. break;
  192. }
  193. }
  194. }else{
  195. Log::error('recruitment','custom_uid:'.$uid.';拉新活动新增拉新记录失败:'.json_encode($data));
  196. }
  197. }else{
  198. Log::error('recruitment','custom_uid:'.$uid.';无拉新活动:'.json_encode($activeInfo));
  199. }
  200. return true;
  201. }
  202. public function sendScore($uid,$prize,$recordId,$type=1){
  203. $time = time();
  204. $prize = (int)$prize;
  205. $scoreInfo = CustomScore::query()->where('custom_uid','=',$uid)->first();
  206. if ($scoreInfo){
  207. $res = CustomScore::query()->where('custom_uid','=',$uid)->update(['score'=>$scoreInfo['score']+$prize,'update_time'=>$time]);
  208. }else{
  209. $res = CustomScore::query()->where('custom_uid','=',$uid)->insert(['custom_uid'=>$uid,'score'=>$prize,'update_time'=>$time,'insert_time'=>$time]);
  210. }
  211. if (!$res){
  212. Log::error('recruitment','custom_uid:'.$uid.';拉新活动赠送老用户奖励失败:'.json_encode($prize));
  213. return false;
  214. }
  215. $balance = CustomScore::query()->where('custom_uid','=',$uid)->first('score');
  216. $recordInfo = [
  217. 'score' => $prize,
  218. 'balance' => $balance,
  219. 'buy_type' => 9,
  220. 'pay_type' => 1,
  221. 'description' => '拉新奖励',
  222. 'status' => '1',
  223. 'pay_time' => $time,
  224. 'insert_time' => $time,
  225. 'update_time' => $time,
  226. 'custom_uid' => $uid,
  227. ];
  228. //用户积分记录
  229. $res = Record::query()->insertGetId($recordInfo);
  230. if (!$res){
  231. Log::error('recruitment','积分记录失败;record:'.json_encode($recordInfo));
  232. return false;
  233. }
  234. //拉新奖励记录
  235. $prizeRecordInfo = [
  236. 'recruitment_record_id' => $recordId,
  237. 'custom_uid' => $uid,
  238. 'type' => $type,
  239. 'prize_type' => 1,
  240. 'prize' => $prize,
  241. 'insert_time' => $time,
  242. 'update_time' => $time,
  243. ];
  244. $res = RecruitmentPrizeRecord::query()->insertGetId($prizeRecordInfo);
  245. if (!$res){
  246. Log::error('recruitment','奖励记录失败;record:'.json_encode($prizeRecordInfo));
  247. return false;
  248. }
  249. return true;
  250. }
  251. public function sendCoupon($uid,$prize,$recordId,$type=1){
  252. $time = time();
  253. $Coupon = new \App\Models\Coupon();
  254. $CustomCoupon = new \App\Models\CustomCoupon();
  255. // 获取优惠券的可用时间
  256. $couponData = $Coupon->query()->where([['id','=',$prize],['status','=','0']])->first(['issue_total','status','exp_time']);
  257. // 如果不存在数据,发送失败
  258. if( !$couponData || $couponData['status'] ) return 0;
  259. // 查询总共发放数量
  260. $total = $this->query()->where([['coupon_id','=',$prize]])->count();
  261. // 数量超过的话。不发
  262. if( $total >= $couponData['issue_total'] ) return 0;
  263. // 时间转时间
  264. $expTime = $Coupon->getExpTime($couponData['exp_time']);
  265. // 发送优惠券
  266. $res = $CustomCoupon->add(['coupon_id'=>$prize,'custom_uid'=>$uid,'exp_time'=>$expTime]);
  267. if (!$res) return false;
  268. //拉新奖励记录
  269. $prizeRecordInfo = [
  270. 'recruitment_record_id' => $recordId,
  271. 'custom_uid' => $uid,
  272. 'type' => $type,
  273. 'prize_type' => 1,
  274. 'prize' => $prize,
  275. 'insert_time' => $time,
  276. 'update_time' => $time,
  277. ];
  278. $res = RecruitmentPrizeRecord::query()->insertGetId($prizeRecordInfo);
  279. if (!$res){
  280. Log::error('recruitment','奖励记录失败;record:'.json_encode($prizeRecordInfo));
  281. return false;
  282. }
  283. return true;
  284. }
  285. }