Custom.php 5.0 KB

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