Address.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Address as AddressModel;
  4. use app\common\model\Area;
  5. use app\common\Enum\StatusEnum;
  6. /**
  7. * 地址
  8. */
  9. class Address extends Base
  10. {
  11. protected $noNeedLogin = [];
  12. //地址列表
  13. public function index()
  14. {
  15. $list = AddressModel::getAddressList($this->auth->id);
  16. $this->success('', $list);
  17. }
  18. //地址详情
  19. public function detail()
  20. {
  21. $id = $this->request->param('id');
  22. if (!$id) {
  23. $this->error('参数错误');
  24. }
  25. $row = AddressModel::where('user_id', $this->auth->id)->where('id', $id)->find();
  26. if (!$row) {
  27. $this->error('未找到记录');
  28. }
  29. $this->success('获取成功', $row);
  30. }
  31. /**
  32. * @ 默认地址
  33. */
  34. public function def_address()
  35. {
  36. $row = AddressModel::where('user_id', $this->auth->id)->where('is_default', 1)->find();
  37. if (!$row) {
  38. $this->error('未找到记录');
  39. }
  40. $this->success('获取成功', $row);
  41. }
  42. //添加(编辑)地址
  43. public function addedit()
  44. {
  45. $id = $this->request->post('id');
  46. $address = $this->request->post('address');
  47. $area_id = $this->request->post('area_id');
  48. $isdefault = $this->request->post('is_default');
  49. $mobile = $this->request->post('mobile');
  50. $receiver = $this->request->post('receiver');
  51. // 增加 验证器
  52. $validate = new \app\api\validate\Address();
  53. $data_to_validate = [
  54. 'receiver' => $receiver,
  55. 'mobile' => $mobile,
  56. 'address' => $address,
  57. 'area_id' => $area_id,
  58. 'is_default' => $isdefault,
  59. ];
  60. // 验证数据
  61. $scene = $id ? 'edit' : 'add';
  62. if (!$validate->scene($scene)->check($data_to_validate)) {
  63. $this->error($validate->getError());
  64. }
  65. $area = Area::field('a.id,a.zipcode,c.id as city_id,p.id as province_id')
  66. ->alias('a')
  67. ->join('shop_area c', 'a.pid=c.id')
  68. ->join('shop_area p', 'c.pid=p.id')
  69. ->where('a.id', $area_id)
  70. ->where('a.level', 3)
  71. ->find();
  72. if (!$area) {
  73. $this->error('未找到地区记录');
  74. }
  75. if (!$area['city_id'] || !$area['province_id']) {
  76. $this->error('地址错误');
  77. }
  78. $data = [
  79. 'address' => $address,
  80. 'is_default' => $isdefault,
  81. 'mobile' => $mobile,
  82. 'receiver' => $receiver,
  83. 'area_id' => $area_id,
  84. 'user_id' => $this->auth->id,
  85. 'city_id' => $area['city_id'],
  86. 'province_id' => $area['province_id'],
  87. 'zipcode' => $area['zipcode'],
  88. 'status' => StatusEnum::ENABLED
  89. ];
  90. if ($id) { //编辑
  91. $row = AddressModel::where('id', $id)
  92. ->where('user_id', $this->auth->id)
  93. ->find();
  94. if (!$row) {
  95. $this->error('未找到记录');
  96. }
  97. $row->save($data);
  98. } else { //添加
  99. (new AddressModel)->save($data);
  100. }
  101. $this->success('操作成功');
  102. }
  103. //删除地址
  104. public function del()
  105. {
  106. $id = $this->request->post('id');
  107. if (!$id) {
  108. $this->error('参数错误');
  109. }
  110. $row = AddressModel::where('user_id', $this->auth->id)->where('id', $id)->find();
  111. if (!$row) {
  112. $this->error('未找到记录');
  113. }
  114. $row->delete();
  115. $this->success('删除成功');
  116. }
  117. }