Преглед изворни кода

C端 新增用户收货地址设置

ouyang пре 1 месец
родитељ
комит
020fc0870d
2 измењених фајлова са 268 додато и 0 уклоњено
  1. 188 0
      app-mall/controllers/UserAddressController.php
  2. 80 0
      common/models/xhUserAddress.php

+ 188 - 0
app-mall/controllers/UserAddressController.php

@@ -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('设置失败');
+        }
+    }
+}

+ 80 - 0
common/models/xhUserAddress.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace common\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "xhUserAddress".
+ *
+ * @property string $id
+ * @property int $userId 商家id/用户id
+ * @property string $name 收货人姓名
+ * @property string $phone 手机号
+ * @property string $tag 标签
+ * @property string $lat 纬度
+ * @property string $long 经度
+ * @property string $province 省
+ * @property string $city 市
+ * @property string $dist 区县
+ * @property string $address 街道地址
+ * @property string $floor 楼号门牌号
+ * @property string $fullAddress 完整地址
+ * @property string $showAddress 地图接口显示的完整地址
+ * @property int $default 是否默认地址
+ * @property string $addTime 添加时间
+ * @property string $updateTime
+ */
+class xhUserAddress extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'xhUserAddress';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['userId', 'default'], 'integer'],
+            [['addTime', 'updateTime'], 'safe'],
+            [['lat', 'long'], 'string', 'max' => 20],
+            [['name', 'phone', 'tag'], 'string', 'max' => 50],
+            [['province', 'city', 'dist'], 'string', 'max' => 100],
+            [['address', 'fullAddress'], 'string', 'max' => 500],
+            [['floor'], 'string', 'max' => 50],
+            [['showAddress'], 'string', 'max' => 900],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'userId' => 'User ID',
+            'name' => 'Name',
+            'phone' => 'Phone',
+            'tag' => 'Tag',
+            'lat' => 'Lat',
+            'long' => 'Long',
+            'province' => 'Province',
+            'city' => 'City',
+            'dist' => 'Dist',
+            'address' => 'Address',
+            'floor' => 'Floor',
+            'fullAddress' => 'Full Address',
+            'showAddress' => 'Show Address',
+            'default' => 'Default',
+            'addTime' => 'Add Time',
+            'updateTime' => 'Update Time',
+        ];
+    }
+}