CommonModel.php
7.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
namespace api\common\model;
use think\Model;
use think\Loader;
class CommonModel extends Model
{
// 关联模型过滤
protected $relationFilter = [];
/**
* @access public
* @param array $params 过滤参数
* @return array|collection 查询结果
*/
public function getDatas($params = [])
{
if (empty($params)) {
return $this->select();
}
$this->setCondition($params);
if (!empty($params['id'])) {
$datas = $this->find();
} else {
$datas = $this->select();
}
if (!empty($params['relation'])) {
$allowedRelations = $this->allowedRelations($params['relation']);
if (!empty($allowedRelations)) {
if (!empty($params['id'])) {
if (!empty($datas)) {
$datas->append($allowedRelations);
}
} else {
if (count($datas) > 0) {
$datas->load($allowedRelations);
$datas->append($allowedRelations);
}
}
}
}
return $datas;
}
/**
* @access public
* @param array $params 过滤参数
* @return $this
*/
public function setCondition($params)
{
if (empty($params)) {
return $this;
}
if (!empty($params['relation'])) {
$allowedRelations = $this->allowedRelations($params['relation']);
if (!empty($allowedRelations)) {
if (!empty($params['id']) && count($allowedRelations) == 1) {
$this->paramsFilter($params);
} else {
$this->paramsFilter($params);//->with($allowedRelations);
}
}
} else {
$this->paramsFilter($params);
}
return $this;
}
/**
* @access public
* @param array $params 过滤参数
* @param model $model 关联模型
* @return model|array $this|链式查询条件数组
*/
public function paramsFilter($params, $model = null)
{
if (!empty($model)) {
$_this = $model;
} else {
$_this = $this;
}
if (isset($_this->visible)) {
$whiteParams = $_this->visible;
}
// 设置field字段过滤
if (!empty($params['field'])) {
$filterParams = $this->strToArr($params['field']);
if (!empty($whiteParams)) {
$mixedField = array_intersect($filterParams, $whiteParams);
} else {
$mixedField = $filterParams;
}
if (!empty($mixedField)) {
$_this->field($mixedField);
}
}
// 设置id,ids
if (!empty($params['ids'])) {
$ids = $this->strToArr($params['ids']);
foreach ($ids as $key => $value) {
$ids[$key] = intval($value);
}
}
if (!empty($params['id'])) {
$id = intval($params['id']);
if (!empty($id)) {
return $_this->where('id', $id);
}
} elseif (!empty($ids)) {
$_this->where('id', 'in', $ids);
}
if (!empty($params['where'])) {
if (empty($model)) {
$_this->where($params['where']);
}
}
// 设置分页
if (!empty($params['page'])) {
$pageArr = $this->strToArr($params['page']);
$page = [];
foreach ($pageArr as $value) {
$page[] = intval($value);
}
if (count($page) == 1) {
$_this->page($page[0]);
} elseif (count($page) == 2) {
$_this->page($page[0], $page[1]);
}
} elseif (!empty($params['limit'])) { // 设置limit查询
$limitArr = $this->strToArr($params['limit']);
$limit = [];
foreach ($limitArr as $value) {
$limit[] = intval($value);
}
if (count($limit) == 1) {
$_this->limit($limit[0]);
} elseif (count($limit) == 2) {
$_this->limit($limit[0], $limit[1]);
}
} else {
$_this->limit(10);
}
//设置排序
if (!empty($params['order'])) {
$order = $this->strToArr($params['order']);
foreach ($order as $key => $value) {
$upDwn = substr($value, 0, 1);
$orderType = $upDwn == '-' ? 'desc' : 'asc';
$orderField = substr($value, 1);
if (!empty($whiteParams)) {
if (in_array($orderField, $whiteParams)) {
$orderWhere[$orderField] = $orderType;
}
} else {
$orderWhere[$orderField] = $orderType;
}
}
if (!empty($orderWhere)) {
$_this->order($orderWhere);
}
}
return $_this;
}
/**
* 设置链式查询
* @access public
* @param array $params 链式查询条件
* @param model $model 模型
* @return $this
*/
public function setParamsQuery($params, $model = null)
{
if (!empty($model)) {
$_this = $model;
} else {
$_this = $this;
}
$_this->alias('articles');
if (!empty($params['field'])) {
$_this->field($params['field']);
}
if (!empty($params['ids'])) {
$_this->where('articles.id', $params['ids'][1], $params['ids'][2]);
}
if (!empty($params['limit'])) {
$_this->limit($params['limit']);
}
if (!empty($params['page'])) {
$_this->page($params['page']);
}
if (!empty($params['order'])) {
$_this->order($params['order']);
}
return $_this;
}
public function allowedRelations($relations)
{
if (is_string($relations)) {
$relations = explode(',', $relations);
}
if (!is_array($relations)) {
return false;
}
return array_intersect($this->relationFilter, $relations);
}
/**
* 是否允许关联
* @access public
* @param string $relationName 模型关联方法名
* @return boolean
*/
public function isWhite($relationName)
{
if (!is_string($relationName)) {
return false;
}
$name = Loader::parseName($relationName, 1, false);
if (in_array($name, $this->relationFilter)) {
return true;
} else {
return false;
}
}
/**
* 懒人函数
* @access public
* @param string $value 字符串
* @return array
*/
public function strToArr($string)
{
return is_string($string) ? explode(',', $string) : $string;
}
}