Address.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $arrReturn = [];
  38. if ($row) {
  39. $arrReturn = $row->toArray();
  40. }
  41. $this->success('获取成功', $arrReturn);
  42. }
  43. //添加(编辑)地址
  44. public function addedit()
  45. {
  46. $id = $this->request->post('id');
  47. $address = $this->request->post('address');
  48. $area_id = $this->request->post('area_id');
  49. $isdefault = $this->request->post('is_default');
  50. $mobile = $this->request->post('mobile');
  51. $receiver = $this->request->post('receiver');
  52. // 增加 验证器
  53. $validate = new \app\api\validate\Address();
  54. $data_to_validate = [
  55. 'receiver' => $receiver,
  56. 'mobile' => $mobile,
  57. 'address' => $address,
  58. 'area_id' => $area_id,
  59. 'is_default' => $isdefault,
  60. ];
  61. // 验证数据
  62. $scene = $id ? 'edit' : 'add';
  63. if (!$validate->scene($scene)->check($data_to_validate)) {
  64. $this->error($validate->getError());
  65. }
  66. $area = Area::field('a.id,a.zipcode,c.id as city_id,p.id as province_id')
  67. ->alias('a')
  68. ->join('shop_area c', 'a.pid=c.id')
  69. ->join('shop_area p', 'c.pid=p.id')
  70. ->where('a.id', $area_id)
  71. ->where('a.level', 3)
  72. ->find();
  73. if (!$area) {
  74. $this->error('未找到地区记录');
  75. }
  76. if (!$area['city_id'] || !$area['province_id']) {
  77. $this->error('地址错误');
  78. }
  79. $data = [
  80. 'address' => $address,
  81. 'is_default' => $isdefault,
  82. 'mobile' => $mobile,
  83. 'receiver' => $receiver,
  84. 'area_id' => $area_id,
  85. 'user_id' => $this->auth->id,
  86. 'city_id' => $area['city_id'],
  87. 'province_id' => $area['province_id'],
  88. 'zipcode' => $area['zipcode'],
  89. 'status' => StatusEnum::ENABLED
  90. ];
  91. if ($id) { //编辑
  92. $row = AddressModel::where('id', $id)
  93. ->where('user_id', $this->auth->id)
  94. ->find();
  95. if (!$row) {
  96. $this->error('未找到记录');
  97. }
  98. $row->save($data);
  99. } else { //添加
  100. (new AddressModel)->save($data);
  101. }
  102. $this->success('操作成功');
  103. }
  104. //删除地址
  105. public function del()
  106. {
  107. $id = $this->request->post('id');
  108. if (!$id) {
  109. $this->error('参数错误');
  110. }
  111. $row = AddressModel::where('user_id', $this->auth->id)->where('id', $id)->find();
  112. if (!$row) {
  113. $this->error('未找到记录');
  114. }
  115. $row->delete();
  116. $this->success('删除成功');
  117. }
  118. }