CustomAddr.php 6.4 KB

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