添加 init(envi) 方法来初始化

This commit is contained in:
陆柯 2022-03-09 16:01:21 +08:00
parent 3be713d74e
commit d7840a4efa

View File

@ -1,8 +1,14 @@
const JsonWebToken = require('jsonwebtoken')
const crypto = require('crypto')
const { workerData } = require('worker_threads')
const my = {}
module.exports = {
init (envi) {
my.tokenKey = envi.tokenKey
},
createToken: function (content, key) {
// content 可以是数字,非空字符串或非空对象,不可以是数组。
// key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值)
@ -11,7 +17,7 @@ module.exports = {
content,
crypto
.createHash('sha256')
.update(key || '', 'utf8')
.update(key || my.tokenKey || wo.envi.tokenKey || '', 'utf8')
.digest('hex')
)
} catch (exp) {
@ -24,7 +30,7 @@ module.exports = {
token,
crypto
.createHash('sha256')
.update(key || '', 'utf8')
.update(key || my.tokenKey || wo.envi.tokenKey || '', 'utf8')
.digest('hex')
)
} catch (exp) {