UNPKG

will-lib

Version:
69 lines (68 loc) 2.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NewsAuthentication = void 0; const axios_1 = __importDefault(require("axios")); const AuthenError_1 = require("../AuthenError"); const HTTP_1 = require("../HTTP"); const EnvironmentVariable_1 = require("../EnvironmentVariable"); const BaseAuthentication_1 = require("./BaseAuthentication"); class NewsAuthentication extends BaseAuthentication_1.BaseAuthentication { constructor(url) { super(); this.url = EnvironmentVariable_1.NEWS_AUTHEN_URL; this.url = url; } static getInstance() { return new NewsAuthentication(EnvironmentVariable_1.NEWS_AUTHEN_URL); } getRequestUserInfo() { return { user: "", password: "", company: "FWG" }; } getHeaderConfig() { return { headers: { 'Content-Type': 'application/json', 'api-key': EnvironmentVariable_1.NEWS_AUTHEN_API_KEY } }; } async login(user, pwd, site) { let data = this.getRequestUserInfo(); data.user = user; data.password = pwd; if (site && site.trim().length > 0) data.company = site; let config = this.getHeaderConfig(); let resinfo = this.getResponseInfo(); let result = await axios_1.default.post(this.url, data, config).then((res) => { console.log("response data", JSON.stringify(res.data)); if (res.data.head) { if (res.data.head.status != "success") { let errmsg = res.data.head.statusmessage ? res.data.head.statusmessage : "Authen fail"; return Promise.reject(new AuthenError_1.AuthenError(errmsg, HTTP_1.HTTP.UNAUTHORIZED)); } if (res.data.body) { return { ...resinfo, accessToken: res.data.body.accessToken, appServerUrl: res.data.body.appServerUrl, auth_token: res.data.body.auth_token, userProfile: res.data.body.userProfile }; } } return Promise.reject(new AuthenError_1.AuthenError("Authen fail", HTTP_1.HTTP.UNAUTHORIZED)); }).catch(function (error) { return Promise.reject(error); }); return Promise.resolve(result); } } exports.NewsAuthentication = NewsAuthentication;