UserAddressController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\custom\classes\CustomClass;
  4. use common\components\util;
  5. use common\models\xhUserAddress;
  6. use Yii;
  7. /**
  8. * 商城用户收货地址(xhUserAddress)
  9. * 设为默认时同步回写同一 userId 下全部 xhCustom,保证各门店客户档案地址一致。
  10. */
  11. class UserAddressController extends BaseController
  12. {
  13. public $guestAccess = [];
  14. /**
  15. * 默认地址落库后:按 userId 批量同步到 xhCustom
  16. * @param int $userId 当前登录用户 id
  17. * @param xhUserAddress $address 已保存的默认地址
  18. */
  19. private function syncCustomWhenDefault($userId, xhUserAddress $address)
  20. {
  21. if ((int)($address->default ?? 0) !== 1) {
  22. return;
  23. }
  24. CustomClass::syncFromUserDefaultAddress($userId, $address);
  25. }
  26. // 获取地址列表
  27. public function actionList()
  28. {
  29. $user = $this->user;
  30. if (empty($user)) {
  31. util::fail('请先登录');
  32. }
  33. $list = xhUserAddress::find()
  34. ->where(['userId' => $user->id])
  35. ->orderBy(['default' => SORT_DESC, 'id' => SORT_DESC])
  36. ->asArray()
  37. ->all();
  38. // 格式化数据,兼容前端需要的字段
  39. foreach ($list as &$item) {
  40. $item['name'] = $item['name'] ?? '';
  41. $item['phone'] = $item['phone'] ?? '';
  42. $item['tag'] = $item['tag'] ?? '';
  43. }
  44. util::success(['list' => $list]);
  45. }
  46. // 获取地址详情
  47. public function actionDetail()
  48. {
  49. $user = $this->user;
  50. if (empty($user)) {
  51. util::fail('请先登录');
  52. }
  53. $id = Yii::$app->request->get('id');
  54. if (empty($id)) {
  55. util::fail('参数错误');
  56. }
  57. $info = xhUserAddress::find()
  58. ->where(['id' => $id, 'userId' => $user->id])
  59. ->asArray()
  60. ->one();
  61. if (empty($info)) {
  62. util::fail('地址不存在');
  63. }
  64. util::success(['info' => $info]);
  65. }
  66. // 创建地址
  67. public function actionCreate()
  68. {
  69. $user = $this->user;
  70. if (empty($user)) {
  71. util::fail('请先登录');
  72. }
  73. $post = Yii::$app->request->post();
  74. $model = new xhUserAddress();
  75. $model->userId = $user->id;
  76. $model->name = $post['name'] ?? '';
  77. $model->phone = $post['phone'] ?? '';
  78. $model->tag = $post['tag'] ?? '';
  79. $model->lat = $post['lat'] ?? '';
  80. $model->long = $post['long'] ?? '';
  81. $model->province = $post['province'] ?? '';
  82. $model->city = $post['city'] ?? '';
  83. $model->dist = $post['dist'] ?? '';
  84. $model->address = $post['address'] ?? '';
  85. $model->floor = $post['floor'] ?? '';
  86. $model->fullAddress = $post['fullAddress'] ?? '';
  87. $model->showAddress = $post['showAddress'] ?? '';
  88. $model->default = $post['default'] ?? 0;
  89. if ($model->default == 1) {
  90. xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
  91. }
  92. if ($model->save()) {
  93. // 新建并勾选默认:同步到该用户全部门店客户档案
  94. $this->syncCustomWhenDefault($user->id, $model);
  95. util::complete();
  96. } else {
  97. util::fail('保存失败');
  98. }
  99. }
  100. // 更新地址
  101. public function actionUpdate()
  102. {
  103. $user = $this->user;
  104. if (empty($user)) {
  105. util::fail('请先登录');
  106. }
  107. $post = Yii::$app->request->post();
  108. $id = $post['id'] ?? 0;
  109. $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
  110. if (empty($model)) {
  111. util::fail('地址不存在');
  112. }
  113. $model->name = $post['name'] ?? $model->name;
  114. $model->phone = $post['phone'] ?? $model->phone;
  115. $model->tag = $post['tag'] ?? $model->tag;
  116. $model->lat = $post['lat'] ?? $model->lat;
  117. $model->long = $post['long'] ?? $model->long;
  118. $model->province = $post['province'] ?? $model->province;
  119. $model->city = $post['city'] ?? $model->city;
  120. $model->dist = $post['dist'] ?? $model->dist;
  121. $model->address = $post['address'] ?? $model->address;
  122. $model->floor = $post['floor'] ?? $model->floor;
  123. $model->fullAddress = $post['fullAddress'] ?? $model->fullAddress;
  124. $model->showAddress = $post['showAddress'] ?? $model->showAddress;
  125. $model->default = $post['default'] ?? $model->default;
  126. if ($model->default == 1) {
  127. xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
  128. }
  129. if ($model->save()) {
  130. // 保存为默认(含编辑默认地址内容):同步到该用户全部门店客户档案
  131. $this->syncCustomWhenDefault($user->id, $model);
  132. util::complete();
  133. } else {
  134. util::fail('保存失败');
  135. }
  136. }
  137. // 删除地址
  138. public function actionDelete()
  139. {
  140. $user = $this->user;
  141. if (empty($user)) {
  142. util::fail('请先登录');
  143. }
  144. $post = Yii::$app->request->post();
  145. $id = $post['id'] ?? 0;
  146. $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
  147. if (empty($model)) {
  148. util::fail('地址不存在');
  149. }
  150. if ($model->delete()) {
  151. util::complete();
  152. } else {
  153. util::fail('删除失败');
  154. }
  155. }
  156. // 设为默认地址
  157. public function actionSetDefault()
  158. {
  159. $user = $this->user;
  160. if (empty($user)) {
  161. util::fail('请先登录');
  162. }
  163. $post = Yii::$app->request->post();
  164. $id = $post['id'] ?? 0;
  165. $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
  166. if (empty($model)) {
  167. util::fail('地址不存在');
  168. }
  169. xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
  170. $model->default = 1;
  171. if ($model->save()) {
  172. // 列表「设为默认」:同步到该用户全部门店客户档案
  173. $this->syncCustomWhenDefault($user->id, $model);
  174. util::complete();
  175. } else {
  176. util::fail('设置失败');
  177. }
  178. }
  179. }