From 65e7cfe11647dc785d03014423c7e86c03d8fa23 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 8 May 2023 20:10:15 +0800 Subject: [PATCH] feat(admin-next): add message and group list --- pnpm-lock.yaml | 13 +++++ server/admin-next/package.json | 1 + server/admin-next/src/client/App.tsx | 38 +++++++++++--- server/admin-next/src/client/fields.ts | 69 +++++++++++++++++++------ server/admin-next/src/client/request.ts | 46 +++++++++++++++++ 5 files changed, 146 insertions(+), 21 deletions(-) create mode 100644 server/admin-next/src/client/request.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9b264526a88..c263a243872 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1587,6 +1587,9 @@ importers: '@fastify/busboy': specifier: ^1.1.0 version: 1.1.0 + axios: + specifier: ^1.4.0 + version: 1.4.0 body-parser: specifier: ^1.20.1 version: 1.20.1 @@ -14547,6 +14550,16 @@ packages: - debug dev: false + /axios@1.4.0: + resolution: {integrity: sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==} + dependencies: + follow-redirects: 1.15.2 + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false + /b-tween@0.3.3: resolution: {integrity: sha512-oEHegcRpA7fAuc9KC4nktucuZn2aS8htymCPcP3qkEGPqiBH+GfqtqoG2l7LxHngg6O0HFM7hOeOYExl1Oz4ZA==} dev: false diff --git a/server/admin-next/package.json b/server/admin-next/package.json index 1168cc546db..171597e155e 100644 --- a/server/admin-next/package.json +++ b/server/admin-next/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "@fastify/busboy": "^1.1.0", + "axios": "^1.4.0", "body-parser": "^1.20.1", "compression": "^1.7.4", "express": "^4.18.2", diff --git a/server/admin-next/src/client/App.tsx b/server/admin-next/src/client/App.tsx index d1db1dff999..7d562d3ac2c 100644 --- a/server/admin-next/src/client/App.tsx +++ b/server/admin-next/src/client/App.tsx @@ -5,10 +5,12 @@ import { Resource, Tushan, } from 'tushan'; +import { IconMessage, IconUser } from 'tushan/icon'; import { authProvider } from './auth'; -import { photoFields, userFields } from './fields'; +import { groupFields, messageFields, userFields } from './fields'; +import { httpClient } from './request'; -const dataProvider = jsonServerProvider('https://jsonplaceholder.typicode.com'); +const dataProvider = jsonServerProvider('/admin/api', httpClient); function App() { return ( @@ -20,11 +22,12 @@ function App() { } list={ } list={ + } + /> + + } + list={ + } diff --git a/server/admin-next/src/client/fields.ts b/server/admin-next/src/client/fields.ts index 72effdf64a8..03392ee9145 100644 --- a/server/admin-next/src/client/fields.ts +++ b/server/admin-next/src/client/fields.ts @@ -3,15 +3,12 @@ import { createEmailField, createImageField, createTextField, - createUrlField, + createBooleanField, } from 'tushan'; export const userFields = [ createTextField('id', { label: 'ID', - }), - createTextField('name', { - label: 'Name', list: { sort: true, }, @@ -19,25 +16,67 @@ export const userFields = [ createEmailField('email', { label: 'Email', }), - createUrlField('website', { - label: 'Website', + createTextField('nickname', { + label: 'Nickname', + }), + createTextField('discriminator', { + label: 'Discriminator', + }), + createBooleanField('temporary', { + label: 'Temporary', + }), + createImageField('avatar', { + label: 'Avatar', + height: 42, + }), + createTextField('settings', { + label: 'Settings', + list: { + width: 200, + }, + edit: { + hidden: true, // wait for json field + }, + }), + createTextField('createdAt', { + label: 'Created At', }), ]; -export const photoFields = [ +export const messageFields = [ createTextField('id', { label: 'ID', }), - createTextField('albumId', { - label: 'AlbumId', + createTextField('content', { + label: 'Content', + }), + createTextField('author', { + label: 'Author', }), - createTextField('title', { - label: 'Title', + createTextField('groupId'), + createTextField('converseId'), + createBooleanField('hasRecall'), + createTextField('reactions'), + createTextField('createdAt'), +]; + +export const groupFields = [ + createTextField('id', { + label: 'ID', }), - createImageField('url', { - label: 'Url', + createTextField('name'), + createTextField('owner'), + createTextField('members.length', { + edit: { + hidden: true, + }, }), - createAvatarField('thumbnailUrl', { - label: 'ThumbnailUrl', + createTextField('panels.length', { + edit: { + hidden: true, + }, }), + createTextField('roles'), + createTextField('fallbackPermissions'), + createTextField('createdAt'), ]; diff --git a/server/admin-next/src/client/request.ts b/server/admin-next/src/client/request.ts new file mode 100644 index 00000000000..e475e750c0d --- /dev/null +++ b/server/admin-next/src/client/request.ts @@ -0,0 +1,46 @@ +import axios from 'axios'; +import { authStorageKey } from './auth'; +import _set from 'lodash/set'; +import { fetchJSON } from 'tushan'; + +/** + * 创建请求实例 + */ +function createRequest() { + const ins = axios.create({ + baseURL: '/admin/api', + }); + + ins.interceptors.request.use(async (val) => { + try { + const { token } = JSON.parse( + window.localStorage.getItem(authStorageKey) ?? '{}' + ); + _set(val, ['headers', 'Authorization'], `Bearer ${token}`); + + return val; + } catch (err) { + throw err; + } + }); + + return ins; +} + +export const request = createRequest(); + +export const httpClient: typeof fetchJSON = (url, options = {}) => { + try { + if (!options.headers) { + options.headers = new Headers({ Accept: 'application/json' }); + } + const { token } = JSON.parse( + window.localStorage.getItem(authStorageKey) ?? '{}' + ); + (options.headers as Headers).set('Authorization', `Bearer ${token}`); + + return fetchJSON(url, options); + } catch (err) { + return Promise.reject(); + } +};