CustomAddr.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /**
  9. * 客户地址
  10. *
  11. * @author 刘相欣
  12. *
  13. * */
  14. class CustomAddr extends Api{
  15. /**
  16. * 获取列表 /api/custom_addr/get_list
  17. *
  18. * @param string $code 授权码
  19. *
  20. * */
  21. public function get_list(Model $Model){
  22. // 接口验签
  23. // $this->verify_sign();
  24. // 检查登录
  25. $uid = $this->checkLogin();
  26. // 查询信息
  27. $list = $Model->getListByCustom($uid);
  28. // 返回结果
  29. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$list]);
  30. }
  31. /**
  32. * 添加 /api/custom_addr/add
  33. *
  34. * @param string $code 授权码
  35. *
  36. * */
  37. public function add(Request $request,Model $Model,Custom $Custom,City $City){
  38. // 接口验签
  39. // $this->verify_sign();
  40. // 验证参数
  41. $request->scene('add')->validate();
  42. // 检查登录
  43. $uid = $this->checkLogin();
  44. // 接收参数
  45. $data['contact_province'] = trim(request('contact_province',''));
  46. $data['contact_city'] = trim(request('contact_city',''));
  47. $data['contact_area'] = trim(request('contact_area',''));
  48. $data['contact_addr'] = trim(request('contact_addr',''));
  49. $data['contact_name'] = trim(request('contact_name',''));
  50. $data['contact_shop'] = trim(request('contact_shop',''));
  51. $data['shop_type'] = trim(request('shop_type',0));
  52. $data['contact_phone'] = trim(request('contact_phone',''));
  53. $data['is_default'] = request('is_default',0);
  54. $data['custom_uid'] = $uid;
  55. // 获取客户城市ID
  56. $cityId = (int) $Custom->getValue($uid,'city_id');
  57. $cityName = $City->getOne($cityId,'name');
  58. $pid = $City->getOne($cityId,'pid');
  59. $province = $City->getOne($pid,'name');
  60. // 判断选择的城市名称是不是一致
  61. if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.($province=='直辖县级'?$cityName:$province).'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  62. // 替换地址中间的空格
  63. $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
  64. // 最大数量
  65. $limitMax = 20;
  66. $havaNum = $Model->query()->where([['custom_uid','=',$uid]])->count();
  67. // 如果是最大数量的话
  68. if( $havaNum > $limitMax ) return json_send(['code'=>'error','msg'=>'您已经有'.$limitMax.'条地址了','data'=>['error'=>'最多只能保20条地址']]);
  69. // 如果需要默认当前的话
  70. if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  71. // 查询是否已经提交过
  72. $result = $Model->add($data);
  73. // 如果用户状态被拉黑,不允许登录
  74. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  75. // 返回结果
  76. return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  77. }
  78. /**
  79. * 修改 /api/custom_addr/edit
  80. *
  81. * @param string $code 授权码
  82. *
  83. * */
  84. public function edit(Request $request,Model $Model,Custom $Custom,City $City){
  85. // 接口验签
  86. // $this->verify_sign();
  87. // 检查登录
  88. $uid = $this->checkLogin();
  89. // 验证参数
  90. $request->scene('edit')->validate();
  91. // 接收参数
  92. $id = request('id',0);
  93. $data['contact_province'] = trim(request('contact_province',''));
  94. $data['contact_city'] = trim(request('contact_city',''));
  95. $data['contact_area'] = trim(request('contact_area',''));
  96. $data['contact_addr'] = trim(request('contact_addr',''));
  97. $data['contact_name'] = trim(request('contact_name',''));
  98. $data['contact_shop'] = trim(request('contact_shop',''));
  99. $data['shop_type'] = trim(request('shop_type',0));
  100. $data['contact_phone'] = trim(request('contact_phone',''));
  101. $data['is_default'] = request('is_default',0);
  102. // 获取客户城市ID
  103. $cityId = (int) $Custom->getValue($uid,'city_id');
  104. $cityName = $City->getOne($cityId,'name');
  105. $pid = $City->getOne($cityId,'pid');
  106. $province = $City->getOne($pid,'name');
  107. // 判断选择的城市名称是不是一致
  108. if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.($province=='直辖县级'?$cityName:$province).'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  109. // 替换地址中间的空格
  110. $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
  111. // 如果需要默认当前的话
  112. if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  113. // 查询是否已经提交过
  114. $result = $Model->edit($id,$data);
  115. // 如果用户状态被拉黑,不允许登录
  116. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  117. // 返回结果
  118. return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  119. }
  120. /**
  121. * 删除 /api/custom_addr/del
  122. *
  123. * @param string $code 授权码
  124. *
  125. * */
  126. public function del(Request $request,Model $Model){
  127. // 接口验签
  128. // $this->verify_sign();
  129. // 检查登录
  130. $uid = $this->checkLogin();
  131. // 验证参数
  132. $request->scene('del')->validate();
  133. // 接收参数
  134. $id = request('id',0);
  135. // 如果需要默认当前的话
  136. $result = $Model->del($id);
  137. // 如果用户状态被拉黑,不允许登录
  138. if( !$result ) return json_send(['code'=>'success','msg'=>'删除失败','data'=>['error'=>'删除失败']]);
  139. // 返回结果
  140. return json_send(['code'=>'success','msg'=>'删除成功','data'=>['id'=>$id]]);
  141. }
  142. /**
  143. * 修改 /api/custom_addr/set_default
  144. *
  145. * @param string $code 授权码
  146. *
  147. * */
  148. public function set_default(Request $request,Model $Model){
  149. // 接口验签
  150. // $this->verify_sign();
  151. // 检查登录
  152. $uid = $this->checkLogin();
  153. // 验证参数
  154. $request->scene('set_default')->validate();
  155. // 接收参数
  156. $id = request('id',0);
  157. // 如果需要默认当前的话
  158. $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  159. // 查询是否已经提交过
  160. $result = $Model->edit($id,['is_default'=>1]);
  161. // 如果用户状态被拉黑,不允许登录
  162. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  163. // 返回结果
  164. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['id'=>$id]]);
  165. }
  166. /**
  167. * 获取用户地址信息 /api/custom_addr/get_remark_addr
  168. *
  169. */
  170. public function get_remark_addr(Custom $Custom,City $City,WeiBanFollow $WeiBanFollow){
  171. // 接口验签
  172. // $this->verify_sign();
  173. // 检查登录
  174. $uid = $this->checkLogin();
  175. // 获取客户城市ID
  176. $custom = $Custom->getOne($uid);
  177. // 数据组合
  178. $data = ['contact_province'=>'','contact_city'=>'','contact_area'=>'','contact_addr'=>'','contact_name'=>'','contact_shop'=>'','contact_phone'=>'','shop_type'=>1];
  179. // 如果不存在用户信息
  180. if( !$custom ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  181. // 省份
  182. $data['contact_phone'] = $custom['phone'];
  183. // 获取用户城市信息
  184. $city = $City->getOne($custom['city_id']);
  185. // 如果有城市信息
  186. if( !$city ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  187. // 获取上级城市/省份
  188. $parentCity = $City->getOne($city['pid']);
  189. // 如果上级是省辖,获取省份
  190. if( isset($parentCity['name']) && $parentCity['name'] == '直辖县级' ) $parentCity = $City->getOne($parentCity['pid']);
  191. // 如果有获取到省份信息
  192. if( !$parentCity ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  193. // 省份
  194. $data['contact_province'] = $parentCity['name'];
  195. // 省份
  196. $data['contact_city'] = $city['name'];
  197. // 如果用户没有绑定对应的微伴账号
  198. if( !$custom['weiban_extid'] ) return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  199. // 获取客服对应的备注等信息
  200. $follow = $WeiBanFollow->getListByWeibanExtid($custom['weiban_extid']);
  201. // 获取备注列表
  202. $remark = array_filter(array_column($follow,'remark'),function($value) {return !empty($value);});
  203. // 获取备注列表
  204. $contactShop = array_filter(array_column($follow,'remark_corp_name'),function($value) {return !empty($value);});
  205. // 如果有备注信息
  206. if( isset($remark[0]) ) $data['contact_name'] = $remark[0];
  207. // 如果有备注信息
  208. if( isset($contactShop[0]) ) $data['contact_shop'] = $contactShop[0];
  209. // 返回结果
  210. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  211. }
  212. /**
  213. * 获取可能性地址 /api/custom_addr/get_guess_addr
  214. *
  215. */
  216. public function get_guess_addr(Request $request,Model $Model){
  217. // 接口验签
  218. // $this->verify_sign();
  219. // 检查登录
  220. $uid = $this->checkLogin();
  221. // 接收参数
  222. $request->scene('get_guess_addr')->validate();
  223. // 接收参数
  224. $contactShop = request('contact_shop','');
  225. // 获取参数
  226. $map = [['contact_shop','LIKE','%'.$contactShop.'%']];
  227. // 查询
  228. $list = $Model->query()->where($map)->limit('20')->get(['contact_province','contact_city','contact_area','contact_addr','contact_shop','shop_type'])->toArray();
  229. // 返回结果
  230. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$list]);
  231. }
  232. }