From 5e0ce6ce14b08ab640d5d9f27704d76b4e749aee Mon Sep 17 00:00:00 2001 From: JianMiau Date: Wed, 6 Mar 2024 17:27:32 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E6=96=B0=E5=A2=9ELine=E7=99=BB?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +- src/api/account/ApiLogin.ts | 18 ++++++++---- src/api/room/ApiJoin.ts | 2 ++ src/component/Room/Room.ts | 2 +- src/component/User/User.ts | 9 ++++-- src/utils/index.ts | 28 ++++++++++++++++++ yarn.lock | 57 +++++++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 31650a0..42bf129 100644 --- a/package.json +++ b/package.json @@ -31,9 +31,10 @@ "typescript": "^4.9.5" }, "dependencies": { + "axios": "^1.6.7", "dayjs": "^1.11.9", "dotenv": "^16.3.1", "fs": "^0.0.1-security", "tsrpc": "^3.4.12" } -} \ No newline at end of file +} diff --git a/src/api/account/ApiLogin.ts b/src/api/account/ApiLogin.ts index 05a0676..62b750a 100644 --- a/src/api/account/ApiLogin.ts +++ b/src/api/account/ApiLogin.ts @@ -1,19 +1,25 @@ import { ApiCall, BaseConnection } from "tsrpc"; import { ReqLogin, ResLogin } from "../../shared/protocols/account/PtlLogin"; +import { ILiffData } from "../../shared/protocols/define/interface"; import { ServiceType } from "../../shared/protocols/serviceProto"; +import { getLiffProfile } from "../../utils"; export default async function (call: ApiCall) { // Error - if (!call.req.name) { + if (!call.req.token) { call.error("Name is empty"); return; } // Success - const { name } = call.req; + const { token } = call.req; const conn: BaseConnection = call.conn; - console.log(`name: ${name} is Login`); - conn.UserId = conn.id; - conn.NickName = name; - call.succ(0); + const profile: ILiffData = await getLiffProfile(token); + console.log(`name: ${profile.displayName} is Login`); + conn.userId = profile.userId ?? ""; + conn.displayName = profile.displayName ?? ""; + conn.statusMessage = profile.statusMessage ?? ""; + conn.pictureUrl = profile.pictureUrl ?? ""; + const data: ILiffData = { ...conn }; + call.succ(data); } \ No newline at end of file diff --git a/src/api/room/ApiJoin.ts b/src/api/room/ApiJoin.ts index e14dd5c..a09f147 100644 --- a/src/api/room/ApiJoin.ts +++ b/src/api/room/ApiJoin.ts @@ -2,6 +2,7 @@ import { ApiCall, BaseConnection } from "tsrpc"; import Room from "../../component/Room/Room"; import { ReqJoin, ResJoin } from "../../shared/protocols/room/PtlJoin"; import { ServiceType } from "../../shared/protocols/serviceProto"; +import { sleep } from "../../utils"; export default async function (call: ApiCall) { const { roomId } = call.req; @@ -17,6 +18,7 @@ export default async function (call: ApiCall) { call.succ([room.ConnCount(), type]); if (room.ConnCount() >= 2) { + await sleep(100); room.GotoGame(); } } else { diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index dae4b7b..245b9ca 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -72,7 +72,7 @@ export default class Room { /** GotoGame */ public GotoGame(): void { - let data: MsgGoToGame = this.type; + let data: MsgGoToGame = [this.type, this.conns as any]; server.broadcastMsg("room/GoToGame", data, []>this.conns); } diff --git a/src/component/User/User.ts b/src/component/User/User.ts index f8b5afa..04a4508 100644 --- a/src/component/User/User.ts +++ b/src/component/User/User.ts @@ -1,15 +1,18 @@ +import { ILiffData } from "../../shared/protocols/define/interface"; import Room from "../Room/Room"; /** * User */ -export default class User { +export default class User implements ILiffData { //#region User - public UserId: string = undefined; - public NickName: string = undefined; + public userId: string = undefined; + public displayName: string = undefined; + public statusMessage: string = undefined; + public pictureUrl: string = undefined; //#endregion diff --git a/src/utils/index.ts b/src/utils/index.ts index 343e0b6..3d77050 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,6 @@ +import axios from "axios"; +import { ILiffData } from "../shared/protocols/define/interface"; + export function sleep(ms: any): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } @@ -8,4 +11,29 @@ export function getArray(count: number): number[] { array.push(i); } return array; +} + +export async function getLiffProfile(token: string): Promise { + let config = { + method: 'get', + maxBodyLength: Infinity, + url: 'https://api.line.me/v2/profile', + headers: { + 'Authorization': 'Bearer ' + token + } + }; + + let resp: ILiffData; + axios.request(config) + .then((response) => { + resp = response.data; + }) + .catch((error) => { + resp = null; + }); + + while (resp === undefined) { + await sleep(100); + } + return resp; } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index a66f897..a95017c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -323,6 +323,20 @@ array-union@^2.1.0: resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw== +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + +axios@^1.6.7: + version "1.6.7" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.7.tgz#7b48c2e27c96f9c68a2f8f31e2ab19f59b06b0a7" + integrity sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA== + dependencies: + follow-redirects "^1.15.4" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz" @@ -456,6 +470,13 @@ color-name@~1.1.4: resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + commander@^2.19.0: version "2.20.3" resolved "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz" @@ -521,6 +542,11 @@ defaults@^1.0.3: dependencies: clone "^1.0.2" +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + diff@5.0.0: version "5.0.0" resolved "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz" @@ -781,6 +807,20 @@ flatted@^3.2.7: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== +follow-redirects@^1.15.4: + version "1.15.5" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.5.tgz#54d4d6d062c0fa7d9d17feb008461550e3ba8020" + integrity sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw== + +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + fs-extra@^10.1.0: version "10.1.0" resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz" @@ -1193,6 +1233,18 @@ micromatch@^4.0.4: braces "^3.0.2" picomatch "^2.3.1" +mime-db@1.52.0: + version "1.52.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" + integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== + +mime-types@^2.1.12: + version "2.1.35" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" + integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== + dependencies: + mime-db "1.52.0" + mimic-fn@^2.1.0: version "2.1.0" resolved "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz" @@ -1413,6 +1465,11 @@ prelude-ls@^1.2.1: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz"