Custom.php 5.2 KB

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