客户与跟进
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace app\util;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Firebase\JWT\ExpiredException;
|
||||
use Firebase\JWT\SignatureInvalidException;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
class JwtUtil
|
||||
{
|
||||
/**
|
||||
* 生成 JWT Token
|
||||
* @param int|string $userId
|
||||
* @return string
|
||||
*/
|
||||
public static function generateToken($userId): string
|
||||
{
|
||||
$config = config('jwt');
|
||||
$payload = [
|
||||
'iss' => 'webman', // 签发者
|
||||
'aud' => 'client', // 接收方
|
||||
'iat' => time(), // 签发时间
|
||||
'exp' => time() + $config['expire'], // 过期时间
|
||||
'sub' => $userId // 用户标识
|
||||
];
|
||||
return JWT::encode($payload, $config['secret'], $config['algorithm']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证并解析 Token
|
||||
* @param string $token
|
||||
* @return object
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public static function verifyToken(string $token)
|
||||
{
|
||||
$config = config('jwt');
|
||||
try {
|
||||
return JWT::decode($token, new Key($config['secret'], $config['algorithm']));
|
||||
} catch (ExpiredException $e) {
|
||||
throw new BusinessException('Token 已过期', 401);
|
||||
} catch (SignatureInvalidException $e) {
|
||||
throw new BusinessException('Token 无效', 401);
|
||||
} catch (\Exception $e) {
|
||||
throw new BusinessException('鉴权失败', 401);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace app\util;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Firebase\JWT\ExpiredException;
|
||||
use Firebase\JWT\SignatureInvalidException;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
|
||||
class Util
|
||||
{
|
||||
/**
|
||||
* 参数是否为空
|
||||
*/
|
||||
public static function isEmpty($param)
|
||||
{
|
||||
if(!empty($param) || $param == 0){
|
||||
$result = false;
|
||||
}else{
|
||||
$result = true;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数是否为空
|
||||
*/
|
||||
public static function getParam(Request $request,$param,$default='')
|
||||
{
|
||||
$param = $request->input($param,$default);
|
||||
return $param;
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static function fail($data = [],$message = '', $code = '500')
|
||||
{
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => $data
|
||||
];
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static function success($data = [],$message = '', $code = '200')
|
||||
{
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => $data
|
||||
];
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询返回
|
||||
*/
|
||||
public static function page($data,$message = '', $code = '200')
|
||||
{
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'message' => $message
|
||||
];
|
||||
|
||||
$result['data'] = [
|
||||
'list' => $data->items(), // 当前页数据
|
||||
'total' => $data->total(), // 总记录数
|
||||
'current_page' => $data->currentPage(),
|
||||
'per_page' => $data->perPage(),
|
||||
'last_page' => $data->lastPage()
|
||||
];
|
||||
return json($result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user