<?php
declare (strict_types = 1);

namespace app\model\warehouse;

use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\facade\Cache;
use think\Model;

/**
 * @mixin \think\Model
 */
class Warehouse extends BaseModel
{
    use ModelTrait;
    use JwtAuthModelTrait;
    //


    /**
     * 后台管理人员登录
     * @param $username
     * @param $pwd
     */
    public static function login($username, $pwd){
        $adminInfo = self::get(compact('username'));
        if(empty($adminInfo)) return self::setErrorInfo('登录的账号不存在');
        if (!$adminInfo['status']) return self::setErrorInfo('账户以后被停用!');
        if($adminInfo['password'] != md5($pwd)) return self::setErrorInfo('账号或密码错误，请重新输入');
     //   if (!password_verify($pwd, $adminInfo['password'])) return self::setErrorInfo('账号或密码错误，请重新输入');
        return $adminInfo;
    }



    /**
     * 生成token存放令牌 | 存放redis
     * @param Admin $adminInfo
     * @param $type
     * @return array|bool
     */
    public static function createToken(Warehouse $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;
    }



}
