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.

78 lines
2.3 KiB

<?php
namespace app\service\user;
use app\model\Admin;
use app\service\BaseService;
use fast\Http;
use Firebase\JWT\JWT;
class LoginService extends BaseService
{
public function __construct()
{
parent::__construct();
}
public function checkUser($account_id, $nick){
// 权限校验
// $path = env('app.real_url') . "/adminmgt/v1/permission/getPathByNick";
$path = env('app.real_url') . "/users/adminmgt/v1/permission/getPathByNick";
$path .= "?accountId={$account_id}";
$sign = parent::createSign("user_real");
$headers = array(
CURLOPT_HTTPHEADER => array(
"api_token:{$sign}",
"nick:{$nick}",
"Content-Type: application/json"
)
);
$result = Http::get($path, [], $headers);
if($result['code'] != 200){
throw new \think\Exception($result['msg'], 400);
}
$res = json_decode($result['data'], true);
if($res['resultCode'] != '00000000'){
throw new \think\Exception($res['resultMsg'], 400);
}
return $res['data'];
}
public function adminLogin($phone, $password){
$result = Admin::where('username', $phone)->where('is_deleted', 0)->find();
if($result){
if($result->status == 1){
throw new \think\Exception('用户已失效', 400);
}
if(password_verify($password, $result->password)){
unset($result->password);
// 生成token
$encrypt = array(
'lat' => config('jwt.lat'),
'nbf' => config('jwt.nbf'),
'exp' => config('jwt.exp'),
'user_id' => $result->id,
'role_ids' => $result->role_id,
'username' => $result->username,
);
$token =JWT::encode($encrypt, config('jwt.key'), 'HS256');
$result->logintime = date('Y-m-d H:i:s');
$result->token = $token;
$result->save();
$result = $result->toArray();
return $result;
}
}
throw new \think\Exception('用户名或密码错误!', 400);
}
}