Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
TyCoding committed Nov 30, 2020
1 parent 476e7da commit 9276e8c
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cn.tycoding.biz.controller;

import cn.hutool.core.util.URLUtil;
import cn.tycoding.common.constants.CommonConstant;
import cn.tycoding.common.constants.enums.CommonEnum;
import cn.tycoding.common.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author tycoding
* @since 2020/11/28
*/
@RestController
@RequestMapping(CommonConstant.BASE_API)
public class UploadDownController {

@Value("${server.port}")
private int port;

/**
* 文件上传
*
* @param file
* @param request
* @return
*/
@RequestMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws FileNotFoundException {
//获取文件在服务器的储存位置
String path = ResourceUtils.getURL("classpath:").getPath() + "/static/upload";
File filePath = new File(path);
System.out.println("文件的保存路径:" + path);
if (!filePath.exists() && !filePath.isDirectory()) {
System.out.println("目录不存在,创建目录:" + filePath);
filePath.mkdir();
}

//获取原始文件名称(包含格式)
String originalFileName = file.getOriginalFilename();
System.out.println("原始文件名称:" + originalFileName);

//获取文件类型,以最后一个`.`为标识
String type = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
System.out.println("文件类型:" + type);
//获取文件名称(不包含格式)
String name = originalFileName.substring(0, originalFileName.lastIndexOf("."));

//设置文件新名称: 当前时间+文件名称(不包含格式)
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String date = sdf.format(d);
String fileName = date + name + "." + type;
System.out.println("新文件名称:" + fileName);

//在指定路径下创建一个文件
File targetFile = new File(path, fileName);

//将文件保存到服务器指定位置
try {
file.transferTo(targetFile);
System.out.println("上传成功");
//将文件在服务器的存储路径返回
Map<String, Object> map = new HashMap<>();
map.put("url", "http://localhost:" + port + "/upload/" + fileName);
return new R(map);
} catch (IOException e) {
System.out.println("上传失败");
e.printStackTrace();
return new R(CommonEnum.UPLOAD_FAIL);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void update(SysUser sysUser) {
@Override
@Transactional
public void updatePass(SysUser sysUser) {
SysUser user = new SysUser().setPassword(sysUser.getPassword());
SysUser user = new SysUser()
.setId(sysUser.getId())
.setPassword(md5Util.encryptPassword(sysUser.getPassword()));
userMapper.updateById(user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public enum CommonEnum {
COMMON_ERROR(500, "系统内部错误"),
FILE_ERROR(400, "上传的文件为空"),
COMMON_SUCCESS(200, "成功"),
RESET_SUCCESS(200, "重置密码成功");
RESET_SUCCESS(200, "重置密码成功"),
UPLOAD_FAIL(400, "文件上传失败")
;

private final int code;
private final String msg;
Expand Down
4 changes: 2 additions & 2 deletions app/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
ENV = 'development'

# base api
VUE_APP_BASE_API = '/'
VUE_APP_BASE_API = '/api'

VUE_APP_API_BASE = '/'
VUE_APP_API_BASE = '/api'
4 changes: 2 additions & 2 deletions app/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
ENV = 'production'

# base api
VUE_APP_BASE_API = '/'
VUE_APP_BASE_API = '/api'

VUE_APP_API_BASE = '/'
VUE_APP_API_BASE = '/api'
4 changes: 2 additions & 2 deletions app/.env.staging
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ NODE_ENV = production
ENV = 'staging'

# base api
VUE_APP_BASE_API = '/'
VUE_APP_BASE_API = '/api'

VUE_APP_API_BASE = '/'
VUE_APP_API_BASE = '/api'
2 changes: 1 addition & 1 deletion app/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const constantRoutes = [
]

const createRouter = () => new Router({
// mode: 'history', // require service support
mode: 'history', // require service support
scrollBehavior: () => ({ y: 0 }),
routes: constantRoutes
})
Expand Down
2 changes: 2 additions & 0 deletions app/src/store/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const actions = {
logout(state.token).then(() => {
removeToken() // must remove token first
resetRouter()
commit('SET_TOKEN', '')
commit('RESET_STATE')
resolve()
}).catch(error => {
Expand All @@ -89,6 +90,7 @@ const actions = {
return new Promise(resolve => {
removeToken() // must remove token first
commit('RESET_STATE')
commit('SET_TOKEN', '')
resolve()
})
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/views/tumo/article/add.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default {
if (res.code === 200) {
this.$message.success('文章添加成功,即将跳转到文章列表页面')
setTimeout(function() {
this.$router.push({ name: 'tumoArticleList' })
window.location.href = '/tumo/article'
}, 400)
} else {
this.$message.error(res.msg)
Expand Down
2 changes: 1 addition & 1 deletion app/src/views/tumo/article/edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default {
if (res.code === 200) {
this.$message.success('文章更新成功,即将跳转到文章列表页面')
setTimeout(function() {
this.$router.push({ name: 'tumoArticleList' })
window.location.href = '/tumo/article'
}, 400)
} else {
this.$message.error(res.msg)
Expand Down
64 changes: 48 additions & 16 deletions app/src/views/tumo/blog/category/components/model.vue
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
<template>
<el-dialog title="新增/修改" :before-close="handleClose" :visible="dialogVisible" width="40%">
<el-form :model="form" :rules="rules" label-width="80px">
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="名称" prop="name">
<el-input v-model="form.name" />
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="handleSubmit">确 定</el-button>
<el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
</span>
</el-dialog>
</template>

<script>
import { categoryAdd, categoryUpdate, getCategoryById } from '@/api/category'
export default {
name: 'Model',
props: {
form: {
type: Object,
default() {
return {}
}
},
dialogVisible: {
type: Boolean,
default: false
}
},
data() {
return {
dialogVisible: false,
form: {},
rules: {
name: [
{ required: true, message: '请输入名称', trigger: 'blur' }
Expand All @@ -37,11 +29,51 @@ export default {
}
},
methods: {
fetchData(id) {
this.dialogVisible = true
if (id !== undefined) {
getCategoryById(id).then(res => {
if (res.code === 200) {
this.form = res.data
} else {
this.$message.error(res.msg)
}
})
}
},
handleClose() {
this.form = {}
this.dialogVisible = false
},
handleSubmit() {
this.$emit('modelOpen')
handleSubmit(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
if (this.form.id === undefined) {
categoryAdd(this.form).then(res => {
if (res.code === 200) {
this.$message.success('添加成功')
this.handleClose()
this.$emit('refresh')
} else {
this.$message.error(res.msg)
}
})
} else {
categoryUpdate(this.form).then(res => {
if (res.code === 200) {
this.$message.success('修改成功')
this.handleClose()
this.$emit('refresh')
} else {
this.$message.error(res.msg)
}
})
}
} else {
console.log('error submit!!')
return false
}
})
}
}
Expand Down
55 changes: 6 additions & 49 deletions app/src/views/tumo/blog/category/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</el-form-item>
<el-form-item style="padding: 0;margin: 0">
<el-button type="success" icon="el-icon-search" @click="fetchData" />
<el-button type="primary" icon="el-icon-plus" @click="handleAdd" />
<el-button type="primary" icon="el-icon-plus" @click="$refs.model.fetchData()" />
</el-form-item>
</el-form>
<el-tree
Expand All @@ -22,7 +22,7 @@
<span slot-scope="{ node, data }" class="custom-tree-node">
<span>{{ data.name }}</span>
<span>
<el-button type="text" size="mini" icon="el-icon-edit" @click="() => handleEdit(node, data)" />
<el-button type="text" size="mini" icon="el-icon-edit" @click="() => $refs.model.fetchData(data.id)" />
<el-button type="text" size="mini" icon="el-icon-delete" @click="() => handleDel(node, data)" />
</span>
</span>
Expand All @@ -45,12 +45,12 @@
</el-card>
</el-col>
</el-row>
<model :form="form" :dialog-visible="dialogVisible" @modelOpen="handleSubmit" />
<model ref="model" @refresh="fetchData" />
</div>
</template>

<script>
import { getCategoryFilterList, getCategoryById, categoryUpdate, categoryAdd, categoryDel } from '@/api/category'
import { getCategoryFilterList, categoryDel } from '@/api/category'
import { findByCategory } from '@/api/article'
import Model from './components/model'
export default {
Expand All @@ -59,20 +59,14 @@ export default {
data() {
return {
list: [],
form: {},
query: {},
articleList: [],
dialogVisible: false
articleList: []
}
},
created() {
this.fetchData()
},
methods: {
handleClose() {
this.dialogVisible = false
this.form = {}
},
fetchData() {
getCategoryFilterList(this.query).then(res => {
if (res.code === 200) {
Expand All @@ -82,43 +76,6 @@ export default {
}
})
},
handleEdit(node, data) {
getCategoryById(data.id).then(res => {
if (res.code === 200) {
this.form = res.data
this.dialogVisible = true
} else {
this.$message.error(res.msg)
}
})
},
handleAdd() {
this.dialogVisible = true
},
handleSubmit(formName) {
if (this.form.id === undefined) {
categoryAdd(this.form).then(res => {
if (res.code === 200) {
this.$message.success('添加成功')
this.fetchData()
this.handleClose()
} else {
this.$message.error(res.msg)
}
})
} else {
categoryUpdate(this.form).then(res => {
if (res.code === 200) {
this.$message.success('修改成功')
this.fetchData()
this.handleClose()
} else {
this.$message.error(res.msg)
}
})
}
},
handleNodeChange(data, node) {
findByCategory(data.id).then(res => {
this.articleList = res.data
Expand Down Expand Up @@ -148,5 +105,5 @@ export default {
</script>

<style scoped>
.custom-tree-node{flex:1;display:flex;align-items:center;justify-content:space-between;font-size:14px;padding-right:8px}.el-tree{margin-left:-24px}
.custom-tree-node{flex:1;display:flex;align-items:center;justify-content:space-between;font-size:14px;padding-right:8px}.el-tree{margin-left:-24px}
</style>
Loading

0 comments on commit 9276e8c

Please sign in to comment.