Files
2025-11-29 10:40:16 +08:00

62 lines
2.3 KiB
PHP

<?php
require_once COMMPATH . 'libraries/JWT.php';
use \Firebase\JWT\JWT;
/**
* 验证token
*/
class ValidToken
{
private $jwt_key_system; //系统推送token
private $jwt_key_pingan; //平安后台
private $jwt_algorithm;
public function __construct()
{
$this->ci = &get_instance();
$this->ci->config->load('wss');
$this->jwt_key_pingan = $this->ci->config->item('jwt_key_pingan');
$this->jwt_key_system = $this->ci->config->item('jwt_key_system');
$this->jwt_algorithm = $this->ci->config->item('jwt_algorithm');
}
public function validateToken($token, $platform)
{
try {
$uid = 0;
switch ($platform) {
case Ws_conn_model::PLAT_FORM_ADMIN:
$uid = JWT::decode($token, $this->jwt_key_system, array($this->jwt_algorithm));
if (!$uid) {
throw new Exception('授权失败', WsResponse::CODE_UNAUTHORIZED);
}
break;
case Ws_conn_model::PLAT_FORM_PINGAN:
$uid = JWT::decode($token, $this->jwt_key_pingan, array($this->jwt_algorithm));
if (!$uid) {
throw new Exception('授权失败', WsResponse::CODE_UNAUTHORIZED);
}
break;
case Ws_conn_model::PLAT_FORM_WXAPP:
throw new Exception('暂不支持微信小程序');
break;
//服务端 推送数据
case Ws_conn_model::PLAT_FORM_SYS_ADMIN:
case Ws_conn_model::PLAT_FORM_SYS_PINGAN:
case Ws_conn_model::PLAT_FORM_SYS_WXAPP:
case Ws_conn_model::PLAT_FORM_SYS_H5:
$uid = JWT::decode($token, $this->jwt_key_system, array($this->jwt_algorithm));
if (!$uid) {
throw new Exception('授权失败', WsResponse::CODE_UNAUTHORIZED);
}
break;
default:
throw new Exception('平台参数错误');
}
return new MyResponse(WsResponse::CODE_SUCCESS, '验证成功', ['uid' => $uid]);
} catch (Exception $e) {
return new MyResponse($e->getCode() ?: WsResponse::CODE_ERROR, $e->getMessage());
}
}
}