1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace app\admin\model;
- use think\Model;
- class User extends Model
- {
- // 表名
- protected $name = 'user';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'prevtime_text',
- 'logintime_text',
- 'jointime_text'
- ];
- protected static function init()
- {
- self::beforeUpdate(function ($row) {
- $changed = $row->getChangedData();
- //如果有修改密码
- if (isset($changed['password'])) {
- if ($changed['password']) {
- $salt = \fast\Random::alnum();
- $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
- $row->salt = $salt;
- } else {
- unset($row->password);
- }
- }
- });
- }
- public function getGenderList()
- {
- return ['1' => __('Male'), '0' => __('Female')];
- }
- public function getStatusList()
- {
- return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
- }
- public function getPrevtimeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['prevtime'];
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- public function getLogintimeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['logintime'];
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- public function getJointimeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['jointime'];
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setPrevtimeAttr($value)
- {
- return $value && !is_numeric($value) ? strtotime($value) : $value;
- }
- protected function setLogintimeAttr($value)
- {
- return $value && !is_numeric($value) ? strtotime($value) : $value;
- }
- protected function setJointimeAttr($value)
- {
- return $value && !is_numeric($value) ? strtotime($value) : $value;
- }
- public function group()
- {
- return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- }
|