AddressController.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace app\ht\modules\system\controllers;
use Yii;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;
use app\ht\controllers\BaseController;
use common\models\Address as AddressModel;
use common\models\AddressProfile as AddressProfileModel;
/**
* 地址管理控制器
* Class AddressController
* @package app\ht\modules\system\controllers
*/
class AddressController extends BaseController
{
/**
* 地址管理列表
*/
public function actionIndex()
{
$request = Yii::$app->request;
$city = $request->get('city');
$district = $request->get('district');
$keyword = $request->get('keyword');
$type = isset($_GET['type']) ? $_GET['type'] : null;
/**
* 获取地址类型label
*/
$typeLables = AddressModel::typeLabels();
/**
* 查询过滤处理
*/
$get = [];
$get['city'] = empty($city) ? "" : $city;
$get['district'] = empty($district) ? "" : $district;
$get['keyword'] = empty($keyword) ? "" : $keyword;
$get['type'] = ($type == "") ? null : $type;
$query = AddressModel::find()
->select('a.*, su.province, su.city, su.district')
->from(AddressModel::tableName() . 'a')
->orderBy('a.id DESC')
->leftJoin(AddressProfileModel::tableName() . 'su', "a.id = su.address_id");
if (!empty($city)) {
$query->andWhere(['like', 'su.city', $city]);
}
if (!empty($district)) {
$query->andWhere(['like', 'su.district', $district]);
}
if (!empty($keyword)) {
$query->orWhere(['like', 'a.address', $keyword]);
$query->orWhere(['like', 'a.title', $keyword]);
$query->orWhere(['like', 'a.detail', $keyword]);
}
if ($get['type'] !== null) {
$query->andWhere(['=', 'a.type', $type]);
}
/**
* 分页处理
*/
$pageSize = $request->get("pageSize") ? (int)$request->get("pageSize") : 50;
$pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => $pageSize]);
$query->offset($pages->offset)->limit($pages->limit)->all();
$model = $query->asArray()->all();
return $this->render('index', array(
'address' => $model,
'pages' => $pages,
'typeLables' => $typeLables,
'gets' => $get
));
}
/**
* 地址详情页
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
return $this->render('update', [
'model' => $model,
]);
}
/**
* 执行更新操作
*/
public function actionDoUpdate()
{
$req = Yii::$app->request;
$id = (int)$req->post('id');
$address = $req->post('address');
$title = $req->post('title');
$detail = $req->post('detail');
$longitude = $req->post('longitude');
$latitude = $req->post('latitude');
$model = $this->findModel($id);
$model->address = $address;
$model->detail = $detail;
$model->title = $title;
$model->longitude = $longitude;
$model->latitude = $latitude;
$res = $model->save();
if ($res) {
Yii::$app->session->setFlash('success', '保存地址成功');
return $this->renderJson(array("status" => 1, "msg" => "操作成功"));
} else {
return $this->renderJson(array("status" => 0, "msg" => "操作失败"));
}
}
/**
* 根据主键查找模型
*/
protected function findModel($id)
{
$obj = Yii::createObject(AddressModel::className());
if (($model = $obj::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('访问页面不存在');
}
}
}