Custom.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 'company_id'=>1, // 无需验证资质
  68. 'show_price'=>$cityId?1:0, // 是否选择了城市处理
  69. 'city_id'=>$cityId,
  70. 'follow_qrcode'=>$followQrcode['thumb'],
  71. 'follow_linkurl'=>$followQrcode['link_url'],
  72. ];
  73. // 返回结果
  74. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$custom]);
  75. }
  76. /**
  77. * 设置信息 /api/custom/set_city
  78. *
  79. * @param string $code 授权码
  80. *
  81. * */
  82. public function set_city(Request $request,Model $Model,City $City){
  83. // 接口验签
  84. // $this->verify_sign();
  85. // 验证参数
  86. $request->scene('set_city')->validate();
  87. // 检查登录
  88. $uid = $this->checkLogin();
  89. // 接收参数
  90. $city = request('city','');
  91. // 通过城市名获取城市ID
  92. $cityId = $City->getIdByName($city);
  93. // 查询用户
  94. $result = $Model->edit($uid,['city_id'=>$cityId]);
  95. // 用户不存在
  96. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败','data'=>['error'=>'保存失败,请重试']]);
  97. // 返回结果
  98. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['city_id'=>$cityId]]);
  99. }
  100. }