import { Service , Inject } from 'typedi' ; import winston from 'winston' ; import { createRandomString } from '../config/util' ; import config from '../config' ; import DataStore from 'nedb' ; import { App , AppModel } from '../data/open' ; import { v4 as uuidV4 } from 'uuid' ; import sequelize , { Op } from 'sequelize' ; @ Service ( ) export default class OpenService { constructor ( @ Inject ( 'logger' ) private logger ) { } public async findTokenByValue ( token ) { const docs = await this . find ( { } ) ; const doc = docs . filter ( ( x ) => x . tokens ?. find ( ( y ) => y . value === token ) ) ; return doc [ 0 ] ; } public async create ( payload ) { const tab = { ... payload } ; tab . client_id = createRandomString ( 12 , 12 ) ; tab . client_secret = createRandomString ( 24 , 24 ) ; const doc = await this . insert ( tab ) ; return { ... doc , tokens : [ ] } ; } public async insert ( payloads ) { const doc = await AppModel . create ( payloads , { returning : true } ) ; return doc . get ( { plain : true } ) as App ; } public async update ( payload ) { const newDoc = await this . updateDb ( { name : payload . name , scopes : payload . scopes , id : payload . id , } as any ) ; return { ... newDoc , tokens : [ ] } ; } private async updateDb ( payload ) { await AppModel . update ( payload , { where : { id : payload . id } } ) ; return await this . getDb ( { id : payload . id } ) ; } public async getDb ( query ) { const doc = await AppModel . findOne ( { where : query } ) ; return doc && ( doc . get ( { plain : true } ) as App ) ; } public async remove ( ids ) { await AppModel . destroy ( { where : { id : ids } } ) ; } public async resetSecret ( id ) { const tab = { client_secret : createRandomString ( 24 , 24 ) , tokens : [ ] , id , } ; const newDoc = await this . updateDb ( tab ) ; return newDoc ; } public async list ( searchText = '' , sort = { } , query = { } ) { let condition = { ... query } ; if ( searchText ) { const encodeText = encodeURIComponent ( searchText ) ; const reg = { [ Op . or ] : [ { [ Op . like ] : `%${ searchText }%` } , { [ Op . like ] : `%${ encodeText }%` } , ] , } ; condition = { ... condition , name : reg , } ; } try { const result = await this . find ( condition ) ; return result . map ( ( x ) => ( { ... x , tokens : [ ] } ) ) ; } catch ( error ) { throw error ; } } private async find ( query , sort ? ) { const docs = await AppModel . findAll ( { where : { ... query } } ) ; return docs . map ( ( x ) => x . get ( { plain : true } ) ) ; } public async authToken ( { client_id , client_secret , } ) { const token = uuidV4 ( ) ; const expiration = Math . round ( Date . now ( ) / 1000 ) + 2592000 ; const doc = await AppModel . findOne ( { where : { client_id , client_secret } } ) ; if ( doc ) { await AppModel . update ( { tokens : [ ... ( doc . tokens || [ ] ) , { value : token , expiration } ] } , { where : { client_id , client_secret } } ) ; return { code : 200 , data : { token , token_type : 'Bearer' , expiration , } , } ; } else { return { code : 400 , message : 'client_id或client_seret有误' } ; } } }