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
187 lines (160 loc) · 3.87 KB
/
Copy pathModel.php
File metadata and controls
187 lines (160 loc) · 3.87 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
<?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 => '删除',
];
/**
* 实例化模型
* @param null $condition
* @return Model|null
*/
public static function findModel($condition = null)
{
if(!$condition){
return new static();
}
if (($model = static::findOne($condition)) !== null) {
return $model;
}
}
/**
* 获取错误字段
* @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']);
}
/**
* 获取当前格式化时间
* @param string $format
* @return false|string
*/
public function getNowTime($format = '')
{
$format = $format ? : 'Y-m-d H:i:s';
return date($format);
}
/**
* 获取友好的时间,如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;
}
}
/**
* 获取客户端IP
* @return mixed|string|null
*/
public function getUserIp()
{
return Yii::$app->request->userIP;
}
/**
* 获取ip地理位置
* @param null $ip
* @return string
*/
public function getLocation($ip = null)
{
$ip = $ip ? : $this->getUserIp();
$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 . ')';
}
}