forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
executable file
·212 lines (167 loc) · 4.39 KB
/
Model.php
File metadata and controls
executable file
·212 lines (167 loc) · 4.39 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
<?php
namespace app\models;
use Yii;
use Jenssegers\Agent\Agent;
use itbdw\Ip\IpLocation;
class Model extends \yii\db\ActiveRecord
{
public $models;
public $pages;
public $params;
public $query;
public $count;
public $sql;
public $pageSize = 20;
const ACTIVE_STATUS = 10; //启用状态
const DISABLE_STATUS = 20; //禁用状态
const DELETED_STATUS = 30; //删除状态
public $statusLabels = [
self::ACTIVE_STATUS => '正常',
self::DISABLE_STATUS => '禁用',
self::DELETED_STATUS => '删除',
];
public static function findModel($condition = null)
{
if(!$condition){
return new static();
}
if (($model = static::findOne($condition)) !== null) {
return $model;
} else {
return new static();
}
}
/**
* 获取错误字段
* @return int|null|string
*/
public function getErrorLabel()
{
return key($this->getFirstErrors());
}
/**
* 获取错误信息
* @return mixed
*/
public function getErrorMessage()
{
return current($this->getFirstErrors());
}
/**
* 创建加密id
* @return string
*/
public function createEncodeId()
{
return date('Y').substr(time(),-5).substr(microtime(),2,5);
}
/**
* 获取状态文案
* @return mixed
*/
public function getStatusLabel()
{
return $this->statusLabels[$this->status];
}
/**
* 获取创建者
* @return \yii\db\ActiveQuery
*/
public function getCreater()
{
return $this->hasOne(Account::className(),['id'=>'creater_id']);
}
/**
* 获取更新者
* @return \yii\db\ActiveQuery
*/
public function getUpdater()
{
return $this->hasOne(Account::className(),['id'=>'updater_id']);
}
/**
* 获取友好的时间,如5分钟前
* @return string
*/
public function getFriendTime($time = null)
{
$time = $time ? strtotime($time) : time();
return Yii::$app->formatter->asRelativeTime($time);
}
/**
* 获取程序安装时间
* @return mixed
*/
public function getInstallTime()
{
$file = Yii::getAlias("@runtime") .'/install/install.lock';
if(file_exists($file)){
$install = file_get_contents($file);
return json_decode($install)->installed_at;
}
}
/**
* 获取模型更新内容
* @param $oldAttributes 原始属性
* @param $dirtyAttributes 更新属性
* @param string $preText 前缀文案
* @return string
*/
public function getUpdateContent($oldAttributes, $dirtyAttributes, $preText = '')
{
$content = '';
foreach ($dirtyAttributes as $name => $value) {
$label = '<strong>' . $this->getAttributeLabel($name) . '</strong>';
if(isset($oldAttributes[$name])){
$oldValue = '<code>' . $oldAttributes[$name] . '</code>';
$newValue = '<code>' . $value . '</code>';
$content .= $preText . ' ' . $label . ' 从' . $oldValue . '更新为' . $newValue . ',';
}
}
return trim($content, ',');
}
/**
* 获取客户端IP
* @return mixed|string|null
*/
public function getIp()
{
return Yii::$app->request->userIP;
}
/**
* 获取ip地理位置
* @param null $ip
* @return string
*/
public function getLocation($ip = null)
{
$ip = $ip ? : $this->getIp();
$location = IpLocation::getLocation($ip);
$country = $location['country'];
$province = $location['province'];
$city = $location['city'] ? : $province;
return $country . ' ' . $province . ' ' . $city;
}
/**
* 获取访问者的操作系统
* @return string
*/
public function getOs()
{
$agent = new Agent();
$platform = $agent->platform();
$version = $agent->version($platform);
return $platform . '(' . $version . ')';
}
/**
* 获取访问者浏览器
* @return string
*/
public function getBrowser()
{
$agent = new Agent();
$browser = $agent->browser();
$version = $agent->version($browser);
return $browser . '(' . $version . ')';
}
}