upload() {
//其他条件判断
if (this.formItem.shengbh == "") {
this.$Message.error("请刷新页面");
return;
}
if (this.formItem.shibh == "") {
this.$Message.error("中心名称不能为空");
return;
}
if (this.formItem.vsj == "") {
this.$Message.error("评价日期不能为空");
return;
}
var jsonStr = JSON.stringify(this.data);
//创建 formData 对象:很重要
let formData = new FormData();
//向 formData 对象中添加文件
//多个文件上传
formData.append("uploadFileBg", this.file[0]); // 文件对象1:报告
formData.append("uploadFileYd", this.file1[0]); // 文件对象2:表格
formData.append("id", this.formItem.id);//其他参数
formData.append("shimc", this.formItem.shimc);
formData.append("shibh", this.formItem.shibh);
formData.append("shengbh", this.formItem.shengbh);
formData.append("shengmc", this.formItem.shengmc);
formData.append("pjsj", this.formItem.pjsj);
formData.append("pjry", this.formItem.pjry);
formData.append("sts", this.formItem.sts);
formData.append("pjdf", this.formItem.pjdf);
formData.append("pjyj", this.formItem.pjyj);
formData.append("gjjy", this.formItem.gjjy);
formData.append("jsonStr", jsonStr);
//使用post方式上传
this.$http.post("/api/zjcdjscpj/doSave", formData).then(res => {
if (res.retHead.code == "0000000") {
this.$Message.success("添加成功");
this.formItem = [];
this.init();
this.file = [];
this.file1 = [];
} else {
this.$Message.error("添加失败");
}
});
},
// 添加请求拦截器
axios.interceptors.request.use(
config => {
var qs = require(‘qs‘);
//上传文件,不对config.data进行处理
if (config.url == "/api/zjcdjscpj/doSave") {
}else if(config.url == "/api/zjctbjdscpj/doSave"){
}
else if(config.url == "/api/zjctbjdscpj/redactJdscpj"){
}
else {
config.data = qs.stringify(config.data);
}
let token = localStorage.getItem("token")
if (token) {
config.headers[‘OGJJ-AuthToken‘] = token
}
return config;
}, error => {
return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use(
response => {
if (response.data.retHead.code == ‘0101005‘
|| response.data.retHead.code == ‘0101006‘
|| response.data.retHead.code == ‘0101007‘
|| response.data.retHead.code == ‘0101008‘
|| response.data.retHead.code == ‘0101009‘) {//token失效
window.location.href = ‘/#/login‘//需求方要求一旦出错立即跳转登录,所以采取这种侵入式的手段。
} else {
return response.data
}
}, error => {
return Promise.reject(error);
});