You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.1 KiB
79 lines
2.1 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\service;
|
|
|
|
use app\api\model\User;
|
|
|
|
// for JWT
|
|
use Lcobucci\JWT\Configuration;
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
|
use DateTimeImmutable;
|
|
use Lcobucci\JWT\Token\Plain;
|
|
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
|
|
use Lcobucci\JWT\Validation\Constraint\SignedWith;
|
|
|
|
/**
|
|
* 用户表
|
|
*/
|
|
class UserService
|
|
{
|
|
/**
|
|
* 用户登录操作,传入用户查询后才对比密码
|
|
* @param string $uname
|
|
* @param string $pass
|
|
* @return User|array|false|int|mixed|\think\Model
|
|
* @throws \cores\exception\BaseException
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function login(string $uname, string $pass)
|
|
{
|
|
// query db
|
|
$drs = User::where(['nick_name' => $uname, 'delete_time' => 0])->find();
|
|
// 异常处理
|
|
if (!isset($drs)) {
|
|
throwError('用户不存在');
|
|
return -1;
|
|
} else {
|
|
$fpass = password($pass . $drs['salt']);
|
|
// var_dump($fpass.'|'.$drs['password']);
|
|
// exit;
|
|
// 对比密码
|
|
if ($drs['password'] != $fpass) {
|
|
throwError('密码错误');
|
|
return false;
|
|
}
|
|
// 返回用户信息
|
|
return $drs;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public function register(array $data): bool
|
|
{
|
|
$arr = [
|
|
'nick_name' => $data['uname'],
|
|
'password' => $data['upass'],
|
|
'mobile' => $data['phone'],
|
|
];
|
|
$salt = makeSalt(6);
|
|
// 密码加密
|
|
$arr['password'] = password($arr['password'] . $salt);
|
|
// 生成salt
|
|
$arr['salt'] = $salt;
|
|
$dtime = time();
|
|
$arr['create_time'] = $dtime;
|
|
$arr['update_time'] = $dtime;
|
|
// 保存
|
|
$model = new User;
|
|
$uid = $model->save($arr);
|
|
return (bool)$uid;
|
|
}
|
|
}
|