Wechat.php 12 KB

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