Skip to content
This repository has been archived by the owner on Sep 24, 2023. It is now read-only.

Commit

Permalink
fix: add controller flag for whether card recharging is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
UNIDY2002 committed Aug 11, 2023
1 parent 2eec71c commit 0192758
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/constants/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export const NETWORK_USER_INFO =

export const THOS_SELECT_ONE_URL = "https://webvpn.tsinghua.edu.cn/https/77726476706e69737468656265737421e4ff4e8f69247b59700f81b9991b2631ca359dd4/fp/fp/Uniformcommon/selectOnePresetData";

export const CARD_I_VERSION_URL = "https://webvpn.tsinghua.edu.cn/http/77726476706e69737468656265737421f1e751d2242326446d0187ab9040227b239c069338e2/api/CardIVersion";
export const CARD_USER_BY_TOKEN_URL = "https://card.tsinghua.edu.cn/login/getUserInfoFromToken";
export const CARD_INFO_BY_USER_URL = "https://card.tsinghua.edu.cn/business/getCardUserinfo";
export const CARD_PHOTO_URL = "https://card.tsinghua.edu.cn/myaccount/showDbImage?idserial=";
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import {uFetch} from "./utils/network";
import {getNetworkBalance, getNetworkDetail, getOnlineDevices} from "./lib/network";
import {getScoreByCourseId} from "./lib/thos";
import {
canRechargeCampusCard,
cardCancelLoss, cardChangeTransactionPassword,
cardGetInfo,
cardGetPhotoUrl,
Expand Down Expand Up @@ -948,6 +949,9 @@ export class InfoHelper {
* @return Uri to request to complete the payment process, undefined for Bank
*/
public rechargeCampusCard = async (amount: number, transactionPassword: string, type: CardRechargeType) => {
if (!await canRechargeCampusCard(this)) {
throw new Error("暂不支持校园卡充值,请升级应用程序。");
}
if (type === CardRechargeType.Bank) {
return cardRechargeFromBank(this, transactionPassword, amount);
}
Expand Down
21 changes: 20 additions & 1 deletion src/lib/card.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {AES, enc, mode, pad} from "crypto-js";
import {roam} from "./core";
import {roam, roamingWrapperWithMocks} from "./core";
import {InfoHelper} from "../index";
import {uFetch} from "../utils/network";
import {
CARD_CANCEL_LOSS_URL,
CARD_CHANGE_PWD_URL,
CARD_I_VERSION_URL,
CARD_INFO_BY_USER_URL,
CARD_MOD_MAX_CONSUME_URL,
CARD_PHOTO_URL, CARD_RECHARGE_FROM_ALIPAY_URL,
Expand All @@ -20,6 +21,8 @@ import {CardTransaction, CardTransactionType} from "../models/card/transaction";
import {MOCK_CARD_INFO} from "../mocks/card";
import {LoginError} from "../utils/error";

const CARD_API_VERSION = 1;

const accountBaseInfo = {
user: "",
cardId: "",
Expand Down Expand Up @@ -190,6 +193,22 @@ export const cardCancelLoss = async (helper: InfoHelper, transactionPassword: st
});
};

export const canRechargeCampusCard = async (helper: InfoHelper) =>
roamingWrapperWithMocks(
helper,
undefined,
"",
async () => {
try {
const {version} = await uFetch(CARD_I_VERSION_URL).then(JSON.parse);
return version <= CARD_API_VERSION;
} catch {
throw new Error("Failed to query card API version.");
}
},
false,
);

export const cardRechargeFromBank = async (helper: InfoHelper, transactionPassword: string, amount: number) => {
if (helper.mocked()) {
return;
Expand Down

0 comments on commit 0192758

Please sign in to comment.