Address.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\common\validate\IDMustBePositiveInt;
  5. use Lettered\Support\Auth as IAuth;
  6. use think\App;
  7. class Address extends ApiController
  8. {
  9. /**
  10. * 地址列表
  11. *
  12. * @author 许祖兴 < zuxing.xu@lettered.cn>
  13. * @date 2020/6/30 16:45
  14. *
  15. * @return \think\response\Json
  16. * @throws \Lettered\Support\Exceptions\FailedException
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function index(){
  22. $model = model('common/UsersAddr')
  23. ->where(['user_id' => $this->auth->user()->id]);
  24. // 加载默认数据
  25. if (input('default') != ""){
  26. $model->where(['default' => 1]);
  27. return $this->ApiJson(0,'OK!', $model->find());
  28. }
  29. $addr = $model->order('default','desc')->select();
  30. return $this->ApiJson(0,'OK!', $addr);
  31. }
  32. /**
  33. * 新增数据
  34. *
  35. * @author 许祖兴 < zuxing.xu@lettered.cn>
  36. * @date 2020/7/7 18:27
  37. *
  38. * @return \think\response\Json
  39. * @throws \Lettered\Support\Exceptions\FailedException
  40. */
  41. public function save()
  42. {
  43. // 接收数据
  44. $params = $this->request->param();
  45. // 数据校验
  46. $valid = $this->validate($params, [
  47. 'name|姓名' => 'require',
  48. 'mobile|手机号' => 'require',
  49. 'province|省份' => 'require',
  50. 'city|城市' => 'require',
  51. 'country|县区' => 'require',
  52. 'detail|地址详细信息' => 'require',
  53. ]);
  54. // 错误返回
  55. if(true !== $valid){
  56. return $this->ApiJson(-1, $valid);
  57. };
  58. // 追加数据
  59. $params['user_id'] = $this->auth->user()['id'];
  60. $params['default'] = $params['default'] == 'true' ? 1 : 0;
  61. $ret = model('common/UsersAddr')->storeBy($params);
  62. if ($ret){
  63. // 默认处理
  64. if ($params['default']){
  65. model('common/UsersAddr')
  66. ->where(['user_id' => $this->auth->user()['id']])
  67. ->where('id','<>' , $ret)
  68. ->update(['default' => 0]);
  69. }
  70. return $this->ApiJson(0,"新增成功");
  71. }
  72. return $this->ApiJson(-1,"数据异常,请稍后重试");
  73. }
  74. /**
  75. * 地址更新
  76. *
  77. * @author 许祖兴 < zuxing.xu@lettered.cn>
  78. * @date 2020/7/1 8:50
  79. *
  80. * @param $id
  81. * @return \think\response\Json
  82. * @throws \Lettered\Support\Exceptions\FailedException
  83. * @throws \think\Exception
  84. * @throws \think\exception\PDOException
  85. */
  86. public function update($id)
  87. {
  88. (new IDMustBePositiveInt())->valid();
  89. // 接收数据
  90. $params = $this->request->param();
  91. // 查询
  92. $addr = model('common/UsersAddr')->findBy($id);
  93. // 数据校验
  94. $valid = $this->validate($params, [
  95. 'name|姓名' => 'require',
  96. 'mobile|手机号' => 'require',
  97. 'province|省份' => 'require',
  98. 'city|城市' => 'require',
  99. 'country|县区' => 'require',
  100. 'detail|地址详细信息' => 'require',
  101. ]);
  102. // 错误返回
  103. if(true !== $valid){
  104. return $this->ApiJson(-1, $valid);
  105. };
  106. $params['default'] = $params['default'] == 'true' ? 1 : 0;
  107. // 更新
  108. $ret = $addr->updateBy($id, $params);
  109. // 默认处理
  110. if ($params['default'] == 1 && $params['default'] != $addr['default']){
  111. model('common/UsersAddr')
  112. ->where(['user_id' => $this->auth->user()['id']])
  113. ->where('id','<>' , $id)
  114. ->update(['default' => 0]);
  115. }
  116. if ($ret){
  117. return $this->ApiJson(0,"更新成功");
  118. }
  119. return $this->ApiJson(-1,"数据异常,请稍后重试");
  120. }
  121. /**
  122. * 删除地址
  123. *
  124. * @author 许祖兴 < zuxing.xu@lettered.cn>
  125. * @date 2020/7/2 11:32
  126. *
  127. * @param $id
  128. * @return \think\response\Json
  129. * @throws \Lettered\Support\Exceptions\EvidentException
  130. */
  131. public function delete($id)
  132. {
  133. (new IDMustBePositiveInt())->valid();
  134. $ret = model('common/UsersAddr')->deleteBy($id);
  135. if ($ret){
  136. return $this->ApiJson(0,"删除地址成功");
  137. }
  138. return $this->ApiJson(-1,"数据异常,请稍后重试");
  139. }
  140. }