-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBase.php
More file actions
306 lines (281 loc) · 8.58 KB
/
Copy pathBase.php
File metadata and controls
306 lines (281 loc) · 8.58 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
// +----------------------------------------------------------------------
// | JNAPI [ Jinaong Api Docment ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 欧阳 <xianglong111@126.com>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Base extends Model{
/**
* 查询条件
* @var array
*/
protected $sql_condition = [
'field' => '',
'where' => '',
'page' => '',
'limit' => 10,
'order' => '',
'group' => '',
'with' => []
];
/**
* 数量
* @var string
*/
private $count;
/**
* uid字段名
* @var string
*/
protected $uid_name = 'uid';
/**
* uid
* @var mixed
*/
protected $uid = false;
/**
* uid字段条件语句
* @var string
*/
protected $uid_condition = '';
/**
* 聚合函数
* @var const
*/
private $ploy_function = [
'count'=>'',
'sum'=>'',
'max'=>'',
'min'=>'',
'avg'=>''
];
/**
* 初始化数据
* @access public
* @param array|object $data 数据
*/
public function initData($model_field){
// 设置默认字段
$this->sql_condition['field'] = $this->allowed_field;
$model_arr = [];
if(is_array($model_field)){
foreach ($model_field as $model_child_name => $model_child) {
// 批量新增和修改的操作
if($model_child_name === 0) return $model_field;
// 是否为字段,判断标准为是否为数组
$is_field = !is_array($model_field[$model_child_name]);
if($is_field){
$model_arr[$model_child_name] = $model_child;
}else{
$model_arr['with'][$model_child_name] = "";
foreach($model_child as $key=>$field){
$model_arr['with'][$model_child_name] = $field;
}
}
}
}
$this->checkParams($model_arr);
return $model_arr;
}
/**
* 检测参数正确性
* @access public
* @param array|object $model_arr 模型数组,包含(table、field、page、count等)
*/
protected function checkParams($model_arr){
if(empty($model_arr)) return [];
foreach($model_arr as $key=>$value){
if(array_key_exists($key,$this->sql_condition)){
$method_name = 'handle'.ucfirst($key);
if(method_exists($this,$method_name)){
call_user_func_array([$this,$method_name],[$value]);
}else{
$this->sql_condition[$key] = $value;
}
}elseif(array_key_exists($key,$this->ploy_function)){
if(!empty($value)){
$this->ploy_function[$key] = $value;
}
}
}
}
/**
* 处理关联模型
* @access protected
* @param array $with 关联模型的值
* @return
*/
private function handleWith($with){
if(empty($with)) return [];
foreach($with as $model_name=>$field){
$model = model($model_name);
$field = $this->checkFieldAllowed($field,$model->allowed_field);
$this->sql_condition['with'][$model_name] = function($query) use ($field,$model_name){
$query->withField($field);
};
}
}
/**
* 处理字段方法
* @access protected
* @param array $field 当前字段列表
* @return
*/
protected function handleField($field){
$this->sql_condition['field'] = $this->checkFieldAllowed($field,$this->allowed_field);
}
/**
* 检查字段是否允许操作
* @access protected
* @param array $field 当前字段列表
* @param array $allowed_field 允许字段列表
* @return
*/
private function checkFieldAllowed($field,$allowed_field){
if($field == ''){
$field = $allowed_field;
}
if(!is_array($field)){
$field = explode(',',$field);
}
if(!empty(array_diff($field, $allowed_field))){
error('NO_PERMISSIONS_FIELD');
}
return $field;
}
/**
* 检查字段是否允许操作
* @access protected
* @param array $field 当前字段列表
* @param array $allowed_field 允许字段列表
* @return
*/
public function setUidCondition(){
$this->uid = getUid();
if($this->uid == false) error('LOGIN_TIMEOUT');
if(!empty($this->with)){
$alias = str_replace(config('database.prefix'),'',$this->table);
$this->uid_condition = $alias.'.'.$this->uid_name.'='.$this->uid;
}
}
/**
* 判断用户是否为当前操作用户
* @access public
* @param int $pk 主键值
* @return bool
*/
protected function checkUser($pk){
$user = $this->where($this->uid_name,$this->uid)->where($this->pk,$pk)->value($this->uid_name);
if(!$user) error('NO_ACCESS_ALLOWED');
}
/**
* 模型条件SQL
* @access public
* @return array
*/
protected function getModel(){
$model = $this->field($this->sql_condition['field']);
foreach($this->sql_condition as $field=>$condition){
if($field != 'field' && $condition != ''){
$model = call_user_func_array([$model,$field],[$this->sql_condition[$field]]);
}
}
return $model;
}
/**
* 查询单条数据
* @access public
* @return array
*/
public function findOne(){
return $this->getModel()->find();
}
/**
* 查询多条数据
* @access public
* @return array
*/
public function findAll(){
return $this->getModel()->select();
}
/**
* 验证token新增修改一条记录
* @access public
* @param mixed $data 主键列表 支持闭包查询条件
* @return bool
*/
public function updateOne($data)
{
$is_update = array_key_exists($this->pk,$data);
if($this->uid !== false){
if($is_update){
$this->checkUser($data[$this->pk]);
}else{
$data[$this->uid_name] = $this->uid;
}
}
return $this->allowField($this->allowed_field)->isUpdate($is_update)->save($data) !== false;
}
/**
* 验证token新增修改多条记录
* @access public
* @param mixed $data 主键列表 支持闭包查询条件
* @return bool
*/
public function updateAll($datas)
{
if($this->uid !== false){
foreach($datas as $key=>$data){
if(array_key_exists($this->pk,$data)){
$this->checkUser($data[$this->pk]);
}else{
$data['uid'] = $this->uid;
$datas[$key] = $data;
}
}
}
return $this->allowField($this->allowed_field)->saveAll($datas) !== false;
}
/**
* 删除记录
* @access public
* @param mixed $data 主键列表 支持闭包查询条件
* @return bool
*/
public function deleteAll($data)
{
$resultSet = $this->select($data);
if (count($resultSet) === 0){
return false;
}else{
foreach ($resultSet as $data) {
$rs = $data->delete();
if($rs === false) return false;
}
}
return true;
}
/**
* 获取聚合函数数据
* @access public
* @return int
*/
public function getPloy($ploy){
$model = $this->getModel();
return call_user_func_array([$model,$ploy],[$this->ploy_function[$ploy]]);
}
/**
* 执行自定义模型方法
* @access public
* @param string
* @param array
* @return
*/
public function exeFun($fun_name,$data){
return call_user_func_array([$this,$fun_name],[$data]);
}
}