Address.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $areainfo = $row->areainfo;
  29. $row['province'] = $areainfo['province'] ? $areainfo['province']['name'] : '';
  30. $row['city'] = $areainfo['city'] ? $areainfo['city']['name'] : '';
  31. $row['area'] = $areainfo['area'] ? $areainfo['area']['name'] : '';
  32. $this->success('获取成功', $row);
  33. }
  34. /**
  35. * @ 默认地址
  36. */
  37. public function def_address()
  38. {
  39. $row = AddressModel::where('user_id', $this->auth->id)->where('isdefault', 1)->find();
  40. if (!$row) {
  41. $this->error('未找到记录');
  42. }
  43. $this->success('获取成功', $row);
  44. }
  45. //添加(编辑)地址
  46. public function addedit()
  47. {
  48. $id = $this->request->post('id');
  49. $address = $this->request->post('address');
  50. $area_id = $this->request->post('area_id');
  51. $isdefault = $this->request->post('isdefault');
  52. $mobile = $this->request->post('mobile');
  53. $receiver = $this->request->post('receiver');
  54. $area = Area::field('a.id,a.zipcode,c.id as city_id,p.id as province_id')
  55. ->alias('a')
  56. ->join('shop_area c', 'a.pid=c.id')
  57. ->join('shop_area p', 'c.pid=p.id')
  58. ->where('a.id', $area_id)
  59. ->where('a.level', 3)
  60. ->find();
  61. if (!$area) {
  62. $this->error('未找到地区记录');
  63. }
  64. if (!$area['city_id'] || !$area['province_id']) {
  65. $this->error('地址错误');
  66. }
  67. $data = [
  68. 'address' => $address,
  69. 'isdefault' => $isdefault,
  70. 'mobile' => $mobile,
  71. 'receiver' => $receiver,
  72. 'area_id' => $area_id,
  73. 'user_id' => $this->auth->id,
  74. 'city_id' => $area['city_id'],
  75. 'province_id' => $area['province_id'],
  76. 'zipcode' => $area['zipcode'],
  77. 'status' => 'normal'
  78. ];
  79. if ($id) { //编辑
  80. $row = AddressModel::where('id', $id)->where('user_id', $this->auth->id)->find();
  81. if (!$row) {
  82. $this->error('未找到记录');
  83. }
  84. $row->save($data);
  85. } else { //添加
  86. (new AddressModel)->save($data);
  87. }
  88. $this->success('操作成功');
  89. }
  90. //删除地址
  91. public function del()
  92. {
  93. $id = $this->request->post('id');
  94. if (!$id) {
  95. $this->error('参数错误');
  96. }
  97. $row = AddressModel::where('user_id', $this->auth->id)->where('id', $id)->find();
  98. if (!$row) {
  99. $this->error('未找到记录');
  100. }
  101. $row->delete();
  102. $this->success('删除成功');
  103. }
  104. }