Address.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\index\controller\shop;
  3. use addons\shop\model\Area;
  4. use app\common\controller\Frontend;
  5. use think\Db;
  6. class Address extends Frontend
  7. {
  8. protected $layout = 'default';
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 我的地址
  13. */
  14. public function index()
  15. {
  16. $addressList = \addons\shop\model\Address::where('user_id', $this->auth->id)->where('status', 'normal')->order('id', 'desc')->paginate(10);
  17. $this->view->assign('addressList', $addressList);
  18. $this->view->assign('title', "我的收货地址");
  19. return $this->view->fetch();
  20. }
  21. /**
  22. * 添加地址
  23. */
  24. public function add()
  25. {
  26. $this->view->assign('title', '添加收货地址');
  27. $this->view->assign('areainfo', null);
  28. $this->view->assign('address', ['isdefault' => 1]);
  29. return $this->view->fetch('shop/address/addedit');
  30. }
  31. /**
  32. * 修改地址
  33. */
  34. public function edit($id = null)
  35. {
  36. $address = \addons\shop\model\Address::get($id);
  37. if (!$address || $address['status'] != 'normal') {
  38. $this->error('未找到相关信息');
  39. }
  40. if ($address['user_id'] != $this->auth->id) {
  41. $this->error('无法进行越权操作');
  42. }
  43. $this->view->assign('address', $address);
  44. $this->view->assign('areainfo', $address->areainfo);
  45. $this->view->assign('title', '编辑地址');
  46. return $this->view->fetch('shop/address/addedit');
  47. }
  48. /**
  49. * 保存地址
  50. */
  51. public function save()
  52. {
  53. if ($this->request->isPost()) {
  54. $id = $this->request->post('id/d', 0);
  55. $province_id = $this->request->post('province_id/d');
  56. $city_id = $this->request->post('city_id/d');
  57. $area_id = $this->request->post('area_id/d');
  58. $address = $this->request->post('address');
  59. $mobile = $this->request->post('mobile');
  60. $receiver = $this->request->post('receiver');
  61. $isdefault = $this->request->post('isdefault', 0);
  62. if (!$province_id || !$city_id || !$area_id) {
  63. $this->error('省市区不能为空');
  64. }
  65. if (!$address) {
  66. $this->error('详细地址不能为空');
  67. }
  68. if (!$receiver) {
  69. $this->error('收货人姓名不能为空');
  70. }
  71. if (!$mobile) {
  72. $this->error('手机号不能为空');
  73. }
  74. $areaInfo = \addons\shop\model\Area::get($area_id);
  75. $zipcode = $areaInfo ? $areaInfo['zipcode'] : '';
  76. $data = [
  77. 'user_id' => $this->auth->id,
  78. 'province_id' => $province_id,
  79. 'city_id' => $city_id,
  80. 'area_id' => $area_id,
  81. 'address' => $address,
  82. 'mobile' => $mobile,
  83. 'receiver' => $receiver,
  84. 'zipcode' => $zipcode,
  85. 'isdefault' => $isdefault,
  86. 'status' => 'normal',
  87. ];
  88. if ($id) {
  89. $addressInfo = \addons\shop\model\Address::get($id);
  90. if (!$addressInfo) {
  91. $this->error('未找到相关信息');
  92. }
  93. if ($addressInfo['user_id'] != $this->auth->id) {
  94. $this->error('无法进行越权操作');
  95. }
  96. $addressInfo->save($data);
  97. } else {
  98. \addons\shop\model\Address::create($data, true);
  99. }
  100. $this->success("保存成功", url("shop.address/index"));
  101. }
  102. return;
  103. }
  104. /**
  105. * 删除地址
  106. */
  107. public function del()
  108. {
  109. $id = $this->request->post("id/d");
  110. $addressInfo = \addons\shop\model\Address::get($id);
  111. if (!$addressInfo) {
  112. $this->error('未找到相关信息');
  113. }
  114. if ($addressInfo['user_id'] != $this->auth->id) {
  115. $this->error('无法进行越权操作');
  116. }
  117. Db::startTrans();
  118. try {
  119. $addressInfo->delete();
  120. if ($addressInfo->isdefault) {
  121. $prev = \addons\shop\model\Address::where('user_id', $this->auth->id)->where('status', 'normal')->order('id', 'desc')->find();
  122. if ($prev) {
  123. $prev->save(['isdefault' => 1]);
  124. }
  125. }
  126. Db::commit();
  127. } catch (\Exception $e) {
  128. Db::rollback();
  129. $this->error('删除失败');
  130. }
  131. $this->success();
  132. }
  133. /**
  134. * 设置默认地址
  135. */
  136. public function setdefault()
  137. {
  138. $id = $this->request->post("id/d");
  139. $addressInfo = \addons\shop\model\Address::get($id);
  140. if (!$addressInfo) {
  141. $this->error('未找到相关信息');
  142. }
  143. if ($addressInfo['user_id'] != $this->auth->id) {
  144. $this->error('无法进行越权操作');
  145. }
  146. Db::startTrans();
  147. try {
  148. \addons\shop\model\Address::where('user_id', $this->auth->id)->where('isdefault', 1)->update(['isdefault' => 0]);
  149. $addressInfo->save(['isdefault' => 1]);
  150. Db::commit();
  151. } catch (\Exception $e) {
  152. Db::rollback();
  153. $this->error('操作失败');
  154. }
  155. $this->success();
  156. }
  157. /**
  158. * 读取省市区数据,联动列表
  159. */
  160. public function area()
  161. {
  162. $province_id = $this->request->get('province_id');
  163. $city_id = $this->request->get('city_id');
  164. $where = ['pid' => 0, 'level' => 1];
  165. $provinceList = null;
  166. if ($province_id !== '') {
  167. if ($province_id) {
  168. $where['pid'] = $province_id;
  169. $where['level'] = 2;
  170. }
  171. if ($city_id !== '') {
  172. if ($city_id) {
  173. $where['pid'] = $city_id;
  174. $where['level'] = 3;
  175. }
  176. $provinceList = Area::where($where)->field('id as value,name')->where('status', 'normal')->select();
  177. }
  178. }
  179. $this->success('', null, $provinceList);
  180. }
  181. }