import ABaseTranslatorAbstract from './ABaseTranslatorAbstract' ; import { getYoudaoSign } from './../utils' ; class YoudaoTranslator extends ABaseTranslatorAbstract { apiUrl = 'https://openapi.youdao.com/api' ; configSection = 'youdao' ; getParams ( q ) { const youdaoConfig = this . getConfig ( this . configSection ) || { } ; const { secret , appKey } = youdaoConfig ; const salt = Date . now ( ) . toString ( ) ; const sign = getYoudaoSign ( { appKey , q , salt , secret } ) ; if ( ! secret || ! appKey ) { throw new Error ( '请配置有道api secret、appKey' ) ; } return { appKey , secret , salt , sign , q , from : 'auto' , to : 'auto' , } ; } getResultTitle ( ) { return 'youdao:' ; } getSpeakUrl ( result ) { return result . tSpeakUrl ; } parseRawResult ( result ) { const { web = [ ] , query = '' , translation = [ ] , errorCode = '' , basic = { } } = result ; const { explains } = basic ; if ( errorCode != 0 ) { return '❌' + errorCode ; } const arrayResult = [ ] ; if ( translation && translation . length ) { arrayResult . push ( ... translation . map ( ( item ) => `- ${ item }` ) ) ; } if ( explains && explains . length ) { arrayResult . push ( '基本释义:' ) ; arrayResult . push ( ... explains . map ( ( item ) => `- ${ item }` ) ) ; } if ( web && web . length ) { arrayResult . push ( '网络释义:' ) ; web . forEach ( ( item ) => { arrayResult . push ( [ item . key , item . value . join ( '、' ) ] ) ; } ) ; } return arrayResult ; } getReplaceableResult ( rawResult ) { return rawResult . translation || [ ] ; } } export default YoudaoTranslator ;