diff --git a/auth/index.js b/auth/index.js index 3ad2700..2a95d28 100644 --- a/auth/index.js +++ b/auth/index.js @@ -3,25 +3,14 @@ const crypto = require('crypto'); const Redis = require('ioredis'); -function AuthToken(redisOpt) { - if (!(this instanceof AuthToken)) { - return new AuthToken(redisOpt); - } - - if (!redisOpt) { - throw 'AuthToken: need redis config.'; - } - - this.tokenDB = new Redis(redisOpt); - this.tokenDB.on("connect",function(){ - console.log("AuthToken: ioredis connected: " + JSON.stringify(redisOpt)); - }); +function AuthToken(redisdb) { + this.tokenDB = redisdb; } AuthToken.prototype.genToken = async function(userData, expiresSeconds) { // 生成系统内部的user token let hash = crypto.createHash('md5'); - hash.update(JSON.stringify(userData)); + hash.update(JSON.stringify(userData) + Date()); let userToken = hash.digest('hex'); // 缓存到redis @@ -109,4 +98,10 @@ AuthToken.prototype.getTokenData = async function (userToken) { return tokenData; }; -module.exports = AuthToken; +let tokenInstance = null; +module.exports = function getTokenInstance(redisPort, redisHost, db) { + if (!tokenInstance) { + tokenInstance = new AuthToken(redisPort, redisHost, db); + } + return tokenInstance; +}; diff --git a/auth/package.json b/auth/package.json index 781914f..9485b1f 100644 --- a/auth/package.json +++ b/auth/package.json @@ -1,6 +1,6 @@ { "name": "@ehason/auth", - "version": "1.0.2", + "version": "1.2.2", "description": "User auth lib", "main": "index.js", "scripts": { diff --git a/utils/impl/koaresp.js b/utils/impl/koaresp.js new file mode 100644 index 0000000..3b37361 --- /dev/null +++ b/utils/impl/koaresp.js @@ -0,0 +1,15 @@ +'use strict'; + +function ctxresp(ctx, data, errno, errmsg) { + if (errno === null || errno === undefined) errno = 0; + if (errmsg === null || errmsg === undefined) errmsg = ''; + if (data === null || data === undefined) data = {}; + + ctx.body = { + result: errno === 0 ? 'ok' : 'fail', + error: { code: errno, msg: errmsg }, + data: data + }; +} + +module.exports = ctxresp; diff --git a/utils/index.js b/utils/index.js index 2a4f21c..47d7597 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,4 +1,5 @@ module.exports = { - prxrmq: require('./impl/prxrmq') + prxrmq: require('./impl/prxrmq'), + ctxresp: require('./impl/koaresp') }; diff --git a/utils/package.json b/utils/package.json index 1ac9593..4c0f354 100644 --- a/utils/package.json +++ b/utils/package.json @@ -1,6 +1,6 @@ { "name": "@ehason/utils", - "version": "1.0.1", + "version": "1.0.2", "description": "Utils for ehason develope", "main": "index.js", "scripts": {