Address.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace addons\shopro\controller\user;
  3. use think\Db;
  4. use addons\shopro\controller\Common;
  5. use app\admin\model\shopro\data\Area as AreaModel;
  6. use app\admin\model\shopro\user\Address as UserAddressModel;
  7. class Address extends Common
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. public function index()
  12. {
  13. $user = auth_user();
  14. $userAddresses = UserAddressModel::where('user_id', $user->id)
  15. ->order('is_default', 'desc')
  16. ->order('id', 'desc')
  17. ->select();
  18. $this->success('获取成功', $userAddresses);
  19. }
  20. /**
  21. * 添加收货地址
  22. */
  23. public function add()
  24. {
  25. $user = auth_user();
  26. $params = $this->request->only([
  27. 'consignee', 'mobile', 'province_name', 'city_name', 'district_name', 'address', 'is_default'
  28. ]);
  29. $params['user_id'] = $user->id;
  30. $this->svalidate($params, ".add");
  31. $params = $this->getAreaIdByName($params);
  32. Db::transaction(function () use ($user, $params) {
  33. $userAddress = new UserAddressModel();
  34. $userAddress->save($params);
  35. if ($userAddress->is_default) {
  36. // 修改其他收货地址为非默认
  37. UserAddressModel::where('id', '<>', $userAddress->id)
  38. ->where('user_id', $user->id)
  39. ->where('is_default', 1)
  40. ->update(['is_default' => 0]);
  41. }
  42. });
  43. $this->success('保存成功');
  44. }
  45. /**
  46. * 收货地址详情
  47. *
  48. * @param $id
  49. * @return \think\Response
  50. */
  51. public function detail()
  52. {
  53. $user = auth_user();
  54. $id = $this->request->param('id');
  55. $userAddress = UserAddressModel::where('user_id', $user->id)->where('id', $id)->find();
  56. if (!$userAddress) {
  57. $this->error(__('No Results were found'));
  58. }
  59. $this->success('获取成功', $userAddress);
  60. }
  61. /**
  62. * 默认收货地址
  63. *
  64. * @return \think\Response
  65. */
  66. public function getdefault()
  67. {
  68. $user = auth_user();
  69. $userAddress = UserAddressModel::default()->where('user_id', $user->id)->find();
  70. $this->success('获取成功', $userAddress);
  71. }
  72. /**
  73. * 编辑收货地址
  74. *
  75. * @return \think\Response
  76. */
  77. public function edit()
  78. {
  79. $user = auth_user();
  80. $id = $this->request->param('id');
  81. $userAddress = UserAddressModel::where('user_id', $user->id)->where('id', $id)->find();
  82. if (!$userAddress) {
  83. $this->error(__('No Results were found'));
  84. }
  85. $params = $this->request->only([
  86. 'consignee', 'mobile', 'province_name', 'city_name', 'district_name', 'address', 'is_default'
  87. ]);
  88. $this->svalidate($params, ".edit");
  89. $params = $this->getAreaIdByName($params);
  90. Db::transaction(function () use ($user, $params, $userAddress) {
  91. $userAddress->save($params);
  92. if ($userAddress->is_default) {
  93. // 修改其他收货地址为非默认
  94. UserAddressModel::where('id', '<>', $userAddress->id)
  95. ->where('user_id', $user->id)
  96. ->where('is_default', 1)
  97. ->update(['is_default' => 0]);
  98. }
  99. });
  100. $this->success('保存成功');
  101. }
  102. /**
  103. * 删除收货地址
  104. *
  105. * @param string $id 要删除的收货地址
  106. * @return void
  107. */
  108. public function delete()
  109. {
  110. $user = auth_user();
  111. $id = $this->request->param('id');
  112. $userAddress = UserAddressModel::where('user_id', $user->id)->where('id', $id)->find();
  113. if (!$userAddress) {
  114. $this->error(__('No Results were found'));
  115. }
  116. $userAddress->delete();
  117. $this->success('删除成功');
  118. }
  119. private function getAreaIdByName($params)
  120. {
  121. $province = AreaModel::where([
  122. 'name' => $params['province_name'],
  123. 'level' => 'province'
  124. ])->find();
  125. if (!$province) $this->error('请选择正确的行政区');
  126. $params['province_id'] = $province->id;
  127. $city = AreaModel::where([
  128. 'name' => $params['city_name'],
  129. 'level' => 'city',
  130. 'pid' => $province->id
  131. ])->find();
  132. if (!$city) $this->error('请选择正确的行政区');
  133. $params['city_id'] = $city->id;
  134. $district = AreaModel::where([
  135. 'name' => $params['district_name'],
  136. 'level' => 'district',
  137. 'pid' => $city->id
  138. ])->find();
  139. if (!$district) $this->error('请选择正确的行政区');
  140. $params['district_id'] = $district->id;
  141. return $params;
  142. }
  143. }