CustomAddr.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\CustomAddr as Model;
  4. use App\Http\Requests\Api\CustomAddr as Request;
  5. use App\Models\City;
  6. use App\Models\Custom;
  7. use App\Models\WeiBan\Follow as WeiBanFollow;
  8. use App\Facades\Servers\WeiBan\OpenApi;
  9. /**
  10. * 客户地址
  11. *
  12. * @author 刘相欣
  13. *
  14. * */
  15. class CustomAddr extends Api{
  16. /**
  17. * 获取列表 /api/custom_addr/get_list
  18. *
  19. * @param string $code 授权码
  20. *
  21. * */
  22. public function get_list(Model $Model){
  23. // 接口验签
  24. // $this->verify_sign();
  25. // 检查登录
  26. $uid = $this->checkLogin();
  27. // 查询信息
  28. $list = $Model->getListByCustom($uid);
  29. // 返回结果
  30. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$list]);
  31. }
  32. /**
  33. * 添加 /api/custom_addr/add
  34. *
  35. * @param string $code 授权码
  36. *
  37. * */
  38. public function add(Request $request,Model $Model,Custom $Custom,City $City,WeiBanFollow $WeiBanFollow){
  39. // 接口验签
  40. // $this->verify_sign();
  41. // 验证参数
  42. $request->scene('add')->validate();
  43. // 检查登录
  44. $uid = $this->checkLogin();
  45. // 获取客户信息
  46. $custom = $Custom->getOne($uid);
  47. // 如果客户信息不存在的话
  48. if( !$custom ) return json_send(['code'=>'error','msg'=>'客户信息不存在','data'=>['error'=>'客户信息不存在']]);
  49. // 如果用户状态被拉黑,不允许登录
  50. if( $custom['status'] ) return json_send(['code'=>'error','msg'=>'禁用账号','data'=>['error'=>'禁用账号']]);
  51. // 接收参数
  52. $data['contact_province'] = trim(request('contact_province',''));
  53. $data['contact_city'] = trim(request('contact_city',''));
  54. $data['contact_area'] = trim(request('contact_area',''));
  55. $data['contact_addr'] = trim(request('contact_addr',''));
  56. $data['contact_name'] = trim(request('contact_name',''));
  57. $data['contact_shop'] = trim(request('contact_shop',''));
  58. $data['shop_type'] = trim(request('shop_type',0));
  59. $data['contact_phone'] = trim(request('contact_phone',''));
  60. $data['is_default'] = request('is_default',0);
  61. $data['custom_uid'] = $uid;
  62. $cityName = $custom['city_id'] ? $City->getOne($custom['city_id'],'name') : '';
  63. $pid = $custom['city_id'] ? $City->getOne($custom['city_id'],'pid') : 0;
  64. // 如果上级不是省份
  65. if( strlen($custom['city_id']) > 4) $pid = (int) $City->getOne($pid,'pid');
  66. $province = $pid ? $City->getOne($pid,'name') : '';
  67. // 如果不是海南,
  68. if( $province != '海南省' ) {
  69. // 判断选择的城市名称是不是一致
  70. if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.($province=='直辖县级'?$cityName:$province).'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  71. }else{
  72. if( trim($data['contact_province']) != trim($province) ) return json_send(['code'=>'error','msg'=>'收货地址请选择海南范围','data'=>['error'=>'收货地址需与您所选城市一致']]);
  73. }
  74. // 替换地址中间的空格
  75. $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
  76. // 最大数量
  77. $limitMax = 20;
  78. $havaNum = $Model->query()->where([['custom_uid','=',$uid]])->count();
  79. // 如果是最大数量的话
  80. if( $havaNum > $limitMax ) return json_send(['code'=>'error','msg'=>'您已经有'.$limitMax.'条地址了','data'=>['error'=>'最多只能保20条地址']]);
  81. // 如果需要默认当前的话
  82. if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  83. // 查询是否已经提交过
  84. $result = $Model->add($data);
  85. // 如果用户状态被拉黑,不允许登录
  86. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  87. // 如果存在微伴ID
  88. if( !$custom['weiban_extid'] ) return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  89. // 获取客服对应的备注等信息
  90. $staffId = $WeiBanFollow->query()->where([['weiban_extid','=',$custom['weiban_extid']]])->value('staff_id');
  91. // 如果存在微伴ID
  92. if( !$staffId ) return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  93. // 自动标签
  94. if( !$havaNum ) $this->autoTags($province,$cityName,$data['shop_type'],$staffId,$custom['weiban_extid']);
  95. // 返回结果
  96. return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  97. }
  98. /**
  99. * 自动标签
  100. */
  101. private function autoTags($province,$cityName,$shopType,$staffId,$weibanExtid){
  102. // 如果有地址信息
  103. switch ($shopType) {
  104. case '1':
  105. $shopType = '单店';
  106. break;
  107. case '2':
  108. $shopType = '连锁';
  109. break;
  110. case '3':
  111. $shopType = '第三终端';
  112. break;
  113. default:
  114. $shopType = '';
  115. break;
  116. }
  117. // 省份
  118. $province = str_ireplace(['自治区','壮族','回族','维吾尔','特别行政区','省'],'',$province);
  119. // 城市
  120. $cityName = rtrim($cityName,'市');
  121. $cityName = str_ireplace(['自治州','自治县','蒙古','蒙古族','回族','藏族','维吾尔','苗族','彝族','壮族','布依族','朝鲜族','满族','侗族','瑶族','白族','土家族','哈尼族','哈萨克','傣族','黎族','傈僳族','佤族','畲族','拉祜族','水族','东乡族','纳西族','景颇族','柯尔克孜','土族','达斡尔族','仫佬族','羌族','布朗族','撒拉族','毛南族','仡佬族','锡伯','阿昌族','普米族','塔吉克','怒族','鄂温克族','德昂族','保安族','裕固族','塔塔尔','独龙族'],'',$cityName);
  122. // 打标签
  123. if( $province ) OpenApi::addTag($staffId,$weibanExtid,'省份',$province);
  124. if( $cityName ) OpenApi::addTag($staffId,$weibanExtid,$province.'城市',$cityName);
  125. if( $shopType ) OpenApi::addTag($staffId,$weibanExtid,'企业类型',$shopType);
  126. // 返回结果
  127. return ['success'=>'返回结果'];
  128. }
  129. /**
  130. * 修改 /api/custom_addr/edit
  131. *
  132. * @param string $code 授权码
  133. *
  134. * */
  135. public function edit(Request $request,Model $Model,Custom $Custom,City $City){
  136. // 接口验签
  137. // $this->verify_sign();
  138. // 检查登录
  139. $uid = $this->checkLogin();
  140. // 验证参数
  141. $request->scene('edit')->validate();
  142. // 接收参数
  143. $id = request('id',0);
  144. $data['contact_province'] = trim(request('contact_province',''));
  145. $data['contact_city'] = trim(request('contact_city',''));
  146. $data['contact_area'] = trim(request('contact_area',''));
  147. $data['contact_addr'] = trim(request('contact_addr',''));
  148. $data['contact_name'] = trim(request('contact_name',''));
  149. $data['contact_shop'] = trim(request('contact_shop',''));
  150. $data['shop_type'] = trim(request('shop_type',0));
  151. $data['contact_phone'] = trim(request('contact_phone',''));
  152. $data['is_default'] = request('is_default',0);
  153. // 获取客户城市ID
  154. $cityId = (int) $Custom->getValue($uid,'city_id');
  155. $cityName = $City->getOne($cityId,'name');
  156. $pid = $City->getOne($cityId,'pid');
  157. // 如果上级不是省份
  158. if( strlen($cityId) > 4 ) $pid = (int) $City->getOne($pid,'pid');
  159. $province = $City->getOne($pid,'name');
  160. // 如果不是海南,
  161. if( $province != '海南省' ) {
  162. // 判断选择的城市名称是不是一致
  163. if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.($province=='直辖县级'?$cityName:$province).'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  164. }else{
  165. if( trim($data['contact_province']) != trim($province) ) return json_send(['code'=>'error','msg'=>'收货地址请选择海南范围','data'=>['error'=>'收货地址需与您所选城市一致']]);
  166. }
  167. // 替换地址中间的空格
  168. $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
  169. // 如果需要默认当前的话
  170. if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  171. // 查询是否已经提交过
  172. $result = $Model->edit($id,$data);
  173. // 如果用户状态被拉黑,不允许登录
  174. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  175. // 返回结果
  176. return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  177. }
  178. /**
  179. * 删除 /api/custom_addr/del
  180. *
  181. * @param string $code 授权码
  182. *
  183. * */
  184. public function del(Request $request,Model $Model){
  185. // 接口验签
  186. // $this->verify_sign();
  187. // 检查登录
  188. $uid = $this->checkLogin();
  189. // 验证参数
  190. $request->scene('del')->validate();
  191. // 接收参数
  192. $id = request('id',0);
  193. // 如果需要默认当前的话
  194. $result = $Model->del($id);
  195. // 如果用户状态被拉黑,不允许登录
  196. if( !$result ) return json_send(['code'=>'success','msg'=>'删除失败','data'=>['error'=>'删除失败']]);
  197. // 返回结果
  198. return json_send(['code'=>'success','msg'=>'删除成功','data'=>['id'=>$id]]);
  199. }
  200. /**
  201. * 修改 /api/custom_addr/set_default
  202. *
  203. * @param string $code 授权码
  204. *
  205. * */
  206. public function set_default(Request $request,Model $Model){
  207. // 接口验签
  208. // $this->verify_sign();
  209. // 检查登录
  210. $uid = $this->checkLogin();
  211. // 验证参数
  212. $request->scene('set_default')->validate();
  213. // 接收参数
  214. $id = request('id',0);
  215. // 如果需要默认当前的话
  216. $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  217. // 查询是否已经提交过
  218. $result = $Model->edit($id,['is_default'=>1]);
  219. // 如果用户状态被拉黑,不允许登录
  220. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  221. // 返回结果
  222. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['id'=>$id]]);
  223. }
  224. /**
  225. * 获取用户地址信息 /api/custom_addr/get_remark_addr
  226. *
  227. */
  228. public function get_remark_addr(Custom $Custom,City $City,WeiBanFollow $WeiBanFollow){
  229. // 接口验签
  230. // $this->verify_sign();
  231. // 检查登录
  232. $uid = $this->checkLogin();
  233. // 获取客户城市ID
  234. $custom = $Custom->getOne($uid);
  235. // 数据组合
  236. $data = ['contact_province'=>'','contact_city'=>'','contact_area'=>'','contact_addr'=>'','contact_name'=>'','contact_shop'=>'','contact_phone'=>'','shop_type'=>1];
  237. // 如果不存在用户信息
  238. if( !$custom ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  239. // 省份
  240. $data['contact_phone'] = $custom['phone'];
  241. // 获取用户城市信息
  242. $city = $City->getOne($custom['city_id']);
  243. // 如果有城市信息
  244. if( !$city ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  245. // 获取上级城市/省份
  246. $parentCity = $City->getOne($city['pid']);
  247. // 如果上级是省辖,获取省份
  248. if( isset($parentCity['name']) && $parentCity['name'] == '直辖县级' ) $parentCity = $City->getOne($parentCity['pid']);
  249. // 如果有获取到省份信息
  250. if( !$parentCity ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  251. // 省份
  252. $data['contact_province'] = $parentCity['name'];
  253. // 省份
  254. $data['contact_city'] = $city['name'];
  255. // 如果用户没有绑定对应的微伴账号
  256. if( !$custom['weiban_extid'] ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  257. // 获取客服对应的备注等信息
  258. $follow = $WeiBanFollow->getListByWeibanExtid($custom['weiban_extid']);
  259. // 获取备注列表
  260. $remark = array_filter(array_column($follow,'remark'),function($value) {return !empty($value);});
  261. // 获取备注列表
  262. $contactShop = array_filter(array_column($follow,'remark_corp_name'),function($value) {return !empty($value);});
  263. // 如果有备注信息
  264. if( isset($remark[0]) ) $data['contact_name'] = $remark[0];
  265. // 如果有备注信息
  266. if( isset($contactShop[0]) ) $data['contact_shop'] = $contactShop[0];
  267. // 返回结果
  268. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  269. }
  270. /**
  271. * 获取可能性地址 /api/custom_addr/get_guess_addr
  272. *
  273. */
  274. public function get_guess_addr(Request $request,Model $Model){
  275. // 接口验签
  276. // $this->verify_sign();
  277. // 检查登录
  278. $uid = $this->checkLogin();
  279. // 接收参数
  280. $request->scene('get_guess_addr')->validate();
  281. // 接收参数
  282. $contactShop = request('contact_shop','');
  283. // 获取参数
  284. $map = [['contact_shop','LIKE','%'.$contactShop.'%']];
  285. // 查询
  286. $list = $Model->query()->where($map)->limit('20')->get(['contact_province','contact_city','contact_area','contact_addr','contact_shop','shop_type'])->toArray();
  287. // 返回结果
  288. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$list]);
  289. }
  290. }