UNPKG

what-is-surprise

Version:

translate tool

92 lines (91 loc) 3.59 kB
var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "crypto-js", "../../utils"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YoudaoFanyiConfig = void 0; var crypto_js_1 = __importDefault(require("crypto-js")); var utils_1 = require("../../utils"); var YoudaoFanyiConfig = /** @class */ (function () { function YoudaoFanyiConfig(config) { this.q = ''; this.from = 'auto'; this.to = 'auto'; this.appKey = ''; this.appSecret = ''; this.salt = Date.now().toString(); this.sign = ''; this.signType = 'v3'; this.curtime = Date.now().toString(); Object.assign(this, config); } YoudaoFanyiConfig.prototype.update = function (part) { Object.assign(this, part); }; YoudaoFanyiConfig.prototype.updateQuery = function (query) { this.q = query; return this; }; YoudaoFanyiConfig.prototype.updateAppKey = function (appKey) { this.appKey = appKey; return this; }; YoudaoFanyiConfig.prototype.updateAppSecret = function (appSecret) { this.appSecret = appSecret; return this; }; YoudaoFanyiConfig.prototype.updateSalt = function (salt) { if (salt === void 0) { salt = Date.now().toString(); } this.salt = salt; return this; }; YoudaoFanyiConfig.prototype.udpateCurtime = function (curtime) { if (curtime === void 0) { curtime = new Date().toString(); } this.curtime = curtime; return this; }; YoudaoFanyiConfig.prototype.setLangFrom = function (lang) { this.from = lang; return this; }; YoudaoFanyiConfig.prototype.setLangTo = function (lang) { this.to = lang; return this; }; /** * 签名生成 */ YoudaoFanyiConfig.prototype.updateSign = function () { var _a = this, q = _a.q, salt = _a.salt, curtime = _a.curtime, appKey = _a.appKey, appSecret = _a.appSecret; /** * 签名生成方法如下: * signType=v3; * sign=sha256(应用ID+input+salt+curtime+应用密钥); * 其中,input的计算方式为: * - input=q前10个字符 + q长度 + q后10个字符(当q长度大于20) * - input=q字符串(当q长度小于等于20) */ var input = q.length > 20 ? q.slice(0, 10) + q.length + q.slice(-10) : q; this.sign = crypto_js_1.default.SHA256(appKey + input + salt + curtime + appSecret).toString(); }; /** * 创建冻结副本 */ YoudaoFanyiConfig.prototype.release = function () { return Object.freeze(utils_1.omit(this, ['release', 'updateSign'])); }; return YoudaoFanyiConfig; }()); exports.YoudaoFanyiConfig = YoudaoFanyiConfig; });