From e07741663688c3cbcff22818787385886da1b91b Mon Sep 17 00:00:00 2001 From: Anzhi Date: Sat, 4 Jan 2025 17:43:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20graphql=E8=AF=B7=E6=B1=82=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 5 ++++- pnpm-lock.yaml | 37 +++++++++++++++++++++++++++++++++++++ src/graphql/graphql-user.ts | 23 +++++++++++++++++++++++ src/graphql/index.ts | 10 ++++++++++ src/graphql/setup.ts | 35 +++++++++++++++++++++++++++++++++++ src/main.ts | 2 ++ src/manifest.json | 4 ++-- src/pages/index/index.vue | 10 +++++++++- src/utils/index.ts | 6 +++--- 9 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 src/graphql/graphql-user.ts create mode 100644 src/graphql/index.ts create mode 100644 src/graphql/setup.ts diff --git a/package.json b/package.json index d48f1d1..3160e98 100644 --- a/package.json +++ b/package.json @@ -102,9 +102,12 @@ "@dcloudio/uni-mp-xhs": "3.0.0-4020920240930001", "@dcloudio/uni-quickapp-webview": "3.0.0-4020920240930001", "dayjs": "1.11.10", + "graphql": "^16.10.0", + "graphql-tag": "^2.12.6", "pinia": "2.0.36", "pinia-plugin-persistedstate": "3.2.1", "qs": "6.5.3", + "villus": "^3.5.1", "vue": "3.4.21", "wot-design-uni": "^1.4.0", "z-paging": "^2.8.4" @@ -166,4 +169,4 @@ "vite-plugin-restart": "^0.4.2", "vue-tsc": "^1.8.27" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9699bff..8082aac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -59,6 +59,12 @@ importers: dayjs: specifier: 1.11.10 version: 1.11.10 + graphql: + specifier: ^16.10.0 + version: 16.10.0 + graphql-tag: + specifier: ^2.12.6 + version: 2.12.6(graphql@16.10.0) pinia: specifier: 2.0.36 version: 2.0.36(typescript@5.7.2)(vue@3.4.21(typescript@5.7.2)) @@ -68,6 +74,9 @@ importers: qs: specifier: 6.5.3 version: 6.5.3 + villus: + specifier: ^3.5.1 + version: 3.5.1(graphql@16.10.0)(vue@3.4.21(typescript@5.7.2)) vue: specifier: 3.4.21 version: 3.4.21(typescript@5.7.2) @@ -3284,6 +3293,16 @@ packages: graphemer@1.4.0: resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==} + graphql-tag@2.12.6: + resolution: {integrity: sha512-FdSNcu2QQcWnM2VNvSCCDCVS5PpPqpzgFT8+GXzqJuoDd0CBncxCY278u4mhRO7tMgo2JjgJA5aZ+nWSQ/Z+xg==} + engines: {node: '>=10'} + peerDependencies: + graphql: ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 + + graphql@16.10.0: + resolution: {integrity: sha512-AjqGKbDGUFRKIRCP9tCKiIGHyriz2oHEbPIbEtcSLSs4YjReZOIPQQWek4+6hjw62H9QShXHyaGivGiYVLeYFQ==} + engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0} + gzip-size@6.0.0: resolution: {integrity: sha512-ax7ZYomf6jqPTQ4+XCpUGyXKHk5WweS+e05MBO4/y3WJ5RkmPXNKvX+bx1behVILVwr6JSQvZAku021CHPXG3Q==} engines: {node: '>=10'} @@ -5323,6 +5342,12 @@ packages: resolution: {integrity: sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==} engines: {node: '>= 0.8'} + villus@3.5.1: + resolution: {integrity: sha512-vE9B9IImsqkVU/reSiPgRO8p4aC5Z/rb2UjLjRJBP0zBeyHtid84ewH0/NAw02OIFyFCJh+i/1C5oXuLfggPQQ==} + peerDependencies: + graphql: ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 + vue: ^3.0.0 || ^2.7.0 + vite-plugin-restart@0.4.2: resolution: {integrity: sha512-9aWN2ScJ8hbT7aC8SDeZnsbWapnslz1vhNq6Vgf2GU9WdN4NExlrWhtnu7pmtOUG3Guj8y6lPcUZ+ls7SVP33w==} peerDependencies: @@ -9799,6 +9824,13 @@ snapshots: graphemer@1.4.0: {} + graphql-tag@2.12.6(graphql@16.10.0): + dependencies: + graphql: 16.10.0 + tslib: 2.8.1 + + graphql@16.10.0: {} + gzip-size@6.0.0: dependencies: duplexer: 0.1.2 @@ -12097,6 +12129,11 @@ snapshots: vary@1.1.2: {} + villus@3.5.1(graphql@16.10.0)(vue@3.4.21(typescript@5.7.2)): + dependencies: + graphql: 16.10.0 + vue: 3.4.21(typescript@5.7.2) + vite-plugin-restart@0.4.2(vite@5.2.8(@types/node@20.17.9)(sass@1.77.8)(terser@5.36.0)): dependencies: micromatch: 4.0.8 diff --git a/src/graphql/graphql-user.ts b/src/graphql/graphql-user.ts new file mode 100644 index 0000000..2d662ea --- /dev/null +++ b/src/graphql/graphql-user.ts @@ -0,0 +1,23 @@ +// graphql-user.ts + +import gql from 'graphql-tag' + +export const refreshToken = gql` + mutation refreshToken($token: JWT!) { + refreshToken(token: $token) { + accessToken + refreshToken + } + } +` + +export const Login = gql` + query Login($input: JsCodeReq!) { + Login(input: $input) { + code + jwt + } + } +` + +// 更多... diff --git a/src/graphql/index.ts b/src/graphql/index.ts new file mode 100644 index 0000000..0473572 --- /dev/null +++ b/src/graphql/index.ts @@ -0,0 +1,10 @@ +import * as user from './graphql-user' +// ... + +/* how to use +import GQL from "@/graphql" +const curGQL = GQL.home.listAsOwner +**/ +export default { + user, +} diff --git a/src/graphql/setup.ts b/src/graphql/setup.ts new file mode 100644 index 0000000..7169afd --- /dev/null +++ b/src/graphql/setup.ts @@ -0,0 +1,35 @@ +import { createClient, fetch } from 'villus' + +type Methods = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT' + +// 此处重写fetch,请求采用UniAPP提供的uni.request +const fetchPlugin = fetch({ + fetch(url, options) { + return new Promise((resolve, reject) => { + uni.request({ + url: url.toString(), + method: options?.method as Methods, + data: options?.body as any, + header: options?.headers, + + success(res) { + resolve({ + ok: true, + status: res.statusCode, + headers: res.header, + text: async () => JSON.stringify(res.data), + json: async () => res.data, + } as Response) + }, + fail(e) { + reject(e) + }, + }) + }) + }, +}) + +export const apolloClient = createClient({ + url: 'http://it9i.com:10007/gql', + use: [fetchPlugin], +}) diff --git a/src/main.ts b/src/main.ts index 8e4466e..bd9331c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,10 +4,12 @@ import store from './store' import { routeInterceptor, requestInterceptor, prototypeInterceptor } from './interceptors' import 'virtual:uno.css' import '@/style/index.scss' +import { apolloClient } from './graphql/setup' export function createApp() { const app = createSSRApp(App) app.use(store) + app.use(apolloClient) app.use(routeInterceptor) app.use(requestInterceptor) app.use(prototypeInterceptor) diff --git a/src/manifest.json b/src/manifest.json index 80400f8..daab0b3 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,5 +1,5 @@ { - "name": "unibest", + "name": "crm-capp", "appid": "H57F2ACE4", "description": "", "versionName": "1.0.0", @@ -108,4 +108,4 @@ "base": "/unibest/" } } -} \ No newline at end of file +} diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 84d3606..f1ecc0d 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -30,9 +30,17 @@ -