| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php namespace App\Models\Manager;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Facades\Servers\Encrypts\AccessToken;
- /**
- *用户模型
- *
- */
- class AdminUser extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'admin';
- // 是否主动维护时间戳
- public $timestamps = false;
- protected $connection = 'mysql';
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 如果操作失败
- if( !$id ) return 0;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['uid'=>$id])->update($data);
- // 如果操作失败
- if( !$result ) return 0;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $id;
- }
- /**
- * 获取列表
- * @param Bool $force 是否强制更新
- *
- */
- public function getList($force = false)
- {
- // 结果数据
- $list = $force ? [] : cache('manager:admin:user:list');
- // 不存在数据
- if ( !$list ) {
- // 从数据库获取数据
- $data = $this->query()->get(['uid','username','phone','status','password','status','insert_time','update_time'])->toArray();
- // 循环处理数据
- $list = [];
- // 进行更新
- foreach ($data as $value) {
- // 重组数据
- $list[$value['uid']] = $value;
- }
- // 存起来
- cache(['manager:admin:user:list'=>$list]);
- }
- // 返回结果
- return $list;
- }
- /**
- * 获取配置平台对应的应用数据
- *
- * @param int 用户ID
- * @param string 指定字段
- *
- */
- public function getOne($id,$field='')
- {
- // 获取列表数据
- $list = $this->getList();
- // 获取数据
- $one = isset($list[$id]) ? $list[$id] : [];
- // 返回值
- return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
- }
- /**
- * 登录
- *
- * @param int 用户ID
- *
- */
- public function Login($uid,$module='manager'){
- // 组合数据
- $jwtData = ['uid'=>$uid,'type'=>$module,'expire'=>time()+3600*8];
- // 登录成功
- $token = AccessToken::encode($jwtData);
- // 如果异常的话
- if( isset($token['error']) ) return $token;
- // 登录成功
- cache([$module.':admin:access_token:'.$uid=>md5($token)],3600*8);
- // 返回结果
- return ['access_token_'.$module=>$token,'expire'=>3600*8];
- }
- /**
- * 退出登录
- *
- * @param int 用户ID
- *
- */
- public function LoginOut($uid,$module='manager'){
- // 删除缓存
- cache([$module.':admin:access_token:'.$uid=>null]);
- // 返回结果
- return true;
- }
- /**
- * 获取登录信息
- */
- public function getLogin($uid,$module='manager'){
- // 删除缓存
- $result = cache($module.':admin:access_token:'.$uid);
- // 返回结果
- return $result;
- }
- }
|