lkb-express-base 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,SAAS,CAAA;AAE7B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAcrD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,GAAG,EAAE,OAAO,CAAC,OAAO,EACpB,aAAa,EAAE,eAAe,CAAC,GAAG,CAAC,QAgFpC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
export { default } from "./declarations/src/index.js";
|
|
3
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGtiLWV4cHJlc3MtYmFzZS5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var multer = require('multer');
|
|
6
|
+
var express = require('express');
|
|
7
|
+
var node_stream = require('node:stream');
|
|
8
|
+
var os = require('os');
|
|
9
|
+
var nodeInfo = require('node-version');
|
|
10
|
+
var Upload = require('graphql-upload/Upload.js');
|
|
11
|
+
|
|
12
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var multer__default = /*#__PURE__*/_interopDefault(multer);
|
|
15
|
+
var express__default = /*#__PURE__*/_interopDefault(express);
|
|
16
|
+
var os__default = /*#__PURE__*/_interopDefault(os);
|
|
17
|
+
var nodeInfo__default = /*#__PURE__*/_interopDefault(nodeInfo);
|
|
18
|
+
var Upload__default = /*#__PURE__*/_interopDefault(Upload);
|
|
19
|
+
|
|
20
|
+
const osNamesMap = {
|
|
21
|
+
Linux: 'Linux',
|
|
22
|
+
Darwin: 'MacOS',
|
|
23
|
+
Windows_NT: 'Windows'
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 获取服务器信息。
|
|
28
|
+
*/
|
|
29
|
+
async function getServerInfo() {
|
|
30
|
+
const osTypeStr = osNamesMap[os__default["default"].type()];
|
|
31
|
+
const networkObj = getNetworkInfo();
|
|
32
|
+
let info = {
|
|
33
|
+
type: osTypeStr,
|
|
34
|
+
//系统类型
|
|
35
|
+
tmpdir: os__default["default"].tmpdir(),
|
|
36
|
+
hostname: os__default["default"].hostname(),
|
|
37
|
+
//系统主机名
|
|
38
|
+
localIP: networkObj.localIP,
|
|
39
|
+
//本机IP
|
|
40
|
+
networkInterfaces: networkObj.networkIP,
|
|
41
|
+
//网络IP
|
|
42
|
+
release: os__default["default"].release(),
|
|
43
|
+
totalmem: Math.floor(os__default["default"].totalmem() / (1024 * 1024)) + 'M',
|
|
44
|
+
//系统内存
|
|
45
|
+
freemem: Math.floor(os__default["default"].freemem() / (1024 * 1024)) + 'M',
|
|
46
|
+
//系统空闲内存
|
|
47
|
+
homedir: os__default["default"].homedir(),
|
|
48
|
+
uptimeStr: Math.floor(os__default["default"].uptime() / (60 * 60)) + '小时',
|
|
49
|
+
nodeVersion: nodeInfo__default["default"].original
|
|
50
|
+
};
|
|
51
|
+
return info;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 获取服务器网络信息。
|
|
58
|
+
*/
|
|
59
|
+
function getNetworkInfo() {
|
|
60
|
+
let localIP = '';
|
|
61
|
+
let networkIP = '';
|
|
62
|
+
const networkInterfaces = os__default["default"].networkInterfaces();
|
|
63
|
+
for (const key in networkInterfaces) {
|
|
64
|
+
if (Object.prototype.hasOwnProperty.call(networkInterfaces, key)) {
|
|
65
|
+
const list = networkInterfaces[key];
|
|
66
|
+
list.forEach(d => {
|
|
67
|
+
if (d.family === 'IPv4') {
|
|
68
|
+
if (d.internal) {
|
|
69
|
+
localIP = d.address;
|
|
70
|
+
} else {
|
|
71
|
+
networkIP = d.address;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
localIP,
|
|
79
|
+
networkIP
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// https://github.com/ali-sdk/ali-oss
|
|
84
|
+
const OSS = require('ali-oss');
|
|
85
|
+
const {
|
|
86
|
+
v4: uuidv4
|
|
87
|
+
} = require('uuid');
|
|
88
|
+
let client = null;
|
|
89
|
+
function getClient() {
|
|
90
|
+
if (client) return client;
|
|
91
|
+
client = new OSS({
|
|
92
|
+
region: 'oss-cn-hangzhou',
|
|
93
|
+
accessKeyId: process.env.NEXT_PUBLIC_OSS_ACCESS_KEY_ID,
|
|
94
|
+
accessKeySecret: process.env.NEXT_PUBLIC_OSS_ACCESS_KEY_SECRET,
|
|
95
|
+
authorizationV4: true,
|
|
96
|
+
bucket: 'lkb-assets'
|
|
97
|
+
});
|
|
98
|
+
return client;
|
|
99
|
+
}
|
|
100
|
+
const uploadOssFile = async (fileName, file, folder = '') => {
|
|
101
|
+
try {
|
|
102
|
+
const client = getClient();
|
|
103
|
+
|
|
104
|
+
// Extract the file extension from the fileName
|
|
105
|
+
const extension = fileName.split('.').pop(); // Get the last part after the dot
|
|
106
|
+
const uniqueFileName = `${folder}${uuidv4()}.${extension}`; // Append UUID and use the original extension
|
|
107
|
+
|
|
108
|
+
const result = await client.put(uniqueFileName, file);
|
|
109
|
+
console.log('Upload Success:', result);
|
|
110
|
+
return result;
|
|
111
|
+
} catch (err) {
|
|
112
|
+
console.error('Upload Error:', err);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Function to delete a file
|
|
117
|
+
async function deleteOssFile(fileName) {
|
|
118
|
+
try {
|
|
119
|
+
const client = getClient();
|
|
120
|
+
const result = await client.delete(fileName);
|
|
121
|
+
console.log('Delete Success:', result);
|
|
122
|
+
return result;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
console.error('Delete Error:', err);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function installAliyunOssRouter(app, upload) {
|
|
129
|
+
// 上传的文件
|
|
130
|
+
app.post('/api/oss/upload', upload.single('file'), async (req, res) => {
|
|
131
|
+
try {
|
|
132
|
+
if (!req.file) {
|
|
133
|
+
return res.status(400).json({
|
|
134
|
+
error: '未指定文件'
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const fileName = req.file.originalname; // Get the original file name
|
|
138
|
+
// Call the uploadFile function to upload to OSS
|
|
139
|
+
const result = await uploadOssFile(fileName, node_stream.Readable.from(req.file.buffer), 'images/');
|
|
140
|
+
if (result) {
|
|
141
|
+
return res.status(200).json({
|
|
142
|
+
message: '文件上传成功',
|
|
143
|
+
data: result
|
|
144
|
+
});
|
|
145
|
+
} else {
|
|
146
|
+
return res.status(500).json({
|
|
147
|
+
error: '文件上传失败'
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.error('Upload Error:', err);
|
|
152
|
+
return res.status(500).json({
|
|
153
|
+
error: '服务器错误'
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// 删除文件
|
|
159
|
+
app.post('/api/oss/delete', async (req, res) => {
|
|
160
|
+
try {
|
|
161
|
+
if (!req.body.fileName) {
|
|
162
|
+
return res.status(400).json({
|
|
163
|
+
error: '未指定文件名'
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const fileName = req.body.fileName;
|
|
167
|
+
const result = await deleteOssFile(fileName);
|
|
168
|
+
if (result) {
|
|
169
|
+
return res.status(200).json({
|
|
170
|
+
message: '文件删除成功',
|
|
171
|
+
data: result
|
|
172
|
+
});
|
|
173
|
+
} else {
|
|
174
|
+
return res.status(500).json({
|
|
175
|
+
error: '文件删除失败'
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
} catch (err) {
|
|
179
|
+
console.error('Delete Error:', err);
|
|
180
|
+
return res.status(500).json({
|
|
181
|
+
error: '服务器错误'
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
//---------------------------------------------------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
// 使用内存存储上传的文件
|
|
190
|
+
const storage = multer__default["default"].memoryStorage();
|
|
191
|
+
const upload = multer__default["default"]({
|
|
192
|
+
storage: storage
|
|
193
|
+
});
|
|
194
|
+
function extendExpressApp(app, commonContext) {
|
|
195
|
+
app.use(express__default["default"].json()); // 解析 JSON 请求体
|
|
196
|
+
|
|
197
|
+
// 全站静态资源
|
|
198
|
+
app.use('/assets', express__default["default"].static('public/assets', {
|
|
199
|
+
index: false,
|
|
200
|
+
redirect: false,
|
|
201
|
+
lastModified: false
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
// 上传的图片
|
|
205
|
+
app.use('/upload/images', express__default["default"].static('public/upload/images', {
|
|
206
|
+
index: false,
|
|
207
|
+
redirect: false,
|
|
208
|
+
lastModified: false
|
|
209
|
+
}));
|
|
210
|
+
|
|
211
|
+
// 上传的文件
|
|
212
|
+
app.use('/upload/files', express__default["default"].static('public/upload/files', {
|
|
213
|
+
setHeaders(res) {
|
|
214
|
+
res.setHeader('Content-Type', 'application/octet-stream');
|
|
215
|
+
},
|
|
216
|
+
index: false,
|
|
217
|
+
redirect: false,
|
|
218
|
+
lastModified: false
|
|
219
|
+
}));
|
|
220
|
+
|
|
221
|
+
// 获取服务器参数信息
|
|
222
|
+
app.get('/api/monitor/server/info', async (req, res) => {
|
|
223
|
+
const serverInfo = await getServerInfo();
|
|
224
|
+
res.json({
|
|
225
|
+
success: true,
|
|
226
|
+
data: serverInfo
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
installAliyunOssRouter(app, upload);
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 上传微信小程序用户头像
|
|
233
|
+
*/
|
|
234
|
+
app.post('/api/user/avatar/upload', upload.single('file'), async (req, res) => {
|
|
235
|
+
try {
|
|
236
|
+
var _context$session, _context$session2;
|
|
237
|
+
const context = await commonContext.withRequest(req, res);
|
|
238
|
+
// console.log("Session:", context.session);
|
|
239
|
+
const openId = (_context$session = context.session) === null || _context$session === void 0 ? void 0 : _context$session.openid;
|
|
240
|
+
const userId = (_context$session2 = context.session) === null || _context$session2 === void 0 ? void 0 : _context$session2.itemId;
|
|
241
|
+
if (!req.file) return res.status(400).json({
|
|
242
|
+
error: '未指定头像文件'
|
|
243
|
+
});
|
|
244
|
+
if (!userId) return res.status(401).json({
|
|
245
|
+
error: '未登录'
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// 文件信息
|
|
249
|
+
const file = req.file;
|
|
250
|
+
const extension = file.originalname.split('.').pop();
|
|
251
|
+
// console.log(Object.keys(file));
|
|
252
|
+
|
|
253
|
+
const upload = new Upload__default["default"]();
|
|
254
|
+
upload.resolve({
|
|
255
|
+
createReadStream: () => node_stream.Readable.from(file.buffer),
|
|
256
|
+
filename: openId + '.' + extension,
|
|
257
|
+
mimetype: file.mimetype,
|
|
258
|
+
encoding: 'utf-8'
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// 使用 Keystone 的文件上传功能更新用户头像
|
|
262
|
+
const updatedUser = await context.query.User.updateOne({
|
|
263
|
+
where: {
|
|
264
|
+
id: userId
|
|
265
|
+
},
|
|
266
|
+
data: {
|
|
267
|
+
avatar: {
|
|
268
|
+
upload
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
query: 'id name phone avatar {id url width height}'
|
|
272
|
+
});
|
|
273
|
+
res.json({
|
|
274
|
+
success: true,
|
|
275
|
+
user: updatedUser
|
|
276
|
+
});
|
|
277
|
+
} catch (error) {
|
|
278
|
+
console.error('头像上传错误:', error);
|
|
279
|
+
res.status(500).json({
|
|
280
|
+
error: '头像上传失败'
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
exports["default"] = extendExpressApp;
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import multer from 'multer';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import nodeInfo from 'node-version';
|
|
6
|
+
import Upload from 'graphql-upload/Upload.js';
|
|
7
|
+
|
|
8
|
+
const osNamesMap = {
|
|
9
|
+
Linux: 'Linux',
|
|
10
|
+
Darwin: 'MacOS',
|
|
11
|
+
Windows_NT: 'Windows'
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 获取服务器信息。
|
|
16
|
+
*/
|
|
17
|
+
async function getServerInfo() {
|
|
18
|
+
const osTypeStr = osNamesMap[os.type()];
|
|
19
|
+
const networkObj = getNetworkInfo();
|
|
20
|
+
let info = {
|
|
21
|
+
type: osTypeStr,
|
|
22
|
+
//系统类型
|
|
23
|
+
tmpdir: os.tmpdir(),
|
|
24
|
+
hostname: os.hostname(),
|
|
25
|
+
//系统主机名
|
|
26
|
+
localIP: networkObj.localIP,
|
|
27
|
+
//本机IP
|
|
28
|
+
networkInterfaces: networkObj.networkIP,
|
|
29
|
+
//网络IP
|
|
30
|
+
release: os.release(),
|
|
31
|
+
totalmem: Math.floor(os.totalmem() / (1024 * 1024)) + 'M',
|
|
32
|
+
//系统内存
|
|
33
|
+
freemem: Math.floor(os.freemem() / (1024 * 1024)) + 'M',
|
|
34
|
+
//系统空闲内存
|
|
35
|
+
homedir: os.homedir(),
|
|
36
|
+
uptimeStr: Math.floor(os.uptime() / (60 * 60)) + '小时',
|
|
37
|
+
nodeVersion: nodeInfo.original
|
|
38
|
+
};
|
|
39
|
+
return info;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 获取服务器网络信息。
|
|
46
|
+
*/
|
|
47
|
+
function getNetworkInfo() {
|
|
48
|
+
let localIP = '';
|
|
49
|
+
let networkIP = '';
|
|
50
|
+
const networkInterfaces = os.networkInterfaces();
|
|
51
|
+
for (const key in networkInterfaces) {
|
|
52
|
+
if (Object.prototype.hasOwnProperty.call(networkInterfaces, key)) {
|
|
53
|
+
const list = networkInterfaces[key];
|
|
54
|
+
list.forEach(d => {
|
|
55
|
+
if (d.family === 'IPv4') {
|
|
56
|
+
if (d.internal) {
|
|
57
|
+
localIP = d.address;
|
|
58
|
+
} else {
|
|
59
|
+
networkIP = d.address;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
localIP,
|
|
67
|
+
networkIP
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// https://github.com/ali-sdk/ali-oss
|
|
72
|
+
const OSS = require('ali-oss');
|
|
73
|
+
const {
|
|
74
|
+
v4: uuidv4
|
|
75
|
+
} = require('uuid');
|
|
76
|
+
let client = null;
|
|
77
|
+
function getClient() {
|
|
78
|
+
if (client) return client;
|
|
79
|
+
client = new OSS({
|
|
80
|
+
region: 'oss-cn-hangzhou',
|
|
81
|
+
accessKeyId: process.env.NEXT_PUBLIC_OSS_ACCESS_KEY_ID,
|
|
82
|
+
accessKeySecret: process.env.NEXT_PUBLIC_OSS_ACCESS_KEY_SECRET,
|
|
83
|
+
authorizationV4: true,
|
|
84
|
+
bucket: 'lkb-assets'
|
|
85
|
+
});
|
|
86
|
+
return client;
|
|
87
|
+
}
|
|
88
|
+
const uploadOssFile = async (fileName, file, folder = '') => {
|
|
89
|
+
try {
|
|
90
|
+
const client = getClient();
|
|
91
|
+
|
|
92
|
+
// Extract the file extension from the fileName
|
|
93
|
+
const extension = fileName.split('.').pop(); // Get the last part after the dot
|
|
94
|
+
const uniqueFileName = `${folder}${uuidv4()}.${extension}`; // Append UUID and use the original extension
|
|
95
|
+
|
|
96
|
+
const result = await client.put(uniqueFileName, file);
|
|
97
|
+
console.log('Upload Success:', result);
|
|
98
|
+
return result;
|
|
99
|
+
} catch (err) {
|
|
100
|
+
console.error('Upload Error:', err);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Function to delete a file
|
|
105
|
+
async function deleteOssFile(fileName) {
|
|
106
|
+
try {
|
|
107
|
+
const client = getClient();
|
|
108
|
+
const result = await client.delete(fileName);
|
|
109
|
+
console.log('Delete Success:', result);
|
|
110
|
+
return result;
|
|
111
|
+
} catch (err) {
|
|
112
|
+
console.error('Delete Error:', err);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function installAliyunOssRouter(app, upload) {
|
|
117
|
+
// 上传的文件
|
|
118
|
+
app.post('/api/oss/upload', upload.single('file'), async (req, res) => {
|
|
119
|
+
try {
|
|
120
|
+
if (!req.file) {
|
|
121
|
+
return res.status(400).json({
|
|
122
|
+
error: '未指定文件'
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const fileName = req.file.originalname; // Get the original file name
|
|
126
|
+
// Call the uploadFile function to upload to OSS
|
|
127
|
+
const result = await uploadOssFile(fileName, Readable.from(req.file.buffer), 'images/');
|
|
128
|
+
if (result) {
|
|
129
|
+
return res.status(200).json({
|
|
130
|
+
message: '文件上传成功',
|
|
131
|
+
data: result
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
return res.status(500).json({
|
|
135
|
+
error: '文件上传失败'
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
} catch (err) {
|
|
139
|
+
console.error('Upload Error:', err);
|
|
140
|
+
return res.status(500).json({
|
|
141
|
+
error: '服务器错误'
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// 删除文件
|
|
147
|
+
app.post('/api/oss/delete', async (req, res) => {
|
|
148
|
+
try {
|
|
149
|
+
if (!req.body.fileName) {
|
|
150
|
+
return res.status(400).json({
|
|
151
|
+
error: '未指定文件名'
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const fileName = req.body.fileName;
|
|
155
|
+
const result = await deleteOssFile(fileName);
|
|
156
|
+
if (result) {
|
|
157
|
+
return res.status(200).json({
|
|
158
|
+
message: '文件删除成功',
|
|
159
|
+
data: result
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
return res.status(500).json({
|
|
163
|
+
error: '文件删除失败'
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
} catch (err) {
|
|
167
|
+
console.error('Delete Error:', err);
|
|
168
|
+
return res.status(500).json({
|
|
169
|
+
error: '服务器错误'
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
//---------------------------------------------------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
// 使用内存存储上传的文件
|
|
178
|
+
const storage = multer.memoryStorage();
|
|
179
|
+
const upload = multer({
|
|
180
|
+
storage: storage
|
|
181
|
+
});
|
|
182
|
+
function extendExpressApp(app, commonContext) {
|
|
183
|
+
app.use(express.json()); // 解析 JSON 请求体
|
|
184
|
+
|
|
185
|
+
// 全站静态资源
|
|
186
|
+
app.use('/assets', express.static('public/assets', {
|
|
187
|
+
index: false,
|
|
188
|
+
redirect: false,
|
|
189
|
+
lastModified: false
|
|
190
|
+
}));
|
|
191
|
+
|
|
192
|
+
// 上传的图片
|
|
193
|
+
app.use('/upload/images', express.static('public/upload/images', {
|
|
194
|
+
index: false,
|
|
195
|
+
redirect: false,
|
|
196
|
+
lastModified: false
|
|
197
|
+
}));
|
|
198
|
+
|
|
199
|
+
// 上传的文件
|
|
200
|
+
app.use('/upload/files', express.static('public/upload/files', {
|
|
201
|
+
setHeaders(res) {
|
|
202
|
+
res.setHeader('Content-Type', 'application/octet-stream');
|
|
203
|
+
},
|
|
204
|
+
index: false,
|
|
205
|
+
redirect: false,
|
|
206
|
+
lastModified: false
|
|
207
|
+
}));
|
|
208
|
+
|
|
209
|
+
// 获取服务器参数信息
|
|
210
|
+
app.get('/api/monitor/server/info', async (req, res) => {
|
|
211
|
+
const serverInfo = await getServerInfo();
|
|
212
|
+
res.json({
|
|
213
|
+
success: true,
|
|
214
|
+
data: serverInfo
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
installAliyunOssRouter(app, upload);
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 上传微信小程序用户头像
|
|
221
|
+
*/
|
|
222
|
+
app.post('/api/user/avatar/upload', upload.single('file'), async (req, res) => {
|
|
223
|
+
try {
|
|
224
|
+
var _context$session, _context$session2;
|
|
225
|
+
const context = await commonContext.withRequest(req, res);
|
|
226
|
+
// console.log("Session:", context.session);
|
|
227
|
+
const openId = (_context$session = context.session) === null || _context$session === void 0 ? void 0 : _context$session.openid;
|
|
228
|
+
const userId = (_context$session2 = context.session) === null || _context$session2 === void 0 ? void 0 : _context$session2.itemId;
|
|
229
|
+
if (!req.file) return res.status(400).json({
|
|
230
|
+
error: '未指定头像文件'
|
|
231
|
+
});
|
|
232
|
+
if (!userId) return res.status(401).json({
|
|
233
|
+
error: '未登录'
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// 文件信息
|
|
237
|
+
const file = req.file;
|
|
238
|
+
const extension = file.originalname.split('.').pop();
|
|
239
|
+
// console.log(Object.keys(file));
|
|
240
|
+
|
|
241
|
+
const upload = new Upload();
|
|
242
|
+
upload.resolve({
|
|
243
|
+
createReadStream: () => Readable.from(file.buffer),
|
|
244
|
+
filename: openId + '.' + extension,
|
|
245
|
+
mimetype: file.mimetype,
|
|
246
|
+
encoding: 'utf-8'
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// 使用 Keystone 的文件上传功能更新用户头像
|
|
250
|
+
const updatedUser = await context.query.User.updateOne({
|
|
251
|
+
where: {
|
|
252
|
+
id: userId
|
|
253
|
+
},
|
|
254
|
+
data: {
|
|
255
|
+
avatar: {
|
|
256
|
+
upload
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
query: 'id name phone avatar {id url width height}'
|
|
260
|
+
});
|
|
261
|
+
res.json({
|
|
262
|
+
success: true,
|
|
263
|
+
user: updatedUser
|
|
264
|
+
});
|
|
265
|
+
} catch (error) {
|
|
266
|
+
console.error('头像上传错误:', error);
|
|
267
|
+
res.status(500).json({
|
|
268
|
+
error: '头像上传失败'
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export { extendExpressApp as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lkb-express-base",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/lkb-express-base.cjs.js",
|
|
5
|
+
"module": "dist/lkb-express-base.esm.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/lkb-express-base.cjs.js",
|
|
9
|
+
"module": "./dist/lkb-express-base.esm.js",
|
|
10
|
+
"default": "./dist/lkb-express-base.cjs.js"
|
|
11
|
+
},
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"express": "^4.18.2",
|
|
16
|
+
"node-version": "^3.0.0",
|
|
17
|
+
"ali-oss": "^6.23.0",
|
|
18
|
+
"multer": "^1.4.5-lts.1",
|
|
19
|
+
"graphql-upload": "^15.0.2",
|
|
20
|
+
"uuid": "^11.0.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"lkb-core": "workspace:^"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/express": "^4.17.14",
|
|
27
|
+
"@types/multer": "^1.4.12"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"preconstruct": {
|
|
33
|
+
"entrypoints": [
|
|
34
|
+
"index.ts"
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
}
|