CustomAddr.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  8. * 客户地址
  9. *
  10. * @author 刘相欣
  11. *
  12. * */
  13. class CustomAddr extends Api{
  14. /**
  15. * 获取列表 /api/custom_addr/get_list
  16. *
  17. * @param string $code 授权码
  18. *
  19. * */
  20. public function get_list(Model $Model){
  21. // 接口验签
  22. // $this->verify_sign();
  23. // 检查登录
  24. $uid = $this->checkLogin();
  25. // 查询信息
  26. $list = $Model->getListByCustom($uid);
  27. // 返回结果
  28. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$list]);
  29. }
  30. /**
  31. * 添加 /api/custom_addr/add
  32. *
  33. * @param string $code 授权码
  34. *
  35. * */
  36. public function add(Request $request,Model $Model,Custom $Custom,City $City){
  37. // 接口验签
  38. // $this->verify_sign();
  39. // 验证参数
  40. $request->scene('add')->validate();
  41. // 检查登录
  42. $uid = $this->checkLogin();
  43. // 接收参数
  44. $data['contact_province'] = trim(request('contact_province',''));
  45. $data['contact_city'] = trim(request('contact_city',''));
  46. $data['contact_area'] = trim(request('contact_area',''));
  47. $data['contact_addr'] = trim(request('contact_addr',''));
  48. $data['contact_name'] = trim(request('contact_name',''));
  49. $data['contact_shop'] = trim(request('contact_shop',''));
  50. $data['shop_type'] = trim(request('shop_type',0));
  51. $data['contact_phone'] = trim(request('contact_phone',''));
  52. $data['is_default'] = request('is_default',0);
  53. $data['custom_uid'] = $uid;
  54. // 获取客户城市ID
  55. $cityId = (int) $Custom->getValue($uid,'city_id');
  56. $cityName = $City->getOne($cityId,'name');
  57. $pid = $City->getOne($cityId,'pid');
  58. $province = $City->getOne($pid,'name');
  59. // 判断选择的城市名称是不是一致
  60. if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.$province.'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  61. // 替换地址中间的空格
  62. $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
  63. // 最大数量
  64. $limitMax = 20;
  65. $havaNum = $Model->query()->where([['custom_uid','=',$uid]])->count();
  66. // 如果是最大数量的话
  67. if( $havaNum > $limitMax ) return json_send(['code'=>'error','msg'=>'您已经有'.$limitMax.'条地址了','data'=>['error'=>'最多只能保20条地址']]);
  68. // 如果需要默认当前的话
  69. if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  70. // 查询是否已经提交过
  71. $result = $Model->add($data);
  72. // 如果用户状态被拉黑,不允许登录
  73. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  74. // 返回结果
  75. return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  76. }
  77. /**
  78. * 修改 /api/custom_addr/edit
  79. *
  80. * @param string $code 授权码
  81. *
  82. * */
  83. public function edit(Request $request,Model $Model,Custom $Custom,City $City){
  84. // 接口验签
  85. // $this->verify_sign();
  86. // 检查登录
  87. $uid = $this->checkLogin();
  88. // 验证参数
  89. $request->scene('edit')->validate();
  90. // 接收参数
  91. $id = request('id',0);
  92. $data['contact_province'] = trim(request('contact_province',''));
  93. $data['contact_city'] = trim(request('contact_city',''));
  94. $data['contact_area'] = trim(request('contact_area',''));
  95. $data['contact_addr'] = trim(request('contact_addr',''));
  96. $data['contact_name'] = trim(request('contact_name',''));
  97. $data['contact_shop'] = trim(request('contact_shop',''));
  98. $data['shop_type'] = trim(request('shop_type',0));
  99. $data['contact_phone'] = trim(request('contact_phone',''));
  100. $data['is_default'] = request('is_default',0);
  101. // 获取客户城市ID
  102. $cityId = (int) $Custom->getValue($uid,'city_id');
  103. $cityName = $City->getOne($cityId,'name');
  104. $pid = $City->getOne($cityId,'pid');
  105. $province = $City->getOne($pid,'name');
  106. // 判断选择的城市名称是不是一致
  107. if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.$province.'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  108. // 替换地址中间的空格
  109. $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
  110. // 如果需要默认当前的话
  111. if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  112. // 查询是否已经提交过
  113. $result = $Model->edit($id,$data);
  114. // 如果用户状态被拉黑,不允许登录
  115. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  116. // 返回结果
  117. return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
  118. }
  119. /**
  120. * 删除 /api/custom_addr/del
  121. *
  122. * @param string $code 授权码
  123. *
  124. * */
  125. public function del(Request $request,Model $Model){
  126. // 接口验签
  127. // $this->verify_sign();
  128. // 检查登录
  129. $uid = $this->checkLogin();
  130. // 验证参数
  131. $request->scene('del')->validate();
  132. // 接收参数
  133. $id = request('id',0);
  134. // 如果需要默认当前的话
  135. $result = $Model->del($id);
  136. // 如果用户状态被拉黑,不允许登录
  137. if( !$result ) return json_send(['code'=>'success','msg'=>'删除失败','data'=>['error'=>'删除失败']]);
  138. // 返回结果
  139. return json_send(['code'=>'success','msg'=>'删除成功','data'=>['id'=>$id]]);
  140. }
  141. /**
  142. * 修改 /api/custom_addr/set_default
  143. *
  144. * @param string $code 授权码
  145. *
  146. * */
  147. public function set_default(Request $request,Model $Model){
  148. // 接口验签
  149. // $this->verify_sign();
  150. // 检查登录
  151. $uid = $this->checkLogin();
  152. // 验证参数
  153. $request->scene('set_default')->validate();
  154. // 接收参数
  155. $id = request('id',0);
  156. // 如果需要默认当前的话
  157. $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
  158. // 查询是否已经提交过
  159. $result = $Model->edit($id,['is_default'=>1]);
  160. // 如果用户状态被拉黑,不允许登录
  161. if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
  162. // 返回结果
  163. return json_send(['code'=>'success','msg'=>'保存成功','data'=>['id'=>$id]]);
  164. }
  165. }