UserAddress.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\user;
  12. use think\App;
  13. use crmeb\basic\BaseController;
  14. use app\validate\api\UserAddressValidate as validate;
  15. use app\common\repositories\user\UserAddressRepository as repository;
  16. class UserAddress extends BaseController
  17. {
  18. /**
  19. * @var repository
  20. */
  21. protected $repository;
  22. /**
  23. * UserAddress constructor.
  24. * @param App $app
  25. * @param repository $repository
  26. */
  27. public function __construct(App $app, repository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. public function lst()
  33. {
  34. return app('json')->success($this->repository->getList($this->request->uid()));
  35. }
  36. public function detail($id)
  37. {
  38. return app('json')->success($this->repository->get($id,$this->request->uid()));
  39. }
  40. /**
  41. * @param validate $validate
  42. * @return mixed
  43. * @author Qinii
  44. */
  45. public function create(validate $validate)
  46. {
  47. $data = $this->checkParams($validate);
  48. if($data['is_default']){
  49. $this->repository->changeDefault($this->request->uid());
  50. } else {
  51. if(!$this->repository->defaultExists($this->request->uid()))$data['is_default'] = 1;
  52. }
  53. $city_id = $this->repository->getCityId($data['province'],$data['city']);
  54. if(!$city_id)return app('json')->fail('未查询到城市ID');
  55. $data['city_id'] = $city_id;
  56. if($data['address_id']){
  57. if(!$this->repository->fieldExists($data['address_id'],$this->request->uid()))
  58. return app('json')->fail('信息不存在');
  59. $this->repository->update($data['address_id'],$data);
  60. return app('json')->success('编辑成功');
  61. };
  62. $data['uid'] = $this->request->uid();
  63. $address = $this->repository->create($data);
  64. return app('json')->success('添加成功', $address->toArray());
  65. }
  66. /**
  67. * @param $id
  68. * @param validate $validate
  69. * @return mixed
  70. * @author Qinii
  71. */
  72. public function update($id,validate $validate)
  73. {
  74. if(!$this->repository->fieldExists($id,$this->request->uid()))
  75. return app('json')->fail('信息不存在');
  76. $data = $this->checkParams($validate);
  77. if($data['is_default']) $this->repository->changeDefault($this->request->uid());
  78. $this->repository->update($id,$data);
  79. return app('json')->success('编辑成功');
  80. }
  81. /**
  82. * @param $id
  83. * @return mixed
  84. * @author Qinii
  85. */
  86. public function delete($id)
  87. {
  88. if(!$this->repository->fieldExists($id,$this->request->uid()))
  89. return app('json')->fail('信息不存在');
  90. if($this->repository->checkDefault($id))
  91. return app('json')->fail('默认地址不能删除');
  92. $this->repository->delete($id);
  93. return app('json')->success('删除成功');
  94. }
  95. public function editDefault($id)
  96. {
  97. if(!$this->repository->fieldExists($id,$this->request->uid()))
  98. return app('json')->fail('信息不存在');
  99. $this->repository->changeDefault($this->request->uid());
  100. $this->repository->update($id,['is_default' => 1]);
  101. return app('json')->success('修改成功');
  102. }
  103. /**
  104. * @param validate $validate
  105. * @return array
  106. * @author Qinii
  107. */
  108. public function checkParams(validate $validate)
  109. {
  110. $data = $this->request->params(['address_id','real_name','phone','province','city','city_id','district','detail','post_code','is_default']);
  111. $validate->check($data);
  112. return $data;
  113. }
  114. }