From 55f1ce3d557603f411a8badffd0dfb57cf50308f Mon Sep 17 00:00:00 2001 From: HolographicHat Date: Thu, 7 Apr 2022 21:04:49 +0800 Subject: [PATCH] auto export to cocogoat --- export.js | 29 ++++++++++++++++++----------- native.d.ts | 1 + native/src/main.cc | 8 ++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/export.js b/export.js index d4dc200..f7bab4e 100644 --- a/export.js +++ b/export.js @@ -1,7 +1,8 @@ const fs = require("fs") +const axios = require("axios") const readline = require("readline") const { loadCache, log } = require("./utils") -const { copyToClipboard } = require("./native") +const { openUrl } = require("./native") const exportToSeelie = proto => { const out = { achievements: {} } @@ -34,9 +35,7 @@ const exportToPaimon = async proto => { const exportToCocogoat = async proto => { const out = { - value: { - achievements: [] - } + achievements: [] } const achTable = new Map() const preStageAchievementIdList = [] @@ -53,19 +52,27 @@ const exportToCocogoat = async proto => { return `${d.getFullYear()}/${p(d.getMonth()+1)}/${p(d.getDate())}` } proto.list.filter(a => a.status === 3 || a.status === 2).forEach(({current, finishTimestamp, id, require}) => { - out.value.achievements.push({ + out.achievements.push({ id: id, status: current === undefined || current === 0 || preStageAchievementIdList.includes(id) ? `${require}/${require}` : `${current}/${require}`, categoryId: achTable.get(id), date: getDate(finishTimestamp) }) }) - const ts = Date.now() - const json = JSON.stringify(out,null,2) - copyToClipboard(json) - const fp = `./export-${ts}-cocogoat.json` - fs.writeFileSync(fp, json) - log(`导出内容已复制到剪贴板`) + const response = await axios.post(`https://77.cocogoat.work/v1/memo?source=${encodeURI("全部成就")}`, out).catch(_ => { + console.log("网络错误,请检查网络后重试 (26-1)") + process.exit(261) + }) + if (response.status !== 201) { + console.log(`API StatusCode 错误,请联系开发者以获取帮助 (26-2-${response.status})`) + process.exit(262) + } + const retcode = openUrl(`https://cocogoat.work/achievement?memo=${response.data.key}`) + if (retcode > 32) { + log("在浏览器内进行下一步操作") + } else { + log(`导出失败,请联系开发者以获取帮助 (26-3-${retcode})`) + } } const exportToCsv = async proto => { diff --git a/native.d.ts b/native.d.ts index c274af6..9f1722f 100644 --- a/native.d.ts +++ b/native.d.ts @@ -5,4 +5,5 @@ export function copyToClipboard(value: string): any export function enablePrivilege(): any export function getDeviceInfo(): any export function getDeviceID(): string +export function openUrl(url: string): number export function pause(): any diff --git a/native/src/main.cc b/native/src/main.cc index 204dd9a..dccb2ba 100644 --- a/native/src/main.cc +++ b/native/src/main.cc @@ -169,8 +169,16 @@ namespace native { return info.Env().Undefined(); } + Value openUrl(const CallbackInfo &info) { + Env env = info.Env(); + wstring url = StringToWString(info[0].As().Utf8Value()); + HINSTANCE retcode = ShellExecute(GetConsoleWindow(), L"open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + return Napi::Number::New(env, (INT_PTR)retcode); // NOLINT(cppcoreguidelines-narrowing-conversions) + } + Object init(Env env, Object exports) { exports.Set("pause", Function::New(env, pause)); + exports.Set("openUrl", Function::New(env, openUrl)); exports.Set("getDeviceID", Function::New(env, getDeviceID)); exports.Set("getDeviceInfo", Function::New(env, getDeviceInfo)); exports.Set("whoUseThePort", Function::New(env, whoUseThePort));