1. packages\mall-cook-service\routes\upload.js
/*
* @Description: What's this for
* @Autor: WangYuan
* @Date: 2021-08-17 14:28:29
* @LastEditors: WangYuan
* @LastEditTime: 2022-03-02 15:13:34
*/
const tools = require('../utils/tools')
const multer = require('koa-multer');
const Router = require('koa-router')
const config = require('../config')
const path = require('path')
const fs = require('fs')
const dayjs =require('dayjs')
const router = new Router()
//创建文件夹
function mkdirsSync(dirname){
if(fs.existsSync(dirname)){
return true;
}else{
if(mkdirsSync(path.dirname(dirname))){
fs.mkdirSync(dirname)
return true
}
}
}
//文件上传
var storage = multer.diskStorage({
//文件保存路径
destination: function (req, file, cb) {
//appjs中koa-static配置目录为public,因此上传目录放public;也可以配置koa-static
const filePath = `${path.resolve('./public')}/img/${dayjs(Date.now()).format('YYYYMMDD')}`
//递归创建多级
if(mkdirsSync(filePath)){
cb(null, filePath)
}
},
//修改文件名称
filename: function (req, file, cb) {
var fileFormat = (file.originalname).split(".");
cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
}
})
//加载配置
var upload = multer({ storage: storage });
//路由
router.post('/upload', upload.single('file'), async (ctx, next) => {
ctx.body = {
data: `${config.serviceApi}/img/${dayjs(Date.now()).format('YYYYMMDD')}/${ctx.req.file.filename}`,
errorCode: "00000",
message: "请求成功",
}
ctx.status = 200
})
module.exports = router
2. packages\mall-cook-platform\vue.config.js
devServer: {
port: '8081', // 设置端口号
proxy: {
'/api': {
target: 'http://localhost:3000', //API服务器的地址
ws: true, //代理websockets
changeOrigin: true, // 是否跨域,虚拟的站点需要更管origin
pathRewrite: {
'^/api': '',
}
}
},
}
3. packages\mall-cook-platform\src\config\global.js
/*
* @Description: What's this for
* @Autor: WangYuan
* @Date: 2022-02-11 10:08:57
* @LastEditors: WangYuan
* @LastEditTime: 2022-11-02 09:10:53
*/
export default {
baseApi: 'http://localhost:3000/', // 后端接口地址
viewUrl: 'http://localhost:8080/#/' // iframe嵌套的H5地址,本地开发可改为本地启动的H5地址
// baseApi: 'http://www.sunmao-design.top/mall-cook/api/', // 后端接口地址
// viewUrl: 'http://139.196.223.93:4001/#/', // iframe嵌套的H5地址,本地开发可改为本地启动的H5地址
}
4. packages\mall-cook-template\src\utils\request.js
const baseUrl = 'http://localhost:3000'
5. packages\mall-cook-service\config.js
/*
* @Description: 配置信息
* @Autor: WangYuan
* @Date: 2022-02-10 19:20:33
* @LastEditors: WangYuan
* @LastEditTime: 2022-11-02 10:35:50
*/
config = {
appid: 'xxx', // 小程序appId
secret: 'xxx', // 小程序secret
serviceApi: 'http://localhost:3000', // 服务器地址
mongodbUrl: 'mongodb://localhost:27017', // mongodb数据库地址 格式:mongodb://username:password@host:port/name
jwtSecret: 'secret'
}
module.exports = config