Custom.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Facades\Servers\WeiBan\OpenApi;
  3. use App\Http\Controllers\Api\Api;
  4. use App\Models\Custom as Model;
  5. use App\Models\WeiBan\Qrcode as WeiBanQrcode;
  6. use Vinkla\Hashids\Facades\Hashids;
  7. use App\Http\Requests\Api\Custom as Request;
  8. use App\Models\City;
  9. use App\Models\PayCity;
  10. use App\Models\WeiBan\Follow as WeiBanFollow;
  11. use App\Models\CustomAmount;
  12. use App\Models\Video\VideoVip;
  13. /**
  14. * 客户接口
  15. *
  16. * @author 刘相欣
  17. *
  18. * */
  19. class Custom extends Api{
  20. /**
  21. * 获取客户信息 /api/custom/get_info
  22. *
  23. * @param string $code 授权码
  24. *
  25. * */
  26. public function get_info(Model $Model,WeiBanQrcode $WeiBanQrcode,City $City,WeiBanFollow $WeiBanFollow,CustomAmount $CustomAmount,VideoVip $VideoVip,PayCity $PayCity){
  27. // 接口验签
  28. // $this->verify_sign();
  29. // 检查登录
  30. $uid = $this->checkLogin();
  31. // 查询用户
  32. $custom = $Model->getOne($uid);
  33. // 用户不存在
  34. if( empty($custom) ) return json_send(['code'=>'error','msg'=>'用户不存在','data'=>['error'=>'用户不存在']]);
  35. // 如果不存在微伴ID
  36. if( !$custom['weiban_extid'] ) {
  37. // 通过手机号查询注册的账号
  38. $custom['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$custom['phone']]])->value('weiban_extid');
  39. // 不存在,通过接口获取数据
  40. if( !$custom['weiban_extid'] ) {
  41. // 获取用户列表
  42. $userList = OpenApi::getUserListByPhone($custom['phone']);
  43. // 如果有的话
  44. if( $userList ) {
  45. // 获取第一个
  46. $extUser = array_shift($userList);
  47. // 获取对应的ID
  48. $custom['weiban_extid'] = empty($extUser['id']) ? '' : $extUser['id'];
  49. }
  50. }
  51. // 存在则修正
  52. if( $custom['weiban_extid'] ) $Model->edit($custom['uid'],['weiban_extid'=>$custom['weiban_extid']]);
  53. }
  54. // 头像
  55. $custom['uid'] = Hashids::encodeHex($custom['uid']);
  56. // 头像
  57. $custom['userpic'] = $custom['userpic'] ? path_compat($custom['userpic']) : '';
  58. // 手机号
  59. $custom['phone'] = hide_phone($custom['phone']);
  60. // 如果没有关联企微,获取二维码,已关联的不验证
  61. $followQrcode = $custom['weiban_extid'] ? ['thumb'=>'','link_url'=>''] : $WeiBanQrcode->getFollowQrcode($custom['city_id']);
  62. // 城市ID换城市名
  63. $cityName = (string) $City->getOne($custom['city_id'],'name');
  64. //判断用户所在城市是否设置了支付
  65. $payCity = $PayCity->where([['status','=',0]])->first();
  66. $isPay = 0;
  67. if ($payCity && $payCity['city_ids']) {
  68. $cityIds = explode(',',$payCity['city_ids']);
  69. if (in_array($custom['city_id'], $cityIds)) {
  70. $isPay = 1;
  71. }
  72. }
  73. //获取用户余额
  74. $amountInfo = $CustomAmount::query()->where([['custom_uid','=',$uid]])->first(['amount','transfer_amount']);
  75. // 如果没有余额,获取默认值
  76. if (!$amountInfo) $amountInfo = ['amount'=>0,'transfer_amount'=>0];
  77. // 获取用户
  78. $isVip = $VideoVip->getOne($uid);
  79. // 所需数组组合
  80. $custom = [
  81. 'uid'=>$custom['uid'],
  82. 'have_follow'=>$custom['weiban_extid']?1:1,
  83. 'username'=>$custom['username'],
  84. 'userpic'=>$custom['userpic'],
  85. 'phone'=>$custom['phone'],
  86. 'is_manager'=>$custom['is_manager'],
  87. 'is_pay'=>$isPay,
  88. 'company_id'=>1, // 无需验证资质
  89. 'show_price'=>$cityName?1:0, // 是否选择了城市处理
  90. 'city_id'=>$cityName,
  91. 'follow_qrcode'=>$followQrcode['thumb'],
  92. 'follow_linkurl'=>$followQrcode['link_url'],
  93. 'amount'=>$amountInfo['amount'],
  94. 'is_video_vip'=> $isVip ? ( $isVip['start_time'] <= time() && $isVip['end_time'] >= time() ? 1: 0 ) : 0,
  95. 'transfer_amount'=>$amountInfo['transfer_amount'],
  96. ];
  97. // 返回结果
  98. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$custom]);
  99. }
  100. /**
  101. * 设置信息 /api/custom/set_city
  102. *
  103. * @param string $code 授权码
  104. *
  105. * */
  106. public function set_city(Request $request,Model $Model,City $City){
  107. // 接口验签
  108. // $this->verify_sign();
  109. // 验证参数
  110. $request->scene('set_city')->validate();
  111. // 检查登录
  112. $uid = $this->checkLogin();
  113. // 接收参数
  114. $city = request('city','');
  115. // 通过城市名获取城市ID
  116. $cityId = $City->getIdByName($city);
  117. // 查询用户
  118. $result = $Model->edit($uid,['city_id'=>$cityId]);
  119. // 用户不存在
  120. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败','data'=>['error'=>'保存失败,请重试']]);
  121. // 返回结果
  122. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['city_id'=>$cityId]]);
  123. }
  124. /**
  125. * 设置信息 /api/custom/get_city
  126. *
  127. * @param string $code 授权码
  128. *
  129. * */
  130. public function get_city(Request $request,Model $Model,City $City){
  131. // 接口验签
  132. // $this->verify_sign();
  133. // 验证参数
  134. $request->scene('get_city')->validate();
  135. // 检查登录
  136. $uid = $this->checkLogin();
  137. // 查询用户
  138. $custom = $Model->getOne($uid);
  139. // 用户不存在
  140. if( empty($custom) ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>['province'=>'','city'=>'']]);
  141. // 获取城市ID
  142. $cityId = $custom['city_id'];
  143. $cityName = $City->getOne($cityId,'name');
  144. $pid = $City->getOne($cityId,'pid');
  145. $province = $City->getOne($pid,'name');
  146. // 返回结果
  147. return json_send(['code'=>'success','msg'=>'获取成功','data'=>['province'=>(string)$province,'city'=>(string)$cityName]]);
  148. }
  149. }