lihai-oa/app/api/controller/User.php

117 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/Apache-2.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api\controller;
use app\api\BaseController;
use app\api\middleware\Auth;
use app\user\validate\AdminCheck;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\facade\Request;
use think\exception\ValidateException;
class User extends BaseController
{
/**
* 控制器中间件 [登录不需要鉴权]
* @var array
*/
protected $middleware = [
Auth::class => ['except' => ['login']]
];
/**
* @param $user_id
* @return string
*/
public function getToken($user_id){
$time = time(); //当前时间
$conf = $this->jwt_conf;
$token = [
'iss' => $conf['iss'], //签发者 可选
'aud' => $conf['aud'], //接收该JWT的一方可选
'iat' => $time, //签发时间
'nbf' => $time-1 , //(Not Before)某个时间点后才能访问比如设置time+30表示当前时间30秒后才能使用
'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时
'data' => [
//自定义信息,不要定义敏感信息
'userid' =>$user_id,
]
];
return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256'
}
public function login()
{
$param = get_params();
if (empty($param['username']) || empty($param['password'])) {
$this->apiError('用户名密码不能为空');
}
// 校验用户名密码
$user = Db::name('Admin')->where(['username' => $param['username']])->find();
if (empty($user)) {
$this->apiError('帐号或密码错误');
}
$param['pwd'] = set_password($param['password'], $user['salt']);
if ($param['pwd'] !== $user['pwd']) {
$this->apiError('帐号或密码错误');
}
if ($user['status'] == -1) {
$this->apiError('该用户禁止登录,请于平台联系');
}
$data = [
'last_login_time' => time(),
'last_login_ip' => request()->ip(),
'login_num' => $user['login_num'] + 1,
];
$res = Db::name('Admin')->where(['id' => $user['id']])->update($data);
if ($res) {
$token = self::getToken($user['id']);
$this->apiSuccess('登录成功', ['token' => $token]);
}
}
public function userinfo(Request $request)
{
$uid = $this->uid;
$userInfo = Db::name('Admin')->where(['id' => $uid])->find();
$this->apiSuccess('请求成功', ['user' => $userInfo]);
}
public function editPassword(Request $request)
{
$param = get_params();
try {
validate(AdminCheck::class)->scene('editPwd')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
$this->apiError($e->getError());
}
$uid = $this->uid;
$admin = Db::name('Admin')->where(['id' => $uid])->find();
$old_psw = set_password($param['old_pwd'], $admin['salt']);
if ($admin['pwd'] != $old_psw) {
$this->apiError('旧密码错误');
}
$salt = set_salt(20);
$new_pwd = set_password($param['pwd'], $salt);
$data = [
'reg_pwd' => '',
'salt' => $salt,
'pwd' => $new_pwd,
'update_time' => time(),
];
Db::name('Admin')->where(['id' => $uid])->strict(false)->field(true)->update($data);
$this->apiSuccess('请求成功', ['user' => $userInfo]);
}
}