添加 init(envi) 方法来初始化
This commit is contained in:
		
							parent
							
								
									3be713d74e
								
							
						
					
					
						commit
						d7840a4efa
					
				
							
								
								
									
										10
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								index.js
									
									
									
									
									
								
							@ -1,8 +1,14 @@
 | 
				
			|||||||
const JsonWebToken = require('jsonwebtoken')
 | 
					const JsonWebToken = require('jsonwebtoken')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const crypto = require('crypto')
 | 
					const crypto = require('crypto')
 | 
				
			||||||
 | 
					const { workerData } = require('worker_threads')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const my = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
 | 
					  init (envi) {
 | 
				
			||||||
 | 
					    my.tokenKey = envi.tokenKey
 | 
				
			||||||
 | 
					  },    
 | 
				
			||||||
  createToken: function (content, key) {
 | 
					  createToken: function (content, key) {
 | 
				
			||||||
    // content 可以是数字,非空字符串或非空对象,不可以是数组。
 | 
					    // content 可以是数字,非空字符串或非空对象,不可以是数组。
 | 
				
			||||||
    // key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值)
 | 
					    // key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值)
 | 
				
			||||||
@ -11,7 +17,7 @@ module.exports = {
 | 
				
			|||||||
        content,
 | 
					        content,
 | 
				
			||||||
        crypto
 | 
					        crypto
 | 
				
			||||||
          .createHash('sha256')
 | 
					          .createHash('sha256')
 | 
				
			||||||
          .update(key || '', 'utf8')
 | 
					          .update(key || my.tokenKey || wo.envi.tokenKey || '', 'utf8')
 | 
				
			||||||
          .digest('hex')
 | 
					          .digest('hex')
 | 
				
			||||||
      )
 | 
					      )
 | 
				
			||||||
    } catch (exp) {
 | 
					    } catch (exp) {
 | 
				
			||||||
@ -24,7 +30,7 @@ module.exports = {
 | 
				
			|||||||
        token,
 | 
					        token,
 | 
				
			||||||
        crypto
 | 
					        crypto
 | 
				
			||||||
          .createHash('sha256')
 | 
					          .createHash('sha256')
 | 
				
			||||||
          .update(key || '', 'utf8')
 | 
					          .update(key || my.tokenKey || wo.envi.tokenKey || '', 'utf8')
 | 
				
			||||||
          .digest('hex')
 | 
					          .digest('hex')
 | 
				
			||||||
      )
 | 
					      )
 | 
				
			||||||
    } catch (exp) {
 | 
					    } catch (exp) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user