Custom.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Custom as Model;
  4. use App\Models\Work\State as WorkState;
  5. use Vinkla\Hashids\Facades\Hashids;
  6. use App\Http\Requests\Api\Custom as Request;
  7. use App\Models\City;
  8. use App\Models\Work\External as WorkExternal;
  9. /**
  10. * 客户接口
  11. *
  12. * @author 刘相欣
  13. *
  14. * */
  15. class Custom extends Api{
  16. /**
  17. * 获取客户信息 /api/custom/get_info
  18. *
  19. * @param string $code 授权码
  20. *
  21. * */
  22. public function get_info(Model $Model,WorkState $WorkState,City $City,WorkExternal $WorkExternal){
  23. // 接口验签
  24. // $this->verify_sign();
  25. // 检查登录
  26. $uid = $this->checkLogin();
  27. // 查询用户
  28. $custom = $Model->getOne($uid);
  29. // 用户不存在
  30. if( empty($custom) ) return json_send(['code'=>'error','msg'=>'用户不存在','data'=>['error'=>'用户不存在']]);
  31. // 如果不存在企微ID,查询数据库
  32. if( !$custom['external_userid'] ) $custom['external_userid'] = (string) $WorkExternal->query()->where([['custom_uid', '=',$custom['uid']]])->value('external_userid');
  33. // 头像
  34. $custom['uid'] = Hashids::encodeHex($custom['uid']);
  35. // 头像
  36. $custom['userpic'] = $custom['userpic'] ? path_compat($custom['userpic']) : '';
  37. // 手机号
  38. $custom['phone'] = hide_phone($custom['phone']);
  39. // 如果没有关联企微,获取二维码,已关联的不验证
  40. $followQrcode = $WorkState->getFollowQrcode($custom['city_id']);
  41. // 城市ID换城市名
  42. $cityId = (string) $City->getOne($custom['city_id'],'name');
  43. // 所需数组组合
  44. $custom = [
  45. 'uid'=>$custom['uid'],
  46. 'username'=>$custom['username'],
  47. 'userpic'=>$custom['userpic'],
  48. 'phone'=>$custom['phone'],
  49. 'company_id'=>1, // 无需验证资质
  50. 'show_price'=>1, // 是否选择了城市处理
  51. 'city_id'=>$cityId,
  52. 'have_follow'=>$custom['external_userid']?1:0,
  53. 'follow_qrcode'=>(string)$followQrcode,
  54. 'follow_linkurl'=>'',
  55. ];
  56. // 返回结果
  57. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$custom]);
  58. }
  59. /**
  60. * 设置信息 /api/custom/set_city
  61. *
  62. * @param string $code 授权码
  63. *
  64. * */
  65. public function set_city(Request $request,Model $Model,City $City){
  66. // 接口验签
  67. // $this->verify_sign();
  68. // 验证参数
  69. $request->scene('set_city')->validate();
  70. // 检查登录
  71. $uid = $this->checkLogin();
  72. // 接收参数
  73. $city = request('city','');
  74. // 通过城市名获取城市ID
  75. $cityId = $City->getIdByName($city);
  76. // 查询用户
  77. $result = $Model->edit($uid,['city_id'=>$cityId]);
  78. // 用户不存在
  79. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败','data'=>['error'=>'保存失败,请重试']]);
  80. // 返回结果
  81. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['city_id'=>$cityId]]);
  82. }
  83. }