Repository navigation
Expand file tree
/
Copy pathCommonTrait.php
More file actions
186 lines (165 loc) · 4.57 KB
/
Copy pathCommonTrait.php
File metadata and controls
186 lines (165 loc) · 4.57 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
<?php
namespace app\common\traits;
use app\BaseLogic;
use app\BaseModel;
use app\common\lib\other\DataEdit;
/**
* 公用的trait
* @author longtaixuan
* @email 416803647@qq.com
*/
trait CommonTrait
{
/**
* 获取DataEdit实例
* @param array $options
* @return DataEdit
*/
public function dataEdit(array $options = []): DataEdit
{
return DataEdit::instance($options);
}
/**
* 创建class实例
* @param string $Class
* @return mixed
*/
public function class(string $Class)
{
return app($Class);
}
/**
* 创建model实例
* @param string $modelClass
* @return BaseModel
*/
public function model(string $modelClass)
{
return app($modelClass);
}
/**
* 创建logic实例
* @param string $logicClass
* @return BaseLogic
*/
public function logic(string $logicClass)
{
return app($logicClass);
}
/**
* 获取参数转换
* @param $arr
* @param array|string $data 数据数组或者获取数据的方法名
* @return array
*/
public function buildParam($arr, $data = 'param')
{
$param = [];
if (is_array($arr)) {
$request = $this->request;
$isDataArr = is_array($data);
$reqParam = $isDataArr ? $data : $request->$data();
foreach ($arr as $k => $v) {
$vv = explode('??', $v); // ??后面跟的是默认值
$vArr = explode('/', $vv[0]); // /后面跟的是强制转换类型
$vv[1] = $vv[1] ?? '';
if (isset($reqParam[$vArr[0]]) || strpos($v, '??') !== false) {
$val = $isDataArr ?
$this->transformValue($reqParam[$vArr[0]] ?? $vv[1], $vArr[1] ?? '') :
($request->$data($v) ?? $this->transformValue($vv[1], $vArr[1] ?? ''));
if (is_int($k)) {
$param[$vArr[0]] = $val;
} else {
$param[$k] = $val;
}
}
}
}
return $param;
}
/**
* 变量类型转换
* @param string $value 变量
* @param string $toType 要转换的类型
* @return array|bool|float|int|mixed|string
*/
public function transformValue($value = '', $toType = '')
{
switch ($toType) {
case 'string':
case 's':
$valueRes = (string)$value;
break;
case 'int':
case 'd':
$valueRes = (int)$value;
break;
case 'array':
case 'a':
$valueRes = (array)$value;
break;
case 'bool':
case 'b':
$valueRes = (bool)$value;
break;
case 'float':
case 'f':
$valueRes = (float)$value;
break;
default:
$valueRes = $value;
}
return $valueRes;
}
/**
* 获取列表(分页)
* @param $model -- 模型路径|模型对象
* @param array $where
* @param string $field
* @param string $order
* @param int $perPage -- 每页数量
* @return array
*/
public function getListPage(
$model,
array $where = [],
string $field = '*',
string $order = 'id desc',
int $perPage = 10
) {
if (is_string($model)) {
$model = app($model);
}
$query = $model->where($where)->field($field);
$count = $query->count();
return $query->order($order)
->paginate(
$perPage,
$count
)->toArray();
}
/**
* 获取列表(不分页)
* @param $model -- 模型路径|模型对象
* @param array $where
* @param string $field
* @param string $order
* @return array
*/
public function getListOnly(
$model,
array $where = [],
string $field = '*',
string $order = 'id desc'
) {
if (is_string($model)) {
$model = app($model);
}
$res = $model->where($where)->field($field)->order($order)->select();
if ($res->isEmpty()) {
return [];
} else {
return $res->toArray();
}
}
}