Skip to content

Commit

Permalink
移除 axios,封装 fetch;只导入必须 cryptoj-js 模块,优化构建输出
Browse files Browse the repository at this point in the history
  • Loading branch information
cxOrz committed Apr 6, 2023
1 parent 69ff74b commit a87e1c5
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 147 deletions.
30 changes: 13 additions & 17 deletions apps/server/src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ router.post('/uvtoken', async (ctx) => {
_uid: uid,
vc3,
});
ctx.body = res;
ctx.body = JSON.parse(res); // 获得的是个JSON字符串,需转换
});

router.post('/upload', async (ctx) => {
Expand Down Expand Up @@ -177,7 +177,7 @@ router.post('/upload', async (ctx) => {
buffer,
});
resolve(res);
console.log(res);
// console.log(res);
});
// 解析请求表单
form.parse(ctx.req);
Expand Down Expand Up @@ -240,13 +240,12 @@ router.post('/qrocr', async (ctx) => {
});

// 200:监听中,201:未监听,202:登录失败
router.post('/monitor/status', (ctx) => {
const { phone } = ctx.request.body as any;
router.get('/monitor/status/:phone', (ctx) => {
// 状态为正在监听
if (processMap.get(phone)) {
ctx.body = '{"code":200,"msg":"Monitoring"}';
if (processMap.get(ctx.params.phone)) {
ctx.body = { code: 200, msg: 'Monitoring' };
} else {
ctx.body = '{"code":201,"msg":"Suspended"}';
ctx.body = { code: 201, msg: 'Suspended' };
}
});

Expand All @@ -257,13 +256,13 @@ router.post('/monitor/stop/:phone', (ctx) => {
process_monitor.kill('SIGKILL');
processMap.delete(phone);
}
ctx.body = '{"code":201,"msg":"Suspended"}';
ctx.body = { code: 201, msg: 'Suspended' };
});

// base64字串需包含 credentials, monitor, mailing, cqserver 内容
router.post('/monitor/start/:phone', async (ctx) => {
if (processMap.get(ctx.params.phone) !== undefined) {
ctx.body = '{"code":200,"msg":"Already started"}';
ctx.body = { code: 200, msg: 'Already started' };
return;
}
const process_monitor = fork(process.argv[1].endsWith('ts') ? 'monitor.ts' : 'monitor.js',
Expand All @@ -279,15 +278,15 @@ router.post('/monitor/start/:phone', async (ctx) => {
switch (msg) {
case 'success': {
processMap.set(ctx.params.phone, process_monitor);
resolve('{"code":200,"msg":"Started Successfully"}');
resolve({ code: 200, msg: 'Started Successfully' });
break;
}
case 'authfail': {
resolve('{"code":202,"msg":"Authencation Failed"}');
resolve({ code: 202, msg: 'Authencation Failed' });
break;
}
case 'notconfigured': {
resolve('{"code":203,"msg":"Not Configured"}');
resolve({ code: 203, msg: 'Not Configured' });
break;
}
}
Expand All @@ -298,16 +297,13 @@ router.post('/monitor/start/:phone', async (ctx) => {

app.use(bodyparser({ enableTypes: ['json', 'form', 'text'] }));
app.use(async (ctx, next) => {
await next();
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Headers', 'Content-Type');
await next();
});
app.use(async (ctx, next) => {
ctx.set('Access-Control-Max-Age', '300');
if (ctx.method === 'OPTIONS') {
ctx.set('Access-Control-Max-Age', '300');
ctx.body = '';
}
await next();
});
app.use(router.routes());

Expand Down
5 changes: 2 additions & 3 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"@mui/icons-material": "^5.6.2",
"@mui/material": "^5.6.3",
"@nuintun/qrcode": "^3.1.3",
"axios": "^0.26.1",
"crypto-js": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "6.3.0",
"crypto-js": "workspace:*"
"react-router-dom": "6.3.0"
},
"devDependencies": {
"@types/react": "^18.0.15",
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/components/UserCard/UserCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import cryptojs from 'crypto-js';
import axios from 'axios';
import { fetch as Fetch } from '../../utils/request';
import enc from 'crypto-js/enc';
import Delete from '@mui/icons-material/Delete';
import SettingsIcon from '@mui/icons-material/Settings';
import Typography from '@mui/material/Typography';
Expand Down Expand Up @@ -138,9 +138,10 @@ function UserCard(props: UserCardProps) {
},
config: { ...props.user.config }
});
reqData = cryptojs.enc.Utf8.parse(payload).toString(cryptojs.enc.Base64);
reqData = enc.Utf8.parse(payload).toString(enc.Base64);
}
const { data: result } = await axios.post(`${reqAPI}/${props.user.phone}`, reqData);

const result = await Fetch(`${reqAPI}/${props.user.phone}`, { method: 'POST', body: reqData });
switch (result.code) {
case 200: {
setMonitorState(props.user, true); break;
Expand Down
53 changes: 30 additions & 23 deletions apps/web/src/pages/DashBoard/DashBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Alert from '@mui/material/Alert';
import ButtonBase from '@mui/material/ButtonBase';
import CircularProgress from '@mui/material/CircularProgress';
import Snackbar from '@mui/material/Snackbar';
import axios from 'axios';
import { fetch as Fetch } from '../../utils/request';
import Box from '@mui/material/Box';
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
Expand Down Expand Up @@ -60,19 +60,23 @@ function DashBoard() {
setProgress(true);
}
}, 350);
activity = await axios.post(activity_api, {
uf: userParams.uf,
_d: userParams._d,
vc3: userParams.vc3,
uid: userParams._uid

activity = await Fetch(activity_api, {
method: 'POST',
body: {
uf: userParams.uf,
_d: userParams._d,
vc3: userParams.vc3,
uid: userParams._uid
}
});
// console.log(activity.data)
// console.log(activity)
setProgress(false);
switch (activity.data) {
switch (activity) {
case 'NoActivity': setSign({ activity: { name: '无签到活动' }, status: '' }); break;
case 'AuthRequired': setSign({ activity: { name: '需重新登录' }, status: '' }); break;
case 'NoCourse': setSign({ activity: { name: '无课程' }, status: '' }); break;
default: setSign({ activity: (activity.data as Activity), status: '' });
default: setSign({ activity: (activity as Activity), status: '' });
}
};

Expand Down Expand Up @@ -133,9 +137,9 @@ function DashBoard() {
const token = await getuvToken(userParams);
// 上传文件,获取上传结果
const result_upload = await uploadFile(userParams, values['photo'] as File, token);
console.log(result_upload);
// console.log(result_upload); // 得到的是JSON字符串
// 传入objectId进行签到
res = await photoSign(userParams, sign.activity.activeId, result_upload.objectId);
res = await photoSign(userParams, sign.activity.activeId, JSON.parse(result_upload).objectId);
setBtnProgress(false);
}
showResultWithTransition(setStatus, res);
Expand Down Expand Up @@ -178,26 +182,29 @@ function DashBoard() {
setUserParams(request_IDBGET.result);
// 身份过期自动重新登陆
if (Date.now() - request_IDBGET.result.date > 432000000) {
const res = await axios.post(login_api, {
phone: request_IDBGET.result.phone,
password: request_IDBGET.result.password
const user = await Fetch(login_api, {
method: 'POST',
body: {
phone: request_IDBGET.result.phone,
password: request_IDBGET.result.password
}
});
if (res.data === 'AuthFailed') {
if (user === 'AuthFailed') {
setAlert({ msg: '重新登录失败', show: true, severity: 'error' });
} else {
const userParam: UserParamsType = {
phone: request_IDBGET.result.phone,
fid: res.data.fid,
vc3: res.data.vc3,
fid: user.fid,
vc3: user.vc3,
password: request_IDBGET.result.password,
_uid: res.data._uid,
_d: res.data._d,
uf: res.data.uf,
name: res.data.name,
_uid: user._uid,
_d: user._d,
uf: user.uf,
name: user.name,
date: new Date(),
lv: res.data.lv,
lv: user.lv,
monitor: false,
config: res.data.config
config: user.config
};
setUserParams(userParam);
// 登陆成功将新信息写入数据库
Expand Down
Loading

0 comments on commit a87e1c5

Please sign in to comment.