Skip to content

Commit

Permalink
上传图片
Browse files Browse the repository at this point in the history
  • Loading branch information
Silin7 committed Nov 12, 2021
1 parent 7e76b71 commit 4cacb09
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 70 deletions.
61 changes: 5 additions & 56 deletions controller/dynamicModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,12 @@ const checkToken = require('./systemModule/checkToken')
const dynamicDao = require('../model/dao/dynamicDao')

/**
* 发动态(图片)
* 发动态
* @token true
* @method POST
* @param author_id, author_name, author_avatar, content
* @param author_id, author_name, author_avatar, content, images
*/
const dynamic_release_img = (req, res, next) => {
let author_id = req.headers.author_id
let form = new formidable.IncomingForm();
let uploadDir = path.join(__dirname, '../../0635-files/dynamicModules', author_id);
if (!fs.existsSync(uploadDir)) {
fs.mkdir(uploadDir, (error) => {
if (error) {
res.json({
code: 500,
data: error
})
}
})
}
form.uploadDir = uploadDir;
form.parse(req, (err, fields, files) => {
if (err) {
res.json({
code: 500,
msg: err
})
} else {
let oldPath = files.file.path;
let newPath = path.join(path.dirname(oldPath), files.file.name);
let backPath = path.join('http://121.89.215.228:10010/0635-files/dynamicModules', author_id, files.file.name)
//fs.rename重命名图片名称
fs.rename(oldPath, newPath, () => {
let parameter = fields
dynamicDao.dynamic_release_img(parameter, author_id, backPath).then(result => {
res.json({
code: 0,
msg: 'success'
})
}).catch(error => {
res.json({
code: 500,
msg: JSON.stringify(error)
})
})
})
}
})
}

/**
* 发动态(文字)
* @token true
* @method POST
* @param author_id, author_name, author_avatar, content
*/
const dynamic_release_txt = async (req, res, next) => {
const dynamic_release = async (req, res, next) => {
if (!checkToken(req.headers)) {
res.json({
code: 401,
Expand All @@ -77,7 +27,7 @@ const dynamic_release_txt = async (req, res, next) => {
}
let parameter = req.body
let author_id = req.headers.author_id
await dynamicDao.dynamic_release_txt(parameter, author_id).then(result => {
await dynamicDao.dynamic_release(parameter, author_id).then(result => {
res.json({
code: 0,
msg: 'success'
Expand Down Expand Up @@ -325,8 +275,7 @@ const comment_list = async (req, res, next) => {
}

module.exports = {
dynamic_release_img,
dynamic_release_txt,
dynamic_release,
dynamic_list,
author_info,
author_dynamic_list,
Expand Down
61 changes: 61 additions & 0 deletions controller/systemModule/uploadFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @Description: 上传文件
* @Author: silin7
* @Date: 2021-11-12
*/

const formidable = require('formidable');
const path = require('path')
const fs = require('fs')

const checkToken = require('./checkToken')

/**
* 上传文件
* @token true
* @method POST
* @param author_id, file_path
*/
const upload_files = (req, res, next) => {
if (!checkToken(req.headers)) {
res.json({
code: 401,
msg: '请登录后操作'
})
return
}
let author_id = req.headers.author_id
let file_path = req.headers.file_path
let form = new formidable.IncomingForm();
let uploadDir = path.join(__dirname, '../../../0635-files/', file_path, author_id);
if (!fs.existsSync(uploadDir)) {
fs.mkdir(uploadDir, (error) => {
if (error) {
res.json({
code: 500,
data: error
})
}
})
}
form.uploadDir = uploadDir;
form.parse(req, (err, fields, files) => {
if (err) {
res.json({
code: 500,
msg: err
})
} else {
let oldPath = files.file.path;
let newPath = path.join(path.dirname(oldPath), files.file.name);
let backPath = path.join('http://121.89.215.228:10010/0635-files/dynamicModules', author_id, files.file.name)
fs.rename(oldPath, newPath, () => {
res.send(backPath)
})
}
})
}

module.exports = {
upload_files
}
15 changes: 4 additions & 11 deletions model/dao/dynamicDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
const db = require('../mySQL')

module.exports = {
// 发动态(图片)
dynamic_release_img: async (parameter, author_id, image_path) => {
let sql = 'INSERT INTO `local_dynamic` (`id`, `author_id`, `author_name`, `author_avatar`, `content`, `image`) VALUES (NULL, ?, ?, ?, ?, ?)'
let sqlParams = [author_id, parameter.author_name, parameter.author_avatar, parameter.content, image_path]
return await db.query(sql, sqlParams)
},

// 发动态(文字)
dynamic_release_txt: async (parameter, author_id) => {
let sql = 'INSERT INTO `local_dynamic` (`id`, `author_id`, `author_name`, `author_avatar`, `content`) VALUES (NULL, ?, ?, ?, ?)'
let sqlParams = [author_id, parameter.author_name, parameter.author_avatar, parameter.content]
// 发动态
dynamic_release: async (parameter, author_id) => {
let sql = 'INSERT INTO `local_dynamic` (`id`, `author_id`, `author_name`, `author_avatar`, `content`, `images`) VALUES (NULL, ?, ?, ?, ?, ?)'
let sqlParams = [author_id, parameter.author_name, parameter.author_avatar, parameter.content, parameter.images]
return await db.query(sql, sqlParams)
},

Expand Down
3 changes: 1 addition & 2 deletions routes/component/dynamicRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const dynamicModule = require('../../controller/dynamicModule');
let dynamicRouter = express.Router();

dynamicRouter
.post('/dynamic/dynamic_release_img', dynamicModule.dynamic_release_img)
.post('/dynamic/dynamic_release_txt', dynamicModule.dynamic_release_txt)
.post('/dynamic/dynamic_release', dynamicModule.dynamic_release)
.get('/dynamic/dynamic_list', dynamicModule.dynamic_list)
.get('/dynamic/author_info', dynamicModule.author_info)
.get('/dynamic/author_dynamic_list', dynamicModule.author_dynamic_list)
Expand Down
4 changes: 3 additions & 1 deletion routes/component/systemRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

const express = require('express');
const wxLogin = require('../../controller/systemModule/wxLogin');
const uploadFiles = require('../../controller/systemModule/uploadFiles');

let systemRouter = express.Router();

systemRouter
.get('/system/wx_login', wxLogin.wx_login);
.get('/system/wx_login', wxLogin.wx_login)
.post('/system/upload_files', uploadFiles.upload_files);


module.exports = systemRouter;

0 comments on commit 4cacb09

Please sign in to comment.