From 5a2406ab4aedccc8411fd1bd0650cf5fd92dcc84 Mon Sep 17 00:00:00 2001 From: terwer Date: Wed, 9 Aug 2023 16:03:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=E7=9F=A5=E4=B9=8E?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- package.json | 8 +- pnpm-lock.yaml | 107 +++++++++++----------- scripts/serve.py | 1 + src/adaptors/api/base/baseBlogApi.ts | 13 ++- src/adaptors/web/base/baseWebApi.ts | 66 +++++++------ src/adaptors/web/zhihu/zhihuWebAdaptor.ts | 101 ++++++++++++++++---- src/components/set/PublishSetting.vue | 2 +- src/composables/useSiyuanApi.ts | 7 +- 9 files changed, 191 insertions(+), 117 deletions(-) diff --git a/.gitignore b/.gitignore index 8976b48b..1aea3a18 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ archive.zip components.d.ts # test -coverage \ No newline at end of file +coverage +testdata \ No newline at end of file diff --git a/package.json b/package.json index a9814ca8..58908362 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,9 @@ "syncWidgetRepo": "python scripts/sync_widget_repo.py" }, "devDependencies": { - "@terwer/esbuild-config-custom": "^1.0.12", + "@terwer/esbuild-config-custom": "^1.2.0", "@terwer/eslint-config-custom": "^1.3.6", - "@types/node": "^18.17.3", + "@types/node": "^18.17.4", "@vitejs/plugin-vue": "^4.2.3", "@vitest/coverage-v8": "^0.34.1", "@vue/test-utils": "^2.4.1", @@ -43,8 +43,8 @@ "typescript": "^5.1.6", "unplugin-auto-import": "^0.16.6", "unplugin-vue-components": "^0.25.1", - "vercel": "^31.2.2", - "vite": "^4.4.9", + "vercel": "^31.2.3", + "vite": "^4.3.9", "vite-plugin-html": "^3.2.0", "vite-plugin-node-polyfills": "^0.8.2", "vitest": "^0.34.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f8d1844f..a135dde8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,14 +80,14 @@ dependencies: devDependencies: '@terwer/esbuild-config-custom': - specifier: ^1.0.12 - version: 1.0.12(@types/minimist@1.2.2)(dotenv@16.3.1)(esbuild-plugin-copy@2.1.1)(esbuild-plugin-d.ts@1.1.0)(esbuild-plugin-ifdef@1.0.1)(esbuild-plugin-inline-image@0.0.9)(esbuild-plugin-vue3@0.3.2)(esbuild-style-plugin@1.6.2)(esbuild@0.17.19)(minimist@1.2.8)(rimraf@4.4.1)(stylus@0.59.0) + specifier: ^1.2.0 + version: 1.2.0(@types/minimist@1.2.2)(dotenv@16.3.1)(esbuild-plugin-copy@2.1.1)(esbuild-plugin-d.ts@1.1.0)(esbuild-plugin-ifdef@1.0.1)(esbuild-plugin-inline-image@0.0.9)(esbuild-plugin-vue3@0.3.2)(esbuild-style-plugin@1.6.2)(esbuild@0.17.19)(minimist@1.2.8)(rimraf@4.4.1)(stylus@0.59.0) '@terwer/eslint-config-custom': specifier: ^1.3.6 version: 1.3.6(@nuxt/eslint-config@0.1.1)(@typescript-eslint/eslint-plugin@5.62.0)(astro-eslint-parser@0.13.3)(eslint-config-prettier@8.10.0)(eslint-config-turbo@1.10.12)(eslint-plugin-prettier@4.2.1)(eslint-plugin-svelte@2.32.4)(eslint-plugin-vue@9.16.1)(eslint@8.46.0)(prettier-plugin-svelte@2.10.1)(prettier@2.8.8)(typescript@5.1.6) '@types/node': - specifier: ^18.17.3 - version: 18.17.3 + specifier: ^18.17.4 + version: 18.17.4 '@vitejs/plugin-vue': specifier: ^4.2.3 version: 4.2.3(vite@4.4.9)(vue@3.3.4) @@ -128,11 +128,11 @@ devDependencies: specifier: ^0.25.1 version: 0.25.1(vue@3.3.4) vercel: - specifier: ^31.2.2 - version: 31.2.2 + specifier: ^31.2.3 + version: 31.2.3 vite: - specifier: ^4.4.9 - version: 4.4.9(@types/node@18.17.3)(stylus@0.59.0) + specifier: ^4.3.9 + version: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) vite-plugin-html: specifier: ^3.2.0 version: 3.2.0(vite@4.4.9) @@ -860,7 +860,7 @@ packages: nopt: 5.0.0 npmlog: 5.0.1 rimraf: 3.0.2 - semver: 7.5.4 + semver: 7.3.8 tar: 6.1.15 transitivePeerDependencies: - encoding @@ -1083,8 +1083,8 @@ packages: resolution: {integrity: sha512-Ccy0NlLkzr0Ex2FKvh2X+OyERHXJ88XJ1MXtsI9y9fGexlaXaVTPzBCRBwIxFkORuOb+uBqeu+RqnpgYTEZRUQ==} dev: false - /@terwer/esbuild-config-custom@1.0.12(@types/minimist@1.2.2)(dotenv@16.3.1)(esbuild-plugin-copy@2.1.1)(esbuild-plugin-d.ts@1.1.0)(esbuild-plugin-ifdef@1.0.1)(esbuild-plugin-inline-image@0.0.9)(esbuild-plugin-vue3@0.3.2)(esbuild-style-plugin@1.6.2)(esbuild@0.17.19)(minimist@1.2.8)(rimraf@4.4.1)(stylus@0.59.0): - resolution: {integrity: sha512-fllPEIgmC9bJer+VQ1xuJ+uIDKEcWIW2tdQe1BkZ71us/ZNSLl5GPMxGr9OGH8lXqkxlErw3M6BXOJzVaCQYqg==} + /@terwer/esbuild-config-custom@1.2.0(@types/minimist@1.2.2)(dotenv@16.3.1)(esbuild-plugin-copy@2.1.1)(esbuild-plugin-d.ts@1.1.0)(esbuild-plugin-ifdef@1.0.1)(esbuild-plugin-inline-image@0.0.9)(esbuild-plugin-vue3@0.3.2)(esbuild-style-plugin@1.6.2)(esbuild@0.17.19)(minimist@1.2.8)(rimraf@4.4.1)(stylus@0.59.0): + resolution: {integrity: sha512-8mKotP4DZ+lnB8Mn0jDu8Zv+Bdy1OdtjnbfodI1qO6zr+//BKWx0k6GMh99A2vz1+WNTOvuSu3QiWseWDmFXXQ==} hasBin: true peerDependencies: '@types/minimist': ^1.2.2 @@ -1237,14 +1237,14 @@ packages: /@types/node-fetch@2.6.3: resolution: {integrity: sha512-ETTL1mOEdq/sxUtgtOhKjyB2Irra4cjxksvcMUR5Zr4n+PxVhsCD9WS46oPbHL3et9Zde7CNRr+WUNlcHvsX+w==} dependencies: - '@types/node': 18.17.3 + '@types/node': 18.17.4 form-data: 3.0.1 dev: true /@types/node-fetch@2.6.4: resolution: {integrity: sha512-1ZX9fcN4Rvkvgv4E6PAY5WXUFWFcRWxZa3EW83UjycOB9ljJCedb2CupIP4RZMEwF/M3eTcCihbBRgwtGbg5Rg==} dependencies: - '@types/node': 18.17.3 + '@types/node': 18.17.4 form-data: 3.0.1 dev: false @@ -1252,8 +1252,8 @@ packages: resolution: {integrity: sha512-qelS/Ra6sacc4loe/3MSjXNL1dNQ/GjxNHVzuChwMfmk7HuycRLVQN2qNY3XahK+fZc5E2szqQSKUyAF0E+2bg==} dev: true - /@types/node@18.17.3: - resolution: {integrity: sha512-2x8HWtFk0S99zqVQABU9wTpr8wPoaDHZUcAkoTKH+nL7kPv3WUI9cRi/Kk5Mz4xdqXSqTkKP7IWNoQQYCnDsTA==} + /@types/node@18.17.4: + resolution: {integrity: sha512-ATL4WLgr7/W40+Sp1WnNTSKbgVn6Pvhc/2RHAdt8fl6NsQyp4oPCi2eKcGOvA494bwf1K/W6nGgZ9TwDqvpjdw==} /@types/sass@1.45.0: resolution: {integrity: sha512-jn7qwGFmJHwUSphV8zZneO3GmtlgLsmhs/LQyVvQbIIa+fzGMUiHI4HXJZL3FT8MJmgXWbLGiVVY7ElvHq6vDA==} @@ -1269,7 +1269,7 @@ packages: /@types/stylus@0.48.38: resolution: {integrity: sha512-B5otJekvD6XM8iTrnO6e2twoTY2tKL9VkL/57/2Lo4tv3EatbCaufdi68VVtn/h4yjO+HVvYEyrNQd0Lzj6riw==} dependencies: - '@types/node': 18.17.3 + '@types/node': 18.17.4 dev: true /@types/unist@2.0.7: @@ -1522,8 +1522,8 @@ packages: eslint-visitor-keys: 3.4.2 dev: true - /@vercel/build-utils@6.8.2: - resolution: {integrity: sha512-pyDBREAzaLBM3zoURCCKjnKIqtqPprKcwDdd9eBTGI32qOSpbwswtyqtgj2zH/Ro0LI61jCr6sK87JHILCr1sg==} + /@vercel/build-utils@6.8.3: + resolution: {integrity: sha512-C86OPuPAvG/pSr27DPKecmptkYYsgyhOKdHTLv9jI3Pv1yvru78k+JjrAyn7N+0ev75KNV0Prv4P3p76168ePw==} dev: true /@vercel/error-utils@1.0.10: @@ -1537,12 +1537,12 @@ packages: web-vitals: 0.2.4 dev: true - /@vercel/gatsby-plugin-vercel-builder@1.3.16: - resolution: {integrity: sha512-T59fdopfGUvS7VX6DLQv89foG87/Bn9Oc3afo4A8fhQBA6XPj+0h0VyDmUGErVYJRgqnKRAMuyhKWsr/rhCvsQ==} + /@vercel/gatsby-plugin-vercel-builder@1.3.17: + resolution: {integrity: sha512-FxT9JZiaak1qCHF1qNKCbcgBPXNarxVLoZbAL2DvEkeyOE9CgadihAA+zTDsqMRFb/Y5dY2KP1+iZFop+K5tuQ==} dependencies: '@sinclair/typebox': 0.25.24 - '@vercel/build-utils': 6.8.2 - '@vercel/node': 2.15.8 + '@vercel/build-utils': 6.8.3 + '@vercel/node': 2.15.9 '@vercel/routing-utils': 2.2.1 esbuild: 0.14.47 etag: 1.8.1 @@ -1561,8 +1561,8 @@ packages: resolution: {integrity: sha512-1rzFB664G6Yzp7j4ezW9hvVjqnaU2BhyUdhchbsxtRuxkMpGgPBZKhjzRQHFvlmkz37XLC658T5Nb1P91b4sBw==} dev: true - /@vercel/next@3.9.3: - resolution: {integrity: sha512-jHuw1HYzaLt5qzJm+U1ydzKMSM9ptW5vHZ3AkFqkav7qaCDrvV0SKOiNQ8xoJhBLNFuXQY6Z4l8Ag86kUYevGA==} + /@vercel/next@3.9.4: + resolution: {integrity: sha512-6qH/dNSEEN2pQW5iVi6RUfjro6v9mxdXLtiRf65gQim89CXfPR9CKcCW3AxcKSkYPX9Q7fPiaEGwTr68fPklCw==} dev: true /@vercel/nft@0.22.5: @@ -1586,15 +1586,15 @@ packages: - supports-color dev: true - /@vercel/node@2.15.8: - resolution: {integrity: sha512-G8+rC3mAouBFonsCKeQ9nINv7WL4SeHa6qx+bW/6BQAFc/eLKfQNa04t6ZYKLiXZQSw4COSC3g2afh7ejYVvVg==} + /@vercel/node@2.15.9: + resolution: {integrity: sha512-nsfTMBZOuXFxz3s7A2b+3rkt2QN016TXn7apPb+apWaO+UzMaRLE0Gubn8TXQ08N4ag6G5bBJXrWRJywL4whrQ==} dependencies: '@edge-runtime/node-utils': 2.0.3 '@edge-runtime/primitives': 2.1.2 '@edge-runtime/vm': 3.0.1 '@types/node': 14.18.33 '@types/node-fetch': 2.6.3 - '@vercel/build-utils': 6.8.2 + '@vercel/build-utils': 6.8.3 '@vercel/error-utils': 1.0.10 '@vercel/static-config': 2.0.17 async-listen: 3.0.0 @@ -1628,10 +1628,10 @@ packages: - supports-color dev: true - /@vercel/remix-builder@1.9.1: - resolution: {integrity: sha512-yOxGKn3uhp3eYxIE4AZVjssv9L+7sZcnS04d+6qPNvuN0WAuN7pi/Jx0ebo4IMgFQmvK6Ws+xwq9UDCs37jcow==} + /@vercel/remix-builder@1.10.0: + resolution: {integrity: sha512-yvCvj/e2xxkVaPukOb5aCBNCLGsEgi9VSfb79Fo8aWlm5aaCO6UJoGD28/7lF5MxAFAl+Jmbgq0EH+53v/CJ5g==} dependencies: - '@vercel/build-utils': 6.8.2 + '@vercel/build-utils': 6.8.3 '@vercel/nft': 0.22.5 '@vercel/static-config': 2.0.17 path-to-regexp: 6.2.1 @@ -1654,11 +1654,11 @@ packages: resolution: {integrity: sha512-J8I0B7wAn8piGoPhBroBfJWgMEJTMEL/2o8MCoCyWdaE7MRtpXhI10pj8IvcUvAECoGJ+SM1Pm+SvBqtbtZ5FQ==} dev: true - /@vercel/static-build@1.3.44: - resolution: {integrity: sha512-yn2/92/kvEvfKAVBLxzSm0XePaZVoYQ4hvO5NCGzZXa5iqS+hQ3kYCz1w2EyEpsGdCBQaR+SCYKqv5kmXvxIHw==} + /@vercel/static-build@1.3.45: + resolution: {integrity: sha512-htXQUuxxSguvTvsZO0gZeJiGXSUjzMu5VAUpUbQxVSdaB2tY4KKUSo/wq1vLq4jO5YydlUwqazelf0nehSJj6A==} dependencies: '@vercel/gatsby-plugin-vercel-analytics': 1.0.10 - '@vercel/gatsby-plugin-vercel-builder': 1.3.16 + '@vercel/gatsby-plugin-vercel-builder': 1.3.17 transitivePeerDependencies: - '@swc/core' - '@swc/wasm' @@ -1680,7 +1680,7 @@ packages: vite: ^4.0.0 vue: ^3.2.25 dependencies: - vite: 4.4.9(@types/node@18.17.3)(stylus@0.59.0) + vite: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) vue: 3.3.4 dev: true @@ -4713,7 +4713,7 @@ packages: resolution: {integrity: sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==} engines: {node: '>=8'} dependencies: - semver: 6.1.1 + semver: 6.3.1 dev: true /make-dir@4.0.0: @@ -5914,6 +5914,11 @@ packages: hasBin: true dev: true + /semver@6.3.1: + resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} + hasBin: true + dev: true + /semver@7.3.8: resolution: {integrity: sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==} engines: {node: '>=10'} @@ -6677,21 +6682,21 @@ packages: convert-source-map: 1.9.0 dev: true - /vercel@31.2.2: - resolution: {integrity: sha512-g9j7h/1vunFJ33lJddMV7tgZycu6vRDXIEE+9uDYO6i+G7Is9s917tpjguQmUAT9nzmX7cwAmbx34QxGAXBpWw==} + /vercel@31.2.3: + resolution: {integrity: sha512-wbdO/3DGBGRcEYYeYBPc1oAiK+TeQ5CPa0CzT1J/BaBoXor2v3TgcnMwNI70wuxq5pXu4rk54E1Ew11EHQiwqQ==} engines: {node: '>= 14'} hasBin: true dependencies: - '@vercel/build-utils': 6.8.2 + '@vercel/build-utils': 6.8.3 '@vercel/go': 2.5.1 '@vercel/hydrogen': 0.0.64 - '@vercel/next': 3.9.3 - '@vercel/node': 2.15.8 + '@vercel/next': 3.9.4 + '@vercel/node': 2.15.9 '@vercel/python': 3.1.60 '@vercel/redwood': 1.1.15 - '@vercel/remix-builder': 1.9.1 + '@vercel/remix-builder': 1.10.0 '@vercel/ruby': 1.3.76 - '@vercel/static-build': 1.3.44 + '@vercel/static-build': 1.3.45 transitivePeerDependencies: - '@swc/core' - '@swc/wasm' @@ -6715,7 +6720,7 @@ packages: vfile-message: 2.0.4 dev: false - /vite-node@0.34.1(@types/node@18.17.3)(stylus@0.59.0): + /vite-node@0.34.1(@types/node@18.17.4)(stylus@0.59.0): resolution: {integrity: sha512-odAZAL9xFMuAg8aWd7nSPT+hU8u2r9gU3LRm9QKjxBEF2rRdWpMuqkrkjvyVQEdNFiBctqr2Gg4uJYizm5Le6w==} engines: {node: '>=v14.18.0'} hasBin: true @@ -6725,7 +6730,7 @@ packages: mlly: 1.4.0 pathe: 1.1.1 picocolors: 1.0.0 - vite: 4.4.9(@types/node@18.17.3)(stylus@0.59.0) + vite: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) transitivePeerDependencies: - '@types/node' - less @@ -6754,7 +6759,7 @@ packages: html-minifier-terser: 6.1.0 node-html-parser: 5.4.2 pathe: 0.2.0 - vite: 4.4.9(@types/node@18.17.3)(stylus@0.59.0) + vite: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) dev: true /vite-plugin-node-polyfills@0.8.2(vite@4.4.9): @@ -6764,12 +6769,12 @@ packages: dependencies: '@rollup/plugin-inject': 5.0.3 node-stdlib-browser: 1.2.0 - vite: 4.4.9(@types/node@18.17.3)(stylus@0.59.0) + vite: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) transitivePeerDependencies: - rollup dev: true - /vite@4.4.9(@types/node@18.17.3)(stylus@0.59.0): + /vite@4.4.9(@types/node@18.17.4)(stylus@0.59.0): resolution: {integrity: sha512-2mbUn2LlUmNASWwSCNSJ/EG2HuSRTnVNaydp6vMCm5VIqJsjMfbIWtbH2kDuwUVW5mMUKKZvGPX/rqeqVvv1XA==} engines: {node: ^14.18.0 || >=16.0.0} hasBin: true @@ -6797,7 +6802,7 @@ packages: terser: optional: true dependencies: - '@types/node': 18.17.3 + '@types/node': 18.17.4 esbuild: 0.18.20 postcss: 8.4.27 rollup: 3.27.2 @@ -6839,7 +6844,7 @@ packages: dependencies: '@types/chai': 4.3.5 '@types/chai-subset': 1.3.3 - '@types/node': 18.17.3 + '@types/node': 18.17.4 '@vitest/expect': 0.34.1 '@vitest/runner': 0.34.1 '@vitest/snapshot': 0.34.1 @@ -6859,8 +6864,8 @@ packages: strip-literal: 1.3.0 tinybench: 2.5.0 tinypool: 0.7.0 - vite: 4.4.9(@types/node@18.17.3)(stylus@0.59.0) - vite-node: 0.34.1(@types/node@18.17.3)(stylus@0.59.0) + vite: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) + vite-node: 0.34.1(@types/node@18.17.4)(stylus@0.59.0) why-is-node-running: 2.2.2 transitivePeerDependencies: - less diff --git a/scripts/serve.py b/scripts/serve.py index 463bcad6..fabf0f9c 100644 --- a/scripts/serve.py +++ b/scripts/serve.py @@ -41,6 +41,7 @@ # 设置环境变量 os.environ['IS_SERVE'] = 'true' + os.environ['VITE_DEFAULT_TYPE'] = 'siyuan' os.environ['VITE_SIYUAN_API_URL'] = URL os.environ['VITE_SIYUAN_AUTH_TOKEN'] = TOKEN os.environ['VITE_SIYUAN_COOKIE'] = COOKIE diff --git a/src/adaptors/api/base/baseBlogApi.ts b/src/adaptors/api/base/baseBlogApi.ts index a37d61f1..f36c0cf9 100644 --- a/src/adaptors/api/base/baseBlogApi.ts +++ b/src/adaptors/api/base/baseBlogApi.ts @@ -71,16 +71,15 @@ export class BaseBlogApi extends BlogApi { * 网页授权通用的请求代理 * * @param url - url - * @param headers - headers - * @param params - 参数 - * @param method - method - @param contentType - - * @param contentType - + * @param headers - headers,默认是[] + * @param params - 参数,默认是 {} + * @param method - 方法,默认是GET + * @param contentType - 类型,默认是 application/json */ protected async proxyFetch( url: string, - headers: any[], - params: any, + headers: any[] = [], + params: any = {}, method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" = "GET", contentType: string = "application/json" ): Promise { diff --git a/src/adaptors/web/base/baseWebApi.ts b/src/adaptors/web/base/baseWebApi.ts index 74fa9efb..40481506 100644 --- a/src/adaptors/web/base/baseWebApi.ts +++ b/src/adaptors/web/base/baseWebApi.ts @@ -28,7 +28,6 @@ import { CommonFetchClient } from "zhi-fetch-middleware" import { AppInstance } from "~/src/appInstance.ts" import { createAppLogger, ILogger } from "~/src/utils/appLogger.ts" import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts" -import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts" import { JsonUtil } from "zhi-common" import { isDev } from "~/src/utils/constants.ts" @@ -44,8 +43,7 @@ class BaseWebApi extends WebApi { protected cfg: WebConfig private readonly kernelApi: SiyuanKernelApi private readonly commonFetchClient: CommonFetchClient - private isInSiyuanWidget: boolean - private isInChromeExtension: boolean + private readonly useSiyuanProxy: boolean /** * 初始化网页授权 API 适配器 @@ -58,12 +56,11 @@ class BaseWebApi extends WebApi { this.cfg = cfg this.logger = createAppLogger("base-web-api") - const { kernelApi } = useSiyuanApi() - const { isInSiyuanWidget, isInChromeExtension } = useSiyuanDevice() - this.kernelApi = kernelApi this.commonFetchClient = new CommonFetchClient(appInstance, cfg.apiUrl, cfg.middlewareUrl, isDev) - this.isInSiyuanWidget = isInSiyuanWidget() - this.isInChromeExtension = isInChromeExtension() + + const { kernelApi, isUseSiyuanProxy } = useSiyuanApi() + this.kernelApi = kernelApi + this.useSiyuanProxy = isUseSiyuanProxy() } // web 适配器专有 @@ -82,7 +79,7 @@ class BaseWebApi extends WebApi { if (res.status !== "success") { throw new Error("网页授权发布文章异常") } - return res.id + return res.post_id } // ================ @@ -92,48 +89,49 @@ class BaseWebApi extends WebApi { * 网页授权通用的请求代理 * * @param url - url - * @param params - 参数 + * @param headers - 请求头,默认取 password 作为 cookie + * @param params - 参数,默认是 {} + * @param method - 方法,默认是GET + * @param contentType - 类型,默认是 application/json */ - protected async proxyFetch(url: string, params: any = {}): Promise { - if (this.isInSiyuanWidget) { + protected async proxyFetch( + url: string, + headers: any[] = [], + params: any = {}, + method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" = "GET", + contentType: string = "application/json" + ): Promise { + if (this.useSiyuanProxy) { this.logger.info("using siyuan forwardProxy") const fetchResult = await this.kernelApi.forwardProxy( url, - [ - { - Cookie: this.cfg.password, - }, - ], + headers.length > 0 + ? headers + : [ + { + Cookie: this.cfg.password, + }, + ], params, - "GET", - "application/json", + method, + contentType, 7000 ) this.logger.debug("proxyFetch result=>", fetchResult) const resText = fetchResult?.body + // 后续调试可打开这个日志 + // this.logger.debug("proxyFetch resText=>", resText) const res = JsonUtil.safeParse(resText, {} as any) return res - } else if (this.isInChromeExtension) { - this.logger.info("using chrome background") - const fetchOptions = { - method: "GET", - headers: { - "Content-Type": "application/json", - Cookie: this.cfg.password, - }, - } - this.logger.info("commonFetchClient from proxyFetch url =>", url) - this.logger.info("commonFetchClient from proxyFetch fetchOptions =>", fetchOptions) - const res = await this.commonFetchClient.fetchCall(url, fetchOptions) - this.logger.debug("commonFetchClient res from proxyFetch =>", res) - return res } else { this.logger.info("using middleware proxy") + const header = headers.length > 0 ? headers[0] : {} const fetchOptions = { - method: "GET", + method: method, headers: { "Content-Type": "application/json", Cookie: this.cfg.password, + ...header, }, } this.logger.info("commonFetchClient from proxyFetch url =>", url) diff --git a/src/adaptors/web/zhihu/zhihuWebAdaptor.ts b/src/adaptors/web/zhihu/zhihuWebAdaptor.ts index 9479979b..93d3ea87 100644 --- a/src/adaptors/web/zhihu/zhihuWebAdaptor.ts +++ b/src/adaptors/web/zhihu/zhihuWebAdaptor.ts @@ -24,7 +24,9 @@ */ import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts" -import { Post } from "zhi-blog-api" +import { Post, UserBlog } from "zhi-blog-api" +import { types } from "sass" +import Error = types.Error /** * 知乎网页授权适配器 @@ -59,7 +61,7 @@ class ZhihuWebAdaptor extends BaseWebApi { uid: res.uid, title: res.name, avatar: res.avatar_url, - supportTypes: ["html"], + supportTypes: ["markdown"], type: "zhihu", displayName: "知乎", home: "https://www.zhihu.com/settings/account", @@ -68,29 +70,96 @@ class ZhihuWebAdaptor extends BaseWebApi { } public async addPost(post: Post) { - let res + const params = JSON.stringify({ + title: post.title, + content: post.description, + }) + const res = await this.proxyFetch("https://zhuanlan.zhihu.com/api/articles/drafts", [], params, "POST") + this.logger.debug("save zhihu draft res=>", res) + + if (!res.id) { + throw new Error("知乎文章发布失败") + } + + // 目前是存草稿,现在需要把它设置为发布 + const pubParams = JSON.stringify({ + column: null, + commentPermission: "anyone", + disclaimer_type: "none", + disclaimer_status: "close", + table_of_contents_enabled: false, + commercial_report_info: { commercial_types: [] }, + commercial_zhitask_bind_info: null, + }) + const pubRes = await this.proxyFetch( + `https://zhuanlan.zhihu.com/api/articles/${res.id}/publish`, + [], + pubParams, + "PUT" + ) + this.logger.debug("publish zhihu article pubRes=>", pubRes) - throw new Error("开发中") - // var res = await $.ajax({ - // url: 'https://zhuanlan.zhihu.com/api/articles/drafts', - // type: 'POST', - // dataType: 'JSON', - // contentType: 'application/json', - // data: JSON.stringify({ - // title: post.post_title, - // // content: post.post_content - // }), - // }) - // console.log(res) return { status: "success", - post_id: res.id, + post_id: res.id.toString(), } } public async getPreviewUrl(postid: string): Promise { return `https://zhuanlan.zhihu.com/p/${postid}` } + + public async deletePost(postid: string): Promise { + let flag = false + try { + const res = await this.proxyFetch(`https://www.zhihu.com/api/v4/articles/${postid}`, [], {}, "DELETE") + this.logger.debug("delete zhihu article res=>", res) + if (res.success) { + flag = true + } else { + throw new Error(res.error.message) + } + } catch (e) { + this.logger.error("知乎文章删除失败", e) + throw e + } + + return flag + } + + public async editPost(postid: string, post: Post, publish?: boolean): Promise { + // 先更新草稿 + const params = JSON.stringify({ + title: post.title, + content: post.description, + table_of_contents: false, + delta_time: 10, + }) + + try { + await this.proxyFetch(`https://zhuanlan.zhihu.com/api/articles/${postid}/draft`, [], params, "PATCH") + this.logger.debug("updated zhihu draft") + } catch (e) { + throw new Error("知乎文章更新失败") + } + + // 目前是存草稿,现在需要把它设置为发布 + const pubParams = JSON.stringify({ + disclaimer_type: "none", + disclaimer_status: "close", + table_of_contents_enabled: false, + commercial_report_info: { commercial_types: [] }, + commercial_zhitask_bind_info: null, + }) + const pubRes = await this.proxyFetch( + `https://zhuanlan.zhihu.com/api/articles/${postid}/publish`, + [], + pubParams, + "PUT" + ) + this.logger.debug("edit zhihu pubRes=>", pubRes) + return true + } } export { ZhihuWebAdaptor } diff --git a/src/components/set/PublishSetting.vue b/src/components/set/PublishSetting.vue index 08d778bf..a7917ad1 100644 --- a/src/components/set/PublishSetting.vue +++ b/src/components/set/PublishSetting.vue @@ -35,7 +35,7 @@ import { AuthMode, deletePlatformByKey, DynamicConfig, - DynamicJsonCfg, + DynamicJsonCfg, getDynPostidKey, PlatformType, replacePlatformByKey, setDynamicJsonCfg, diff --git a/src/composables/useSiyuanApi.ts b/src/composables/useSiyuanApi.ts index 2ed92ebe..2c309a34 100644 --- a/src/composables/useSiyuanApi.ts +++ b/src/composables/useSiyuanApi.ts @@ -34,7 +34,7 @@ import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts" export const useSiyuanApi = () => { const logger = createAppLogger("use-siyuan-api") - const siyuanApiUrl = Utils.emptyOrDefault(process.env.VITE_SIYUAN_API_URL, "") + const siyuanApiUrl = Utils.emptyOrDefault(process.env.VITE_SIYUAN_API_URL, "http:/127.0.0.1:6806") const siyuanAuthToken = Utils.emptyOrDefault(process.env.VITE_SIYUAN_AUTH_TOKEN, "") const siyuanConfig = new SiyuanConfig(siyuanApiUrl, siyuanAuthToken) siyuanConfig.cookie = Utils.emptyOrDefault(process.env.VITE_SIYUAN_COOKIE, "") @@ -50,8 +50,9 @@ export const useSiyuanApi = () => { // 插件SPA(Docker浏览器客户端) - VITE_DEFAULT_TYPE: siyuan // 插件SPA(本地客户端浏览器) - VITE_DEFAULT_TYPE: siyuan // const storeViaSiyuanApi = process.env.VITE_DEFAULT_TYPE === "siyuan" - const storeViaSiyuanApi = process.env.VITE_DEFAULT_TYPE === "siyuan" - logger.info("defaultType=>", process.env.VITE_DEFAULT_TYPE) + const defaultType = process.env.VITE_DEFAULT_TYPE ?? "siyuan" + const storeViaSiyuanApi = defaultType === "siyuan" + logger.info("defaultType=>", defaultType) logger.info("storeViaSiyuanApi=>", String(storeViaSiyuanApi)) return storeViaSiyuanApi }