210 lines
7.1 KiB
PHP
210 lines
7.1 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2021年6月2日
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\modules\api\controllers;
|
||
|
||
use app\modules\api\behaviors\LoginBehavior;
|
||
use app\models\common\CommonAddressActionForm;
|
||
use app\models\common\CommonAddressListForm;
|
||
use app\models\common\CommonAddressEditForm;
|
||
use app\models\Address;
|
||
|
||
|
||
|
||
class AddressController extends Controller
|
||
{
|
||
public function behaviors() {
|
||
return array_merge(parent::behaviors(),[
|
||
'login' => [
|
||
'class' => LoginBehavior::className(),
|
||
'ignore' => [
|
||
]
|
||
]
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* hidedoc
|
||
* @catalog 用户信息/收货地址
|
||
* @title 收货地址列表
|
||
* @description 本接口提供收货地址列表
|
||
* @method get
|
||
* @url /api/address/index
|
||
* @param keywords 非必选 string 关键词
|
||
* @param page 必选 int 页码
|
||
* @param limit 非必选 int 每页记录数
|
||
* @return {"code":0,"msg":"ok","data":[{"id":"1","user_id":"1","name":"张三","mobile":"18812341234","province_id":"13","province":"福建省","city_id":"146","city":"厦门市","district_id":"1469","district":"集美区","town_id":"0","town":"","detail":"软件园三期","lat":"23.459464","lng":"118.224234","is_default":"1","tag":"1","gender":"1","formatted_addr":"","created_at_cn":"1970-01-01 08:00:00","tag_cn":"家","gender_cn":"男"}],"count":"1","page_size":20,"page_count":1,"page_no":1,"end_flag":false}
|
||
* @return_param count int 记录条数
|
||
* @return_param page_size int 每页记录数
|
||
* @return_param page_count int 总页数
|
||
* @return_param page_no int 当前页码
|
||
* @return_param end_flag bool 是否加载结束
|
||
* @remark
|
||
*/
|
||
public function actionIndex()
|
||
{
|
||
$form = new CommonAddressListForm();
|
||
$form->attributes = \Yii::$app->request->get();
|
||
$form->cx_mch_id = $this->cx_mch_id;
|
||
$form->user_id = \Yii::$app->user->identity->id;
|
||
$data = $form->search();
|
||
return $this->responseHandler($data);
|
||
}
|
||
|
||
/**
|
||
* hidedoc
|
||
* @catalog 用户信息/收货地址
|
||
* @title 编辑收货地址
|
||
* @description 本接口提供编辑收货地址
|
||
* @method post
|
||
* @url /api/address/edit?id=${id}
|
||
* @param name 必选 string 收货人姓名
|
||
* @param mobile 必选 string 收货人手机号
|
||
* @param province_id 必选 int 省ID
|
||
* @param city_id 必选 int 城市ID
|
||
* @param dsitrict_id 必选 int 区(县)ID
|
||
* @param town_id 非必选 int 镇(街道)ID
|
||
* @param detail 必选 string 详细地址
|
||
* @param lat 必选 number 纬度
|
||
* @param lng 必选 number 经度
|
||
* @param is_default 必选 int 是否默认收货地址,0=否,1=是
|
||
* @param gender 必选 int 性别,0=未知,1=男,2=女,3=保密
|
||
* @param tag 必选 int 标签,0=未知,1=家,2=公司,3=学校,4=其他
|
||
* @return {"code":0,"msg":"ok","data":[]}
|
||
* @return_param
|
||
* @remark id=0时新增收货地址,get请求获取地址详情
|
||
*/
|
||
public function actionEdit($id = 0)
|
||
{
|
||
$user_id = \Yii::$app->user->identity->id;
|
||
$model = Address::findOne([
|
||
'id' => $id,
|
||
'user_id' => $user_id,
|
||
'cx_mch_id' => $this->cx_mch_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if($model == null)
|
||
$model = new Address();
|
||
if(\Yii::$app->request->isPost){
|
||
$form = new CommonAddressEditForm();
|
||
$form->attributes = \Yii::$app->request->post();
|
||
$form->user_id = $user_id;
|
||
$form->model = $model;
|
||
$form->cx_mch_id = $this->cx_mch_id;
|
||
$data = $form->save();
|
||
return $this->responseHandler($data);
|
||
}
|
||
$data = [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $this->getAddressDetail($model)
|
||
];
|
||
return $this->responseHandler($data);
|
||
}
|
||
|
||
private function getAddressDetail($model){
|
||
$data = [];
|
||
if($model->isNewRecord){
|
||
$data = [
|
||
'id' => 0
|
||
];
|
||
} else {
|
||
$data = [
|
||
'id' => $model->id,
|
||
'name' => $model->name,
|
||
'mobile' => $model->mobile,
|
||
'province' => [
|
||
'id' => $model->province_id,
|
||
'name' => $model->province
|
||
],
|
||
'city' => [
|
||
'id' => $model->city_id,
|
||
'name' => $model->city
|
||
],
|
||
'district' => [
|
||
'id' => $model->district_id,
|
||
'name' => $model->district
|
||
],
|
||
'town' => [
|
||
'id' => $model->town_id,
|
||
'name' => $model->town
|
||
],
|
||
'detail' => $model->detail,
|
||
'formatted_addr' => $model->formatted_addr,
|
||
'is_default' => $model->is_default,
|
||
'lat' => $model->lat,
|
||
'lng' => $model->lng,
|
||
'gender' => $model->gender,
|
||
'tag' => $model->tag
|
||
];
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* hidedoc
|
||
* @catalog 用户信息/收货地址
|
||
* @title 删除收货地址
|
||
* @description 本接口提供删除收货地址
|
||
* @method post
|
||
* @url /api/address/delete
|
||
* @param address_id 必选 int 收货地址ID
|
||
* @return {"code":0,"msg":"ok","data":[]}
|
||
* @return_param
|
||
* @remark
|
||
*/
|
||
public function actionDelete()
|
||
{
|
||
if(!\Yii::$app->request->isPost){
|
||
$data = $this->invaildRequest();
|
||
return $this->responseHandler($data);
|
||
}
|
||
$form = new CommonAddressActionForm();
|
||
$form->attributes = \Yii::$app->request->post();
|
||
$form->user_id = \Yii::$app->user->identity->id;
|
||
$form->cx_mch_id = $this->cx_mch_id;
|
||
$data = $form->delete();
|
||
return $this->responseHandler($data);
|
||
}
|
||
|
||
/**
|
||
* hidedoc
|
||
* @catalog 用户信息/收货地址
|
||
* @title 设置默认收货地址
|
||
* @description 本接口提供设置默认收货地址
|
||
* @method post
|
||
* @url /api/address/set-default
|
||
* @param address_id 必选 int 收货地址ID
|
||
* @return {"code":0,"msg":"ok","data":[]}
|
||
* @return_param
|
||
* @remark
|
||
*/
|
||
public function actionSetDefault()
|
||
{
|
||
if(!\Yii::$app->request->isPost){
|
||
$data = $this->invaildRequest();
|
||
return $this->responseHandler($data);
|
||
}
|
||
$form = new CommonAddressActionForm();
|
||
$form->attributes = \Yii::$app->request->post();
|
||
$form->user_id = \Yii::$app->user->identity->id;
|
||
$form->cx_mch_id = $this->cx_mch_id;
|
||
$data = $form->set_default();
|
||
return $this->responseHandler($data);
|
||
}
|
||
|
||
|
||
}
|
||
|