42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
|
|
import type { StoredPlatformState } from "../platforms";
|
||
|
|
|
||
|
|
import { connectedPlatform, disconnectedPlatform, fetchJson, unsupportedPublishResult } from "./common";
|
||
|
|
import type { AdapterContext, PlatformAdapter, PlatformPublishResult } from "./types";
|
||
|
|
|
||
|
|
type ZolUserInfoResponse = {
|
||
|
|
data?: {
|
||
|
|
userId?: string | number;
|
||
|
|
nickName?: string;
|
||
|
|
photo?: string;
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
async function detectZol(platform: StoredPlatformState): Promise<StoredPlatformState> {
|
||
|
|
try {
|
||
|
|
const response = await fetchJson<ZolUserInfoResponse>("https://open-api.zol.com.cn/api/v1/creator.user.getinfo", {
|
||
|
|
headers: {
|
||
|
|
accept: "application/json, text/plain, */*",
|
||
|
|
},
|
||
|
|
}).catch(() => null);
|
||
|
|
|
||
|
|
const user = response?.data;
|
||
|
|
const uid = user?.userId != null ? String(user.userId) : "";
|
||
|
|
if (!uid || !user?.nickName) {
|
||
|
|
return disconnectedPlatform(platform, "未检测到中关村在线登录态");
|
||
|
|
}
|
||
|
|
return connectedPlatform(platform, uid, user.nickName, user.photo ?? null);
|
||
|
|
} catch (error) {
|
||
|
|
return disconnectedPlatform(platform, error instanceof Error ? error.message : "中关村在线登录检测失败");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function publishZol(): Promise<PlatformPublishResult> {
|
||
|
|
return unsupportedPublishResult("中关村在线");
|
||
|
|
}
|
||
|
|
|
||
|
|
export const zolAdapter: PlatformAdapter = {
|
||
|
|
platformId: "zol",
|
||
|
|
detect: detectZol,
|
||
|
|
publish: (_context: AdapterContext) => publishZol(),
|
||
|
|
};
|