"...git@ustchcs.com:gujinli1118/springboot-plus.git" did not exist on "6f934d8908f5a36092b13a0229cd20457a0e580c"
Commit 4614a9a3 authored by trumansdo's avatar trumansdo
Browse files

完成自定义文件组件,统一使用axios发起请求,使用方法:必须指定content-type为multipart/form-data

parent cc0c508e
package com.ibeetl.admin.core.web; package com.ibeetl.admin.core.web;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.ibeetl.admin.core.entity.CoreOrg; import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreUser; import com.ibeetl.admin.core.entity.CoreUser;
...@@ -14,6 +13,7 @@ import java.io.OutputStream; ...@@ -14,6 +13,7 @@ import java.io.OutputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
...@@ -52,14 +52,14 @@ public class FileSystemElContorller { ...@@ -52,14 +52,14 @@ public class FileSystemElContorller {
@PostMapping("/uploadAttachment") @PostMapping("/uploadAttachment")
@ResponseBody @ResponseBody
public JsonResult<String> uploadFile( public JsonResult<String> uploadFile(
@RequestParam("file") MultipartFile file, String fileBatchId, String bizType, String bizId) @RequestParam("file") MultipartFile file,
@NotBlank String fileBatchId,
String bizType,
String bizId)
throws IOException { throws IOException {
if (file.isEmpty()) { if (file.isEmpty()) {
return JsonResult.fail(); return JsonResult.fail();
} }
if (StrUtil.isBlank(fileBatchId)) {
fileBatchId = IdUtil.fastUUID();
}
CoreUser user = platformService.getCurrentUser(); CoreUser user = platformService.getCurrentUser();
CoreOrg org = platformService.getCurrentOrg(); CoreOrg org = platformService.getCurrentOrg();
FileItem fileItem = FileItem fileItem =
...@@ -78,7 +78,7 @@ public class FileSystemElContorller { ...@@ -78,7 +78,7 @@ public class FileSystemElContorller {
@PostMapping("/deleteAttachment") @PostMapping("/deleteAttachment")
@ResponseBody @ResponseBody
public JsonResult deleteFile(Long fileId, String batchFileUUID) throws IOException { public JsonResult deleteFile(Long fileId, String batchFileUUID) {
fileService.removeFile(fileId, batchFileUUID); fileService.removeFile(fileId, batchFileUUID);
return JsonResult.success(); return JsonResult.success();
} }
......
...@@ -63,10 +63,12 @@ ...@@ -63,10 +63,12 @@
"normalize.css": "7.0.0", "normalize.css": "7.0.0",
"nprogress": "0.2.0", "nprogress": "0.2.0",
"path-to-regexp": "2.4.0", "path-to-regexp": "2.4.0",
"qs": "^6.9.1",
"screenfull": "4.2.0", "screenfull": "4.2.0",
"showdown": "^1.9.1", "showdown": "^1.9.1",
"sortablejs": "1.8.4", "sortablejs": "1.8.4",
"tui-editor": "1.3.3", "tui-editor": "1.3.3",
"uuid": "^7.0.2",
"vue": "2.6.11", "vue": "2.6.11",
"vue-count-to": "1.0.13", "vue-count-to": "1.0.13",
"vue-router": "3.1.6", "vue-router": "3.1.6",
......
/* /*
* @Author: 一日看尽长安花 * @Author: 一日看尽长安花
* @since: 2020-03-16 11:16:52 * @since: 2020-03-16 11:16:52
* @LastEditTime: 2020-03-16 13:13:31 * @LastEditTime: 2020-03-19 21:04:54
* @LastEditors: 一日看尽长安花 * @LastEditors: 一日看尽长安花
* @Description: * @Description:
*/ */
......
<!-- <!--
* @Author: 一日看尽长安花 * @Author: 一日看尽长安花
* @since: 2019-10-12 16:14:37 * @since: 2019-10-12 16:14:37
* @LastEditTime: 2020-03-17 15:46:23 * @LastEditTime: 2020-03-23 14:24:32
* @LastEditors: 一日看尽长安花 * @LastEditors: 一日看尽长安花
* @Description: * @Description:
--> -->
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
> >
<div class="dialog-container"> <div class="dialog-container">
<el-form <el-form
ref="editForm" ref="editFormGP"
:rules="rules" :rules="rules"
:model="dialogData" :model="dialogData"
label-position="right" label-position="right"
...@@ -132,7 +132,7 @@ export default { ...@@ -132,7 +132,7 @@ export default {
return equalsIgnoreCase(str1, type); return equalsIgnoreCase(str1, type);
}, },
createData() { createData() {
this.$refs['editForm'].validate(valid => { this.$refs['editFormGP'].validate(valid => {
if (valid) { if (valid) {
this.$emit('create-data', this.dialogData); this.$emit('create-data', this.dialogData);
this.$emit('update:dialogVisible', false); this.$emit('update:dialogVisible', false);
...@@ -147,7 +147,7 @@ export default { ...@@ -147,7 +147,7 @@ export default {
}); });
}, },
updateData() { updateData() {
this.$refs['editForm'].validate(valid => { this.$refs['editFormGP'].validate(valid => {
if (valid) { if (valid) {
this.$emit('update-data', this.dialogData); this.$emit('update-data', this.dialogData);
this.$emit('update:dialogVisible', false); this.$emit('update:dialogVisible', false);
......
<!-- <!--
* @Author: 一日看尽长安花 * @Author: 一日看尽长安花
* @since: 2020-03-08 11:03:14 * @since: 2020-03-08 11:03:14
* @LastEditTime: 2020-03-17 20:54:30 * @LastEditTime: 2020-03-23 21:36:23
* @LastEditors: 一日看尽长安花 * @LastEditors: 一日看尽长安花
* @Description: * @Description:
--> -->
...@@ -27,11 +27,11 @@ ...@@ -27,11 +27,11 @@
:list-type="listType" :list-type="listType"
:auto-upload="autoUpload" :auto-upload="autoUpload"
:file-list="fileList_d" :file-list="fileList_d"
:http-request="updateFiles" :http-request="httpRequest"
:disabled="disabled" :disabled="disabled"
:limit="limit" :limit="limit"
:on-exceed="onExceed" :on-exceed="onExceed"
action="/core/file/uploadAttachment" :action="action"
> >
<template v-slot:default> <template v-slot:default>
<slot name="default"> </slot> <slot name="default"> </slot>
...@@ -45,6 +45,8 @@ ...@@ -45,6 +45,8 @@
</el-upload> </el-upload>
</template> </template>
<script> <script>
import { v4 as uuidv4 } from 'uuid';
import request from '@/utils/request';
import { getFileList } from '@/api/file'; import { getFileList } from '@/api/file';
export default { export default {
...@@ -63,6 +65,10 @@ export default { ...@@ -63,6 +65,10 @@ export default {
type: String, type: String,
default: null default: null
}, },
action: {
type: String,
default: '/core/file/uploadAttachment'
},
headers: { headers: {
type: Object, type: Object,
default() { default() {
...@@ -164,7 +170,8 @@ export default { ...@@ -164,7 +170,8 @@ export default {
}, },
data() { data() {
return { return {
fileList_d: this.fileList fileList_d: this.fileList,
fileBatchId_d: this.fileBatchId
}; };
}, },
watch: { watch: {
...@@ -179,18 +186,43 @@ export default { ...@@ -179,18 +186,43 @@ export default {
}, },
methods: { methods: {
loadFilelItems() { loadFilelItems() {
if (!this.fileBatchId || this.fileBatchId.trim().length <= 0) { if (!this.fileBatchId_d || this.fileBatchId_d.trim().length <= 0) {
this.fileList_d = []; this.fileList_d = [];
this.fileBatchId_d = uuidv4();
return; return;
} }
getFileList({ fileBatchId: this.fileBatchId }).then(res => { getFileList({ fileBatchId: this.fileBatchId_d }).then(res => {
const { code, data } = { ...res }; const { code, data } = { ...res };
this.fileList_d = data || []; this.fileList_d = data || [];
}); });
}, },
updateFiles(params) { httpRequest(uploadComponentParams) {
debugger; const options = uploadComponentParams;
console.log(params); let formData = new FormData();
if (options.data) {
Object.keys(options.data).forEach(key => {
formData.append(key, options.data[key]);
});
}
formData.append(options.filename, options.file, options.file.name);
formData.set('file-batch-id', this.fileBatchId_d);
formData.set('biz-id', this.data.bizId);
formData.set('biz-type', this.data.bizType);
formData.append('file-type', options.file.type);
return request({
url: options.action,
method: 'post',
timeout: undefined,
headers: {
'Content-Type': 'multipart/form-data;charset=utf-8'
},
data: formData,
onUploadProgress: progressEvent => {
const complete =
((progressEvent.loaded / progressEvent.total) * 100) | 0;
options.onProgress({ percent: complete });
}
}).then(options.onSuccess, options.onError);
} }
} }
}; };
......
/* /*
* @Author: 一日看尽长安花 * @Author: 一日看尽长安花
* @since: 2019-09-04 20:55:14 * @since: 2019-09-04 20:55:14
* @LastEditTime: 2020-03-11 16:00:50 * @LastEditTime: 2020-03-23 13:56:54
* @LastEditors: 一日看尽长安花 * @LastEditors: 一日看尽长安花
* @Description: * @Description:
*/ */
......
...@@ -2,15 +2,32 @@ ...@@ -2,15 +2,32 @@
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @Author: your name * @Author: your name
* @Date: 2019-09-09 12:16:28 * @Date: 2019-09-09 12:16:28
* @LastEditTime: 2020-03-08 15:59:43 * @LastEditTime: 2020-03-22 15:18:48
* @LastEditors: 一日看尽长安花 * @LastEditors: 一日看尽长安花
*/ */
import axios from 'axios'; import axios from 'axios';
import lodash from 'lodash';
import { MessageBox, Message } from 'element-ui'; import { MessageBox, Message } from 'element-ui';
import store from '@/store'; import store from '@/store';
import { getToken, setToken } from '@/utils/auth'; import { getToken, setToken } from '@/utils/auth';
import { toCamelCaseDeep } from '@/utils/object-util'; import { toCamelCaseDeep } from '@/utils/object-util';
const defaultTransformRequest = axios.defaults.transformRequest[0];
axios.defaults.transformRequest = [
function(data, config) {
if (
config['Content-Type'] &&
lodash.startsWith(
config['Content-Type'].toLowerCase(),
'multipart/form-data'
)
)
return data;
return defaultTransformRequest(data, config);
}
];
// create an axios instance // create an axios instance
const service = axios.create({ const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
...@@ -25,7 +42,10 @@ service.interceptors.request.use( ...@@ -25,7 +42,10 @@ service.interceptors.request.use(
/**get方法为params,post、put、delete为data */ /**get方法为params,post、put、delete为data */
const params = config.params || config.data || null; const params = config.params || config.data || null;
/**下划线转为驼峰 */ /**下划线转为驼峰 */
if (params) { if (
params &&
Object.prototype.toString.call(params) !== '[object FormData]'
) {
const sourceParams = Object.assign({}, params); const sourceParams = Object.assign({}, params);
const changedKeyParams = Object.assign( const changedKeyParams = Object.assign(
{}, {},
...@@ -37,9 +57,34 @@ service.interceptors.request.use( ...@@ -37,9 +57,34 @@ service.interceptors.request.use(
} else { } else {
config.data = changedKeyParams; config.data = changedKeyParams;
} }
} else if (
params &&
Object.prototype.toString.call(params) === '[object FormData]'
) {
/**文件上传时使用formdata对象 */
const keys = [];
for (const k of params.keys()) {
keys.push(k);
}
for (const i in keys) {
const key = keys[i];
/*todo 可能的问题:参数未对数组做处理 */
const val = params.get(key);
if (!val || val === 'null' || val === 'undefined') {
params.delete(key);
continue;
}
if (Object.prototype.toString.call(val) === '[object File]') {
/**这里应该在补充一个blob对象的判断 */
params.set(lodash.camelCase(key), val, val.name);
} else {
params.set(lodash.camelCase(key), val);
}
}
config.data = params;
} }
// do something before request is sent
if (store.getters.token) { if (store.getters.token) {
// do something before request is sent
// let each request carry token // let each request carry token
// ['Authorization'] see to MDN explain about "HTTP Authorization" // ['Authorization'] see to MDN explain about "HTTP Authorization"
// please modify it according to the actual situation // please modify it according to the actual situation
......
<!-- <!--
* @Author: 一日看尽长安花 * @Author: 一日看尽长安花
* @since: 2019-10-12 15:43:18 * @since: 2019-10-12 15:43:18
* @LastEditTime: 2020-03-17 18:11:53 * @LastEditTime: 2020-03-23 14:27:26
* @LastEditors: 一日看尽长安花 * @LastEditors: 一日看尽长安花
* @Description: * @Description:
--> -->
...@@ -377,11 +377,14 @@ export default { ...@@ -377,11 +377,14 @@ export default {
}); });
}, },
submitUpload() { submitUpload() {
debugger;
this.$refs.fileUpload.$refs.upload.submit(); this.$refs.fileUpload.$refs.upload.submit();
}, },
onUploadSuccess(response, file, fileList) { onUploadSuccess(response, file, fileList) {
debugger; const { code, message, data } = { ...response };
if (data) {
this.$children[0].$refs.detailPageGP.$props.dialogData.attachment_id =
response.data;
}
} }
} }
}; };
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment