Custom.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $cityId = (string) $City->getOne($custom['city_id'],'name');
  61. // 所需数组组合
  62. $custom = [
  63. 'uid'=>$custom['uid'],
  64. 'username'=>$custom['username'],
  65. 'userpic'=>$custom['userpic'],
  66. 'phone'=>$custom['phone'],
  67. 'is_manager'=>$custom['is_manager'],
  68. 'company_id'=>1, // 无需验证资质
  69. 'show_price'=>$cityId?1:0, // 是否选择了城市处理
  70. 'city_id'=>$cityId,
  71. 'follow_qrcode'=>$followQrcode['thumb'],
  72. 'follow_linkurl'=>$followQrcode['link_url'],
  73. ];
  74. // 返回结果
  75. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$custom]);
  76. }
  77. /**
  78. * 设置信息 /api/custom/set_city
  79. *
  80. * @param string $code 授权码
  81. *
  82. * */
  83. public function set_city(Request $request,Model $Model,City $City){
  84. // 接口验签
  85. // $this->verify_sign();
  86. // 验证参数
  87. $request->scene('set_city')->validate();
  88. // 检查登录
  89. $uid = $this->checkLogin();
  90. // 接收参数
  91. $city = request('city','');
  92. // 通过城市名获取城市ID
  93. $cityId = $City->getIdByName($city);
  94. // 查询用户
  95. $result = $Model->edit($uid,['city_id'=>$cityId]);
  96. // 用户不存在
  97. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败','data'=>['error'=>'保存失败,请重试']]);
  98. // 返回结果
  99. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['city_id'=>$cityId]]);
  100. }
  101. /**
  102. * 设置信息 /api/custom/get_city
  103. *
  104. * @param string $code 授权码
  105. *
  106. * */
  107. public function get_city(Request $request,Model $Model,City $City){
  108. // 接口验签
  109. // $this->verify_sign();
  110. // 验证参数
  111. $request->scene('get_city')->validate();
  112. // 检查登录
  113. $uid = $this->checkLogin();
  114. // 查询用户
  115. $custom = $Model->getOne($uid);
  116. // 用户不存在
  117. if( empty($custom) ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>['province'=>'','city'=>'']]);
  118. // 获取城市ID
  119. $cityId = $custom['city_id'];
  120. $cityName = $City->getOne($cityId,'name');
  121. $pid = $City->getOne($cityId,'pid');
  122. $province = $City->getOne($pid,'name');
  123. // 返回结果
  124. return json_send(['code'=>'success','msg'=>'获取成功','data'=>['province'=>(string)$province,'city'=>(string)$cityName]]);
  125. }
  126. }