Test.php
4.0 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
<?php
namespace app\admin\model;
use think\Model;
use traits\model\SoftDelete;
class Test extends Model
{
use SoftDelete;
// 表名
protected $name = 'test';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'week_text',
'flag_text',
'genderdata_text',
'hobbydata_text',
'refreshtime_text',
'status_text',
'state_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
public function getWeekList()
{
return ['monday' => __('Week monday'), 'tuesday' => __('Week tuesday'), 'wednesday' => __('Week wednesday')];
}
public function getFlagList()
{
return ['hot' => __('Flag hot'), 'index' => __('Flag index'), 'recommend' => __('Flag recommend')];
}
public function getGenderdataList()
{
return ['male' => __('Genderdata male'), 'female' => __('Genderdata female')];
}
public function getHobbydataList()
{
return ['music' => __('Hobbydata music'), 'reading' => __('Hobbydata reading'), 'swimming' => __('Hobbydata swimming')];
}
public function getStatusList()
{
return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
}
public function getStateList()
{
return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2')];
}
public function getWeekTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['week']) ? $data['week'] : '');
$list = $this->getWeekList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getFlagTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
$valueArr = explode(',', $value);
$list = $this->getFlagList();
return implode(',', array_intersect_key($list, array_flip($valueArr)));
}
public function getGenderdataTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['genderdata']) ? $data['genderdata'] : '');
$list = $this->getGenderdataList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getHobbydataTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['hobbydata']) ? $data['hobbydata'] : '');
$valueArr = explode(',', $value);
$list = $this->getHobbydataList();
return implode(',', array_intersect_key($list, array_flip($valueArr)));
}
public function getRefreshtimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['refreshtime']) ? $data['refreshtime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStateTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
$list = $this->getStateList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setFlagAttr($value)
{
return is_array($value) ? implode(',', $value) : $value;
}
protected function setHobbydataAttr($value)
{
return is_array($value) ? implode(',', $value) : $value;
}
protected function setRefreshtimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
}