Address.php 3.1 KB

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