|
|
@@ -0,0 +1,188 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace mall\controllers;
|
|
|
+
|
|
|
+use common\components\util;
|
|
|
+use common\models\xhUserAddress;
|
|
|
+use Yii;
|
|
|
+
|
|
|
+class UserAddressController extends BaseController
|
|
|
+{
|
|
|
+ public $guestAccess = [];
|
|
|
+
|
|
|
+ // 获取地址列表
|
|
|
+ public function actionList()
|
|
|
+ {
|
|
|
+ $user = $this->user;
|
|
|
+ if (empty($user)) {
|
|
|
+ util::fail('请先登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = xhUserAddress::find()
|
|
|
+ ->where(['userId' => $user->id])
|
|
|
+ ->orderBy(['default' => SORT_DESC, 'id' => SORT_DESC])
|
|
|
+ ->asArray()
|
|
|
+ ->all();
|
|
|
+
|
|
|
+ // 格式化数据,兼容前端需要的字段
|
|
|
+ foreach ($list as &$item) {
|
|
|
+ $item['name'] = $item['name'] ?? '';
|
|
|
+ $item['phone'] = $item['phone'] ?? '';
|
|
|
+ $item['tag'] = $item['tag'] ?? '';
|
|
|
+ }
|
|
|
+
|
|
|
+ util::success(['list' => $list]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取地址详情
|
|
|
+ public function actionDetail()
|
|
|
+ {
|
|
|
+ $user = $this->user;
|
|
|
+ if (empty($user)) {
|
|
|
+ util::fail('请先登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $id = Yii::$app->request->get('id');
|
|
|
+ if (empty($id)) {
|
|
|
+ util::fail('参数错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = xhUserAddress::find()
|
|
|
+ ->where(['id' => $id, 'userId' => $user->id])
|
|
|
+ ->asArray()
|
|
|
+ ->one();
|
|
|
+
|
|
|
+ if (empty($info)) {
|
|
|
+ util::fail('地址不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ util::success(['info' => $info]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建地址
|
|
|
+ public function actionCreate()
|
|
|
+ {
|
|
|
+ $user = $this->user;
|
|
|
+ if (empty($user)) {
|
|
|
+ util::fail('请先登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $post = Yii::$app->request->post();
|
|
|
+
|
|
|
+ $model = new xhUserAddress();
|
|
|
+ $model->userId = $user->id;
|
|
|
+ $model->name = $post['name'] ?? '';
|
|
|
+ $model->phone = $post['phone'] ?? '';
|
|
|
+ $model->tag = $post['tag'] ?? '';
|
|
|
+ $model->lat = $post['lat'] ?? '';
|
|
|
+ $model->long = $post['long'] ?? '';
|
|
|
+ $model->province = $post['province'] ?? '';
|
|
|
+ $model->city = $post['city'] ?? '';
|
|
|
+ $model->dist = $post['dist'] ?? '';
|
|
|
+ $model->address = $post['address'] ?? '';
|
|
|
+ $model->floor = $post['floor'] ?? '';
|
|
|
+ $model->fullAddress = $post['fullAddress'] ?? '';
|
|
|
+ $model->showAddress = $post['showAddress'] ?? '';
|
|
|
+ $model->default = $post['default'] ?? 0;
|
|
|
+
|
|
|
+ if ($model->default == 1) {
|
|
|
+ xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($model->save()) {
|
|
|
+ util::complete();
|
|
|
+ } else {
|
|
|
+ util::fail('保存失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新地址
|
|
|
+ public function actionUpdate()
|
|
|
+ {
|
|
|
+ $user = $this->user;
|
|
|
+ if (empty($user)) {
|
|
|
+ util::fail('请先登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $post = Yii::$app->request->post();
|
|
|
+ $id = $post['id'] ?? 0;
|
|
|
+
|
|
|
+ $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
|
|
|
+ if (empty($model)) {
|
|
|
+ util::fail('地址不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $model->name = $post['name'] ?? $model->name;
|
|
|
+ $model->phone = $post['phone'] ?? $model->phone;
|
|
|
+ $model->tag = $post['tag'] ?? $model->tag;
|
|
|
+ $model->lat = $post['lat'] ?? $model->lat;
|
|
|
+ $model->long = $post['long'] ?? $model->long;
|
|
|
+ $model->province = $post['province'] ?? $model->province;
|
|
|
+ $model->city = $post['city'] ?? $model->city;
|
|
|
+ $model->dist = $post['dist'] ?? $model->dist;
|
|
|
+ $model->address = $post['address'] ?? $model->address;
|
|
|
+ $model->floor = $post['floor'] ?? $model->floor;
|
|
|
+ $model->fullAddress = $post['fullAddress'] ?? $model->fullAddress;
|
|
|
+ $model->showAddress = $post['showAddress'] ?? $model->showAddress;
|
|
|
+ $model->default = $post['default'] ?? $model->default;
|
|
|
+
|
|
|
+ if ($model->default == 1) {
|
|
|
+ xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($model->save()) {
|
|
|
+ util::complete();
|
|
|
+ } else {
|
|
|
+ util::fail('保存失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除地址
|
|
|
+ public function actionDelete()
|
|
|
+ {
|
|
|
+ $user = $this->user;
|
|
|
+ if (empty($user)) {
|
|
|
+ util::fail('请先登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $post = Yii::$app->request->post();
|
|
|
+ $id = $post['id'] ?? 0;
|
|
|
+
|
|
|
+ $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
|
|
|
+ if (empty($model)) {
|
|
|
+ util::fail('地址不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($model->delete()) {
|
|
|
+ util::complete();
|
|
|
+ } else {
|
|
|
+ util::fail('删除失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设为默认地址
|
|
|
+ public function actionSetDefault()
|
|
|
+ {
|
|
|
+ $user = $this->user;
|
|
|
+ if (empty($user)) {
|
|
|
+ util::fail('请先登录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $post = Yii::$app->request->post();
|
|
|
+ $id = $post['id'] ?? 0;
|
|
|
+
|
|
|
+ $model = xhUserAddress::findOne(['id' => $id, 'userId' => $user->id]);
|
|
|
+ if (empty($model)) {
|
|
|
+ util::fail('地址不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ xhUserAddress::updateAll(['default' => 0], ['userId' => $user->id]);
|
|
|
+ $model->default = 1;
|
|
|
+
|
|
|
+ if ($model->save()) {
|
|
|
+ util::complete();
|
|
|
+ } else {
|
|
|
+ util::fail('设置失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|