Manager.php 1.3 KB
<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2019/11/26
 * Time: 10:49
 */

namespace app\api\model;


use think\Cache;

class Manager extends Base
{
    protected $autoWriteTimestamp = 'datetime';

    /**
     * 登录 @param $account:账号 $password:密码
     */
    public function login($username,$password){
        //获取当前用户的账号详情
        $info = $this->read(['username'=>$username,'password'=>$password]);
        if(empty($info) || $info['status'] == 1){
            return [];
        }
        //更新数据
        $token  = sha1(uniqid());
        $last_login_time = date('Y-m-d H:i:s',time());
        $last_ip = request()->ip();
        //更新登录时间与登录ip
        $rs = $this->edit(['token'=>$token,'update_time'=>$last_login_time,'last_ip'=>$last_ip],['id'=>$info['id']]);
        if($rs === false){
            return [];
        }
        //清空上一次缓存,实现单点登录
        Cache::rm($info['token']);
        //更新
        $info['token'] = $token;
        $info['update_time'] = $last_login_time;
        $info['last_ip'] = $last_ip;
        //过滤密码
        unset($info['password']);
        //生成缓存
        $expire = 3600 * 24 * 7;
        Cache::set($token, $info, $expire);
        return $info;
    }
}