9 changed files with 221 additions and 63 deletions
			
			
		@ -0,0 +1,61 @@ | 
				
			|||
/* eslint-disable*/ | 
				
			|||
'use strict'; | 
				
			|||
 | 
				
			|||
module.exports = dc => { | 
				
			|||
   const DataTypes = dc.ORM; | 
				
			|||
   const sequelize = dc.orm; | 
				
			|||
   const SecretYingshi = sequelize.define("secretYingshi", { | 
				
			|||
      id: { | 
				
			|||
         type: DataTypes.INTEGER, | 
				
			|||
         allowNull: false, | 
				
			|||
         defaultValue: null, | 
				
			|||
         comment: null, | 
				
			|||
         primaryKey: true, | 
				
			|||
         field: "id", | 
				
			|||
         autoIncrement: true, | 
				
			|||
         unique: "secret_yingshi_id_uindex" | 
				
			|||
      }, | 
				
			|||
      key: { | 
				
			|||
         type: DataTypes.STRING, | 
				
			|||
         allowNull: false, | 
				
			|||
         defaultValue: null, | 
				
			|||
         comment: null, | 
				
			|||
         primaryKey: false, | 
				
			|||
         field: "key", | 
				
			|||
         autoIncrement: false | 
				
			|||
      }, | 
				
			|||
      secret: { | 
				
			|||
         type: DataTypes.STRING, | 
				
			|||
         allowNull: false, | 
				
			|||
         defaultValue: null, | 
				
			|||
         comment: null, | 
				
			|||
         primaryKey: false, | 
				
			|||
         field: "secret", | 
				
			|||
         autoIncrement: false | 
				
			|||
      }, | 
				
			|||
      token: { | 
				
			|||
         type: DataTypes.STRING, | 
				
			|||
         allowNull: true, | 
				
			|||
         defaultValue: null, | 
				
			|||
         comment: null, | 
				
			|||
         primaryKey: false, | 
				
			|||
         field: "token", | 
				
			|||
         autoIncrement: false | 
				
			|||
      }, | 
				
			|||
      expire: { | 
				
			|||
         type: DataTypes.STRING, | 
				
			|||
         allowNull: true, | 
				
			|||
         defaultValue: null, | 
				
			|||
         comment: null, | 
				
			|||
         primaryKey: false, | 
				
			|||
         field: "expire", | 
				
			|||
         autoIncrement: false | 
				
			|||
      } | 
				
			|||
   }, { | 
				
			|||
      tableName: "secret_yingshi", | 
				
			|||
      comment: "", | 
				
			|||
      indexes: [] | 
				
			|||
   }); | 
				
			|||
   dc.models.SecretYingshi = SecretYingshi; | 
				
			|||
   return SecretYingshi; | 
				
			|||
}; | 
				
			|||
@ -0,0 +1,49 @@ | 
				
			|||
'use strict'; | 
				
			|||
 | 
				
			|||
const moment = require('moment') | 
				
			|||
 | 
				
			|||
module.exports = function (app, opts) { | 
				
			|||
   async function token4yingshi ({ key, secret, token, expire } = {}) { | 
				
			|||
      const { models } = app.fs.dc | 
				
			|||
      if (!key || !secret) { | 
				
			|||
         throw '参数 { key, secret } 缺一不可' | 
				
			|||
      } | 
				
			|||
 | 
				
			|||
      if (token && expire && moment().isBefore(moment(expire))) { | 
				
			|||
         return token | 
				
			|||
      } else { | 
				
			|||
         const secretRes = await models.SecretYingshi.findOne({ | 
				
			|||
            where: { key, secret } | 
				
			|||
         }) | 
				
			|||
         if (secretRes && secretRes.expire && secretRes.token && moment().isBefore(moment(secretRes.expire))) { | 
				
			|||
            return secretRes.token | 
				
			|||
         } | 
				
			|||
      } | 
				
			|||
 | 
				
			|||
      // 也可以做基于 redis 的缓存
 | 
				
			|||
      const tokenRes = await app.fs.yingshiRequest.post(`lapp/token/get`, { | 
				
			|||
         query: { | 
				
			|||
            appKey: key, | 
				
			|||
            appSecret: secret | 
				
			|||
         } | 
				
			|||
      }) | 
				
			|||
      if (tokenRes.code == 200) { | 
				
			|||
         const { accessToken, expireTime } = tokenRes.data | 
				
			|||
         await models.SecretYingshi.update({ | 
				
			|||
            token: accessToken, | 
				
			|||
            expire: expireTime | 
				
			|||
         }, { | 
				
			|||
            where: { | 
				
			|||
               key, secret | 
				
			|||
            } | 
				
			|||
         }) | 
				
			|||
         return accessToken | 
				
			|||
      } else { | 
				
			|||
         throw tokenRes | 
				
			|||
      } | 
				
			|||
   } | 
				
			|||
 | 
				
			|||
   return { | 
				
			|||
      token4yingshi | 
				
			|||
   } | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue