Files
ClassContent/项目合集/电商App/shop_backend/utils/Validator.js
2025-04-12 10:54:26 +08:00

35 lines
1.1 KiB
JavaScript
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.

const config = require("../config")
const rsa = require("../utils/rsa")
const jwt = require('jsonwebtoken');
/**
* 拦截器,验证 tokendata
* @type {{Validator(*, *, *): void}}
*/
module.exports = {
Validator(req, res, next) {
if (req.url == config.filter.login || req.url == config.filter.add
|| req.url == config.filter.home
) {
next()
} else {
if (req.headers.hasOwnProperty("token")) {
jwt.verify(req.headers.token, config.jwt, function (err, decode) {
if (err) {
res.json({
status: 403,
message: `身份凭证失效或不存在,请登录!${err.message}`
})
} else {
next();
}
});
} else { // 无token
res.json({
status: 403,
message: '无权限访问接口,您需要先登录才能使用!'
})
}
}
}
}