From 38e689ebb22e5334c7704f37893d537fed9bd71c Mon Sep 17 00:00:00 2001 From: hide_d Date: Sun, 26 Dec 2021 01:58:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20api.php=EB=A5=BC=20sammoAPI()=EB=A1=9C?= =?UTF-8?q?=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/PageInheritPoint.vue | 71 ++++++------------------------------- hwe/ts/PageJoin.vue | 16 ++------- hwe/ts/gateway/login.ts | 64 +++++++++++++++------------------ hwe/ts/util/sammoAPI.ts | 13 +++++-- hwe/ts/v_processing.ts | 36 ++++++------------- 5 files changed, 61 insertions(+), 139 deletions(-) diff --git a/hwe/ts/PageInheritPoint.vue b/hwe/ts/PageInheritPoint.vue index 7bdc19e1..b620d35d 100644 --- a/hwe/ts/PageInheritPoint.vue +++ b/hwe/ts/PageInheritPoint.vue @@ -265,6 +265,7 @@ import _ from "lodash"; import { InvalidResponse } from "@/defs"; import axios from "axios"; import NumberInputWithInfo from "@/components/NumberInputWithInfo.vue"; +import { sammoAPI } from "./util/sammoAPI"; type InheritanceType = | "previous" @@ -505,24 +506,11 @@ export default defineComponent({ return; } - let result: InvalidResponse; try { - const response = await axios({ - url: "api.php", - method: "post", - responseType: "json", - data: { - path: "InheritAction/BuyHiddenBuff", - args: { - type: buffKey, - level, - }, - }, + await sammoAPI("InheritAction/BuyHiddenBuff", { + type: buffKey, + level, }); - result = response.data; - if (!result.result) { - throw result.reason; - } } catch (e) { console.error(e); alert(`실패했습니다: ${e}`); @@ -561,21 +549,8 @@ export default defineComponent({ return; } - let result: InvalidResponse; try { - const response = await axios({ - url: "api.php", - method: "post", - responseType: "json", - data: { - path: `InheritAction/${type}`, - args: {}, - }, - }); - result = response.data; - if (!result.result) { - throw result.reason; - } + await sammoAPI(`InheritAction/${type}`, {}); } catch (e) { console.error(e); alert(`실패했습니다: ${e}`); @@ -608,23 +583,10 @@ export default defineComponent({ return; } - let result: InvalidResponse; try { - const response = await axios({ - url: "api.php", - method: "post", - responseType: "json", - data: { - path: `InheritAction/SetNextSpecialWar`, - args: { - type: this.nextSpecialWar, - }, - }, + await sammoAPI(`InheritAction/SetNextSpecialWar`, { + type: this.nextSpecialWar, }); - result = response.data; - if (!result.result) { - throw result.reason; - } } catch (e) { console.error(e); alert(`실패했습니다: ${e}`); @@ -653,24 +615,11 @@ export default defineComponent({ return; } - let result: InvalidResponse; try { - const response = await axios({ - url: "api.php", - method: "post", - responseType: "json", - data: { - path: `InheritAction/BuySpecificUnique`, - args: { - item: this.specificUnique, - amount, - }, - }, + await sammoAPI(`InheritAction/BuySpecificUnique`, { + item: this.specificUnique, + amount, }); - result = response.data; - if (!result.result) { - throw result.reason; - } } catch (e) { console.error(e); alert(`실패했습니다: ${e}`); diff --git a/hwe/ts/PageJoin.vue b/hwe/ts/PageJoin.vue index f8f351e5..78ec6133 100644 --- a/hwe/ts/PageJoin.vue +++ b/hwe/ts/PageJoin.vue @@ -318,6 +318,7 @@ import { clone, shuffle, sum } from "lodash"; import axios from "axios"; import { InvalidResponse } from "@/defs"; import NumberInputWithInfo from "@/components/NumberInputWithInfo.vue"; +import { sammoAPI } from "./util/sammoAPI"; declare const nationList: { nation: number; @@ -534,21 +535,8 @@ export default defineComponent({ async submitForm() { //검증은 언제 되어야 하는가? const args = clone(this.args); - let result: InvalidResponse; try { - const response = await axios({ - url: "api.php", - method: "post", - responseType: "json", - data: { - path: ["General", "Join"], - args, - }, - }); - result = response.data; - if (!result.result) { - throw result.reason; - } + await sammoAPI(["General", "Join"], args); } catch (e) { console.error(e); alert(`실패했습니다: ${e}`); diff --git a/hwe/ts/gateway/login.ts b/hwe/ts/gateway/login.ts index b1ca1860..b1cd3641 100644 --- a/hwe/ts/gateway/login.ts +++ b/hwe/ts/gateway/login.ts @@ -12,16 +12,22 @@ import { InvalidResponse } from '@/defs'; import { delay } from '@util/delay'; import { Modal } from 'bootstrap'; import '@/gateway/common'; +import { sammoAPI } from '@/util/sammoAPI'; +import { isString } from 'lodash'; type LoginResponse = { result: true, nextToken: [number, string] | undefined, -} | { +} + +type LoginFailed = { result: false, reqOTP: boolean, reason: string, } +type LoginResponseWithKakao = LoginResponse | LoginFailed; + type OTPResponse = { result: true, validUntil: string, @@ -31,15 +37,18 @@ type OTPResponse = { reason: string, } + type AutoLoginNonceResponse = { result: true, loginNonce: string, -} | InvalidResponse; +}; type AutoLoginResponse = { result: true, nextToken: [number, string] | undefined, -} | { +} + +type AutoLoginFailed = { result: false, silent: boolean, reason: string, @@ -92,36 +101,26 @@ async function tryAutoLogin() { const [tokenID, token] = tokenInfo; - const result = await axios.post('api.php', { - path: ["Login", "ReqNonce"] - }); + const result = await sammoAPI(["Login", "ReqNonce"], {}, true); if (!result) { //api 에러. return; } - if (!result.data.result) { + if (!result.result) { resetToken(); return; } - const nonce = result.data.loginNonce; + const nonce = result.loginNonce; const hashedToken = sha512(token + nonce); - const _loginResult = await axios.post('api.php', { - path: ["Login", "LoginByToken"], - args: { - 'hashedToken': hashedToken, - 'token_id': tokenID, - } - }); + const loginResult = await sammoAPI(["Login", "LoginByToken"], { + 'hashedToken': hashedToken, + 'token_id': tokenID, + }, true); - if (!_loginResult) { - return; - } - - const loginResult = _loginResult.data; if (!loginResult.result) { if (!loginResult.silent) { alert(loginResult.reason); @@ -137,6 +136,9 @@ async function tryAutoLogin() { } catch (e) { + if (isString(e)) { + alert(e); + } console.error(e); return; } @@ -201,7 +203,7 @@ async function sendTempPasswordToKakaoTalk(): Promise { } async function doLoginUsingOAuth() { - let result: LoginResponse; + let result: LoginResponseWithKakao; try { const response = await axios({ @@ -307,22 +309,14 @@ $(async function ($) { const salt = unwrap_any($('#global_salt').val()); const hash_pw = sha512(salt + raw_password + salt); - let result: LoginResponse; + let result: LoginResponse | LoginFailed; try { - const response = await axios({ - url: 'api.php', - responseType: 'json', - method: 'post', - data: { - path: ["Login", "LoginByID"], - args: { - username: values.username, - password: hash_pw, - } - } - }); - result = response.data; + result = await sammoAPI(["Login", "LoginByID"], { + username: values.username, + password: hash_pw, + + }, true); } catch (e) { console.error(e); diff --git a/hwe/ts/util/sammoAPI.ts b/hwe/ts/util/sammoAPI.ts index 36d9e052..e92db5f9 100644 --- a/hwe/ts/util/sammoAPI.ts +++ b/hwe/ts/util/sammoAPI.ts @@ -2,11 +2,15 @@ import axios from "axios"; import { isArray } from "lodash"; import { InvalidResponse } from '@/defs'; -type ValidResponse = { +export type ValidResponse = { result: true } -export async function sammoAPI(path: string | string[], args?: Record): Promise { +export async function sammoAPI(path: string | string[], args?: Record): Promise; +export async function sammoAPI(path: string | string[], args: Record | undefined, returnError: false): Promise; +export async function sammoAPI(path: string | string[], args: Record | undefined, returnError: true): Promise; + +export async function sammoAPI(path: string | string[], args?: Record, returnError = false): Promise { if (isArray(path)) { path = path.join('/'); } @@ -20,8 +24,11 @@ export async function sammoAPI(path: string | args, }, }); - const result: InvalidResponse | ResultType = response.data; + const result: ErrorType | ResultType = response.data; if (!result.result) { + if (returnError) { + return result; + } throw result.reason; } return result; diff --git a/hwe/ts/v_processing.ts b/hwe/ts/v_processing.ts index 0624074a..b123f2cc 100644 --- a/hwe/ts/v_processing.ts +++ b/hwe/ts/v_processing.ts @@ -1,9 +1,5 @@ import '@scss/processing.scss'; -import axios from 'axios'; -import { setAxiosXMLHttpRequest } from '@util/setAxiosXMLHttpRequest'; -import { convertFormData } from '@util/convertFormData'; -import { InvalidResponse } from '@/defs'; import { unwrap } from "@util/unwrap"; import BootstrapVue3 from 'bootstrap-vue-3' import Multiselect from 'vue-multiselect'; @@ -13,36 +9,24 @@ import { App, createApp } from 'vue'; import { auto500px } from './util/auto500px'; import { isString } from 'lodash'; import { Args, testSubmitArgs } from './processing/args'; +import { sammoAPI, ValidResponse } from './util/sammoAPI'; declare const turnList: number[]; -setAxiosXMLHttpRequest(); - -async function submitCommand(isChiefTurn: boolean, turnList: number[], command: string, args: Args): Promise { - - - const target = isChiefTurn ? 'j_set_chief_command.php' : 'j_set_general_command.php'; +async function submitCommand(isChiefTurn: boolean, turnList: number[], action: string, arg: Args): Promise { + const target = isChiefTurn ? 'Command/ReserveCommand' : 'NationCommand/ReserveCommand'; try { - const testResult = testSubmitArgs(args); + const testResult = testSubmitArgs(arg); if (testResult !== true) { throw new TypeError(`Invalied Type ${testResult[0]}, ${testResult[2]} should be ${testResult[1]}`); } - console.log('trySubmit', args); - const response = await axios({ - url: target, - responseType: 'json', - method: 'post', - data: convertFormData({ - action: command, - turnList: turnList, - arg: JSON.stringify(args) - }) + console.log('trySubmit', arg); + const response = await sammoAPI(target, { + action, + turnList, + arg, }); - const data = response.data as InvalidResponse; - if (!data.result) { - throw data.reason; - } if (!isChiefTurn) { window.location.href = './'; @@ -50,7 +34,7 @@ async function submitCommand(isChiefTurn: boolean, turnList: number[], comman window.location.href = 'v_chiefCenter.php'; } - return data as unknown as T; + return response as T; } catch (e) { console.error(e);