kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
71 lines (70 loc) • 3.18 kB
JavaScript
import { getCodemaoIFrameElement, getCodemaoWindow, isCodemaoWindow } from "../codemao-environment";
import { CodemaoUserInfo } from "./codemao-user-info";
/**
* 编程猫用户。
*/
export class CodemaoUser {
/**
* 设置身份,仅在 Node 中可用。
*/
static async setAuthorization(authorization) {
const path = await import("path");
const { mkdir, writeFile } = (await import("fs")).promises;
const appDirs = (await import("appdirsjs")).default;
const configPath = appDirs({ appName: "SLIGHTNING/Codemao-Community" }).config;
await mkdir(configPath, { recursive: true });
await writeFile(path.resolve(configPath, "authorization.txt"), authorization);
}
/**
* 请求用户登录编程猫账号,仅在浏览器中可用。
*/
static async userLogInInBrowser() {
if (isCodemaoWindow) {
location.replace("https://shequ.codemao.cn/mobile/login");
}
return new Promise((resolve, reject) => {
(async () => {
const codemaoWindow = await getCodemaoWindow();
const codemaoIFrameElement = getCodemaoIFrameElement();
if (codemaoIFrameElement != null) {
codemaoIFrameElement.style.width = "100%";
codemaoIFrameElement.style.height = "100%";
codemaoIFrameElement.style.display = "block";
codemaoIFrameElement.style.position = "fixed";
codemaoIFrameElement.style.left = "0px";
codemaoIFrameElement.style.top = "0px";
codemaoIFrameElement.style.right = "0px";
codemaoIFrameElement.style.bottom = "0px";
codemaoIFrameElement.style.border = "none";
codemaoIFrameElement.style.backgroundColor = "white";
codemaoIFrameElement.style.cursor = "pointer";
codemaoIFrameElement.style.zIndex = "10000";
}
codemaoWindow.postMessage({
type: "CODEMAO_ENVIRONMENT_LOGIN"
}, "https://coco.codemao.cn");
function listener(event) {
const { data } = event;
if (data == null || typeof data != "object") {
return;
}
if (/^https?:\/\/coco\.codemao\.cn$/.test(event.origin) &&
data.type == "COCO_CODEMAO_API_ENVIRONMENT_SERVER_INIT") {
window.removeEventListener("message", listener);
if (codemaoIFrameElement != null) {
codemaoIFrameElement.style.display = "none";
}
resolve(true);
}
}
window.addEventListener("message", listener);
})().catch(reject);
});
}
/**
* @param info 已知用户信息,如果什么信息都不提供,则表示表示当前登录的用户。
*/
constructor(info = {}) {
this.info = new CodemaoUserInfo(info);
}
}