Skip to content

Commit

Permalink
✨ 完成 cookie_token 获取
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed May 17, 2023
1 parent ee6efd4 commit f1c3151
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
28 changes: 28 additions & 0 deletions http/request/getCookieToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file http request getCookieToken.js
* @description 获取cookieToken
* @author BTMuli<bt-muli@outlook.com>
* @since 1.1.0
*/

// Node
import axios from "axios";
// TGAssistant
import getHeader from "../tools/getHeader.js";


/**
* @description 根据 stoken 获取 cookieToken
* @param {string} cookie cookie
* @param {string} SToken sToken
* @returns {Promise<string>} cookieToken
*/
export async function getCookieTokenBySToken(cookie, SToken) {
const urlPre = "https://passport-api.mihoyo.com/account/auth/api/getCookieAccountInfoBySToken";
const urlCur = `stoken=${SToken}`;
const url = `${urlPre}?${urlCur}`;
const header = getHeader(cookie, "GET", urlCur);
return await axios.get(url, { headers: header }).then(res => {
return res.data["data"]["cookie_token"];
});
}
7 changes: 5 additions & 2 deletions http/tools/operSQLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sqlite3 from "sqlite3";
// TGAssistant
import HttpConstant from "../constant/index.js";
import { transCookie } from "./utils.js";

class LocalSqlite {
constructor() {
Expand Down Expand Up @@ -53,7 +54,7 @@ class LocalSqlite {
async getCookie() {
const sql = "select * from AppData where key='cookie'";
const res = await this.selectSingle(sql);
return JSON.parse(res.value);
return transCookie(JSON.parse(res.value));
}

/**
Expand All @@ -63,7 +64,9 @@ class LocalSqlite {
* @return {Promise<string>}
*/
async getCookieItem(itemKey){
const cookie = await this.getCookie();
const sql = "select * from AppData where key='cookie'";
const res = await this.selectSingle(sql);
const cookie = JSON.parse(res.value);
return cookie[itemKey];
}

Expand Down
17 changes: 17 additions & 0 deletions http/tools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,21 @@ export function getRandomString(length) {
*/
export function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

/**
* @description 转换 cookie
* @since 1.1.0
* @param {object} cookie cookie
* @returns {string} 转换后的 cookie
*/
export function transCookie(cookie) {
let res = "";
const keys = Object.keys(cookie);
for (const key of keys){
if(cookie[key]!==""){
res += `${key}=${cookie[key]};`;
}
}
return res;
}
23 changes: 23 additions & 0 deletions test/http/getCookieToken.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file test http getCookieToken.test.js
* @description 测试 CookieToken 的获取
* @author BTMuli<bt-muli@outlook.com>
* @since 1.1.0
*/

// Node
import { describe, it } from "mocha";
import assert from "node:assert";
// TGAssistant
import { getCookieTokenBySToken } from "../../http/request/getCookieToken.js";
import LocalSqlite from "../../http/tools/operSQLite.js";

describe("测试 cookieToken 获取", ()=>{
it("通过 stoken", async ()=>{
const stoken = await LocalSqlite.getCookieItem("stoken");
const cookie = JSON.stringify(await LocalSqlite.getCookie());
const res = await getCookieTokenBySToken(cookie, stoken);
const cookieToken = await LocalSqlite.getCookieItem("cookie_token");
assert.strictEqual(res, cookieToken);
});
});

0 comments on commit f1c3151

Please sign in to comment.