siyuan/app/src/util/iOSPurchase.ts

79 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {showMessage} from "../dialog/message";
import {fetchPost} from "./fetch";
import {genUUID} from "./genID";
export const processIOSPurchaseResponse = (code: number) => {
if (code === 0) {
/// #if MOBILE
document.querySelector("#modelMain").dispatchEvent(new CustomEvent("click", {
detail: document.querySelector("#modelMain #refresh")
}));
/// #else
document.querySelector('.config__tab-container[data-name="account"] #refresh').dispatchEvent(new Event("click"));
/// #endif
} else {
// -1Invalid cloud region 云端区域无效
// -2Server communication failed, need to retry 服务器通讯失败,需要重试
// -3Non-iOS device 非 iOS 设备
// -4Account not logged in 账号未登录
// -5Account status abnormal 账号状态异常
// -6Parameter error 参数错误
// -7AccountToken verification failed 校验 accountToken 失败
// -8Transaction verification failed 校验 transaction 失败
let message = "";
switch (code) {
case -1:
message = "Invalid cloud region.";
break;
case -2:
message = "Server communication failed, need to retry.";
break;
case -3:
message = "Non-iOS device.";
break;
case -4:
message = "Account not logged in.";
break;
case -5:
message = "Account status abnormal.";
break;
case -6:
message = "Parameter error.";
break;
case -7:
message = "AccountToken verification failed.";
break;
case -8:
message = "Transaction verification failed.";
break;
}
showMessage(message, 0, "error");
}
};
export const iOSPurchase = (productType: string) => {
if (window.siyuan.user) {
fetchPost("/api/setting/getCloudUser", {
token: window.siyuan.user.userToken,
}, response => {
if (window.siyuan.user.userSiYuanOneTimePayStatus !== response.data.userSiYuanOneTimePayStatus ||
window.siyuan.user.userSiYuanProExpireTime !== response.data.userSiYuanProExpireTime ||
window.siyuan.user.userSiYuanSubscriptionPlan !== response.data.userSiYuanSubscriptionPlan ||
window.siyuan.user.userSiYuanSubscriptionType !== response.data.userSiYuanSubscriptionType ||
window.siyuan.user.userSiYuanSubscriptionStatus !== response.data.userSiYuanSubscriptionStatus) {
showMessage(window.siyuan.languages["_kernel"][19]);
return;
}
window.siyuan.user = response.data;
let productID;
if (window.siyuan.config.cloudRegion === 0) {
productID = productType === "function" ? "china00" : "china02";
} else {
productID = productType === "function" ? "00" : "02";
}
window.webkit.messageHandlers.purchase.postMessage(`${productID} ${genUUID().substring(0, 19)}${window.siyuan.config.cloudRegion}00${window.siyuan.user.userId.substring(0, 1)}-${window.siyuan.user.userId.substring(1)}`);
});
} else {
showMessage(window.siyuan.languages.needLogin);
}
}