123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- declare (strict_types = 1);
- namespace app\model\admin;
- use library\basic\BaseModel;
- use library\traits\JwtAuthModelTrait;
- use library\traits\ModelTrait;
- use think\facade\Cache;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class Admin extends BaseModel
- {
- use ModelTrait;
- use JwtAuthModelTrait;
-
- private $sassId = 0;
- public function setSassId($sassid) {
- $this->sassId = $sassid;
- }
- /**
- * 管理注册写入
- * @param $post
- */
- public static function saveAdmin($post) {
- $save = ['name' => $post['name'],
- 'role_id' => $post['role_id'],
- 'avatar' => $post['avatar'],
- 'status' => $post['status'],
- 'sassid' => $post['sassid']
- ];
- if(empty($post['sassid'])){
- return self::setErrorInfo('站点不存在');
- }
- //是否密码写入
- if(!empty($post['password'])) {
- $save['password'] = password_hash($post['password'],PASSWORD_BCRYPT);
- }
- if($post['id']) {
- $info = self::where("username",$post['username'])->where('sassid',$save['sassid'])->find();
- if(!empty($info) && $info['id'] != $post['id'] ) {
- return self::setErrorInfo('登录的账号已存在');
- }
- //判断密码
- if(empty($save['password'])) {
- unset($post['password']);
- }
- self::where('id',$post['id'])->save($save);
- } else {
- $save['time'] = time();
- $save['last_time'] = time();
- $save['username'] = $post['username'];
- $count = self::where("username",$post['username'])->where('sassid',$save['sassid'])->count();
- if($count > 0) {
- return self::setErrorInfo('登录的账号已存在');
- }
- if(empty($save['password'])) {
- return self::setErrorInfo('请输入登录密码');
- }
- self::insert($save);
- }
- return true;
- }
-
- /**
- * 后台管理人员登录
- * @param $username
- * @param $pwd
- */
- public static function login($username, $pwd,$sassid){
- $adminInfo = self::get(compact('username','sassid'));
- if(empty($adminInfo)) return self::setErrorInfo('登录的账号不存在');
- if (!$adminInfo['status']) return self::setErrorInfo('账户以后被停用!');
- if (!password_verify($pwd, $adminInfo['password'])) return self::setErrorInfo('账号或密码错误,请重新输入');
- //发生事件
- event('AdminLoginAfter', [$adminInfo]);
- return $adminInfo;
- }
- /**
- * 验证密码
- * @param $id 管理ID
- * @param $pwd 旧密码
- * @return bool
- */
- public static function checkPassowrd($id,$pwd) {
- $adminInfo = self::get(compact('id'));
- if(empty($adminInfo)) return self::setErrorInfo('登录的账号不存在');
- if (!password_verify($pwd, $adminInfo['password'])){
- return false;
- }
- return true;
- }
- /**
- * 生成token存放令牌 | 存放redis
- * @param Admin $adminInfo
- * @param $type
- * @return array|bool
- */
- public static function createToken(Admin $adminInfo,$type)
- {
- $tokenInfo = $adminInfo->getToken($type, []);
- $exp = (int)bcadd((string)($tokenInfo['params']['exp'] - $tokenInfo['params']['iat']), "60", 0);
- try {
- $res = Cache::store('redis')->set($tokenInfo['token'], ['token' => $tokenInfo['token'], 'exp' => $exp], (int)$exp);
- } catch (\Throwable $e) {
- return self::setErrorInfo('保存token失败');
- }
- return $tokenInfo;
- }
-
-
- }
|