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