UserAddressController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace mall\controllers;
  3. use common\components\util;
  4. use common\models\xhUserAddress;
  5. use Yii;
  6. class UserAddressController extends BaseController
  7. {
  8. public $guestAccess = [];
  9. // 获取地址列表
  10. public function actionList()
  11. {
  12. $user = $this->user;
  13. if (empty($user)) {
  14. util::fail('请先登录');
  15. }
  16. $list = xhUserAddress::find()
  17. ->where(['userId' => $user->id])
  18. ->orderBy(['default' => SORT_DESC, 'id' => SORT_DESC])
  19. ->asArray()
  20. ->all();
  21. // 格式化数据,兼容前端需要的字段
  22. foreach ($list as &$item) {
  23. $item['name'] = $item['name'] ?? '';
  24. $item['phone'] = $item['phone'] ?? '';
  25. $item['tag'] = $item['tag'] ?? '';
  26. }
  27. util::success(['list' => $list]);
  28. }
  29. // 获取地址详情
  30. public function actionDetail()
  31. {
  32. $user = $this->user;
  33. if (empty($user)) {
  34. util::fail('请先登录');
  35. }
  36. $id = Yii::$app->request->get('id');
  37. if (empty($id)) {
  38. util::fail('参数错误');
  39. }
  40. $info = xhUserAddress::find()
  41. ->where(['id' => $id, 'userId' => $user->id])
  42. ->asArray()
  43. ->one();
  44. if (empty($info)) {
  45. util::fail('地址不存在');
  46. }
  47. util::success(['info' => $info]);
  48. }
  49. // 创建地址
  50. public function actionCreate()
  51. {
  52. $user = $this->user;
  53. if (empty($user)) {
  54. util::fail('请先登录');
  55. }
  56. $post = Yii::$app->request->post();
  57. $model = new xhUserAddress();
  58. $model->userId = $user->id;
  59. $model->name = $post['name'] ?? '';
  60. $model->phone = $post['phone'] ?? '';
  61. $model->tag = $post['tag'] ?? '';
  62. $model->lat = $post['lat'] ?? '';
  63. $model->long = $post['long'] ?? '';
  64. $model->province = $post['province'] ?? '';
  65. $model->city = $post['city'] ?? '';
  66. $model->dist = $post['dist'] ?? '';
  67. $model->address = $post['address'] ?? '';
  68. $model->floor = $post['floor'] ?? '';
  69. $model->fullAddress = $post['fullAddress'] ?? '';
  70. $model->showAddress = $post['showAddress'] ?? '';
  71. $model->default = $post['default'] ?? 0;
  72. if ($model->default == 1) {
  73. xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
  74. }
  75. if ($model->save()) {
  76. util::complete();
  77. } else {
  78. util::fail('保存失败');
  79. }
  80. }
  81. // 更新地址
  82. public function actionUpdate()
  83. {
  84. $user = $this->user;
  85. if (empty($user)) {
  86. util::fail('请先登录');
  87. }
  88. $post = Yii::$app->request->post();
  89. $id = $post['id'] ?? 0;
  90. $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
  91. if (empty($model)) {
  92. util::fail('地址不存在');
  93. }
  94. $model->name = $post['name'] ?? $model->name;
  95. $model->phone = $post['phone'] ?? $model->phone;
  96. $model->tag = $post['tag'] ?? $model->tag;
  97. $model->lat = $post['lat'] ?? $model->lat;
  98. $model->long = $post['long'] ?? $model->long;
  99. $model->province = $post['province'] ?? $model->province;
  100. $model->city = $post['city'] ?? $model->city;
  101. $model->dist = $post['dist'] ?? $model->dist;
  102. $model->address = $post['address'] ?? $model->address;
  103. $model->floor = $post['floor'] ?? $model->floor;
  104. $model->fullAddress = $post['fullAddress'] ?? $model->fullAddress;
  105. $model->showAddress = $post['showAddress'] ?? $model->showAddress;
  106. $model->default = $post['default'] ?? $model->default;
  107. if ($model->default == 1) {
  108. xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
  109. }
  110. if ($model->save()) {
  111. util::complete();
  112. } else {
  113. util::fail('保存失败');
  114. }
  115. }
  116. // 删除地址
  117. public function actionDelete()
  118. {
  119. $user = $this->user;
  120. if (empty($user)) {
  121. util::fail('请先登录');
  122. }
  123. $post = Yii::$app->request->post();
  124. $id = $post['id'] ?? 0;
  125. $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
  126. if (empty($model)) {
  127. util::fail('地址不存在');
  128. }
  129. if ($model->delete()) {
  130. util::complete();
  131. } else {
  132. util::fail('删除失败');
  133. }
  134. }
  135. // 设为默认地址
  136. public function actionSetDefault()
  137. {
  138. $user = $this->user;
  139. if (empty($user)) {
  140. util::fail('请先登录');
  141. }
  142. $post = Yii::$app->request->post();
  143. $id = $post['id'] ?? 0;
  144. $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
  145. if (empty($model)) {
  146. util::fail('地址不存在');
  147. }
  148. xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
  149. $model->default = 1;
  150. if ($model->save()) {
  151. util::complete();
  152. } else {
  153. util::fail('设置失败');
  154. }
  155. }
  156. }