Custom.php 4.8 KB

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