diff --git a/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl b/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl index 95940ef2..6c14804c 100644 --- a/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl +++ b/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl @@ -13,6 +13,16 @@ headers: {'Content-Type': 'application/json;charset=UTF-8'}, }); var uploadService = axios.create({ + method: 'post', + timeout: 30000, + baseURL: '${context?default("")}', + headers: {'Content-Type': 'multipart/form-data'}, + onUploadProgress: function (progressEvent) { + var complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%' + nav.tipShow("上传中(" + complete + ")") + }, + }); + var importService = axios.create({ method: 'post', timeout: 30000, baseURL: '${context?default("")}', @@ -64,8 +74,37 @@ } return Promise.resolve(rsp); }) - } - function upRequest(data) { + }; + function uploadRequest(data) { + nav.barShow(); + nav.tipShow("上传中..."); + var fd = new FormData(); + fd.append("file", data.data); + data.data = fd; + return uploadService.request(data) + .then(function (response) { + nav.tipClose(); + if (response.data.errors && response.data.errors.length == 0) { + nav.barFinish(); + } else if (response.data.errors && response.data.errors.length > 0) { + nav.barError(); + } else { + response.data = {errors: [{message: '服务器响应错误'}]}; + nav.barError(); + } + return Promise.resolve(response.data); + }, function (error) { + nav.barError(); + const rsp = {errors: []}; + if (!error.response) { + rsp.errors.push({message: error.message}); + } else { + rsp.errors.push(handleError(error.response.status)); + } + return Promise.resolve(rsp); + }) + }; + function importRequest(data) { nav.barShow(); nav.tipShow("上传中..."); var fd = new FormData(); @@ -179,8 +218,13 @@ this.post = function (callback) { switch (this.mMethod) { case "upload": + return uploadRequest({ + url: '/ajax/' + this.mModule + "/" + this.mTarget + "/" + this.mMethod, + data: this.mData + }).then(callback); + break; case "imports": - return upRequest({ + return importRequest({ url: '/ajax/' + this.mModule + "/" + this.mTarget + "/" + this.mMethod, data: this.mData }).then(callback); @@ -231,7 +275,7 @@ }; this.imports = function (callback) { this.mMethod = "imports"; - return upRequest({ + return importRequest({ url: '/ajax/' + this.mModule + "/" + this.mTarget + "/" + this.mMethod, data: this.mData }).then(callback) @@ -245,7 +289,7 @@ }; this.upload = function (callback) { this.mMethod = "upload"; - return upRequest({ + return uploadRequest({ url: '/ajax/' + this.mModule + "/" + this.mTarget + "/" + this.mMethod, data: this.mData }).then(callback) @@ -282,71 +326,51 @@ this.select = select; }, onTemplate: function () { - new Ajax() - .module(this.module) - .target(this.target) - .method("template") - .template(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } - }) + new Ajax(this.module, this.target, "template").post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } + }) }, onImport: function (item) { utils.selectFile(function (files) { - new Ajax() - .module(this.module) - .target(this.target) - .data(files[0]) - .imports(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } - }); + new Ajax(this.module, this.target, "imports", files[0]).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } + }); }.bind(this)) }, onExport: function () { - new Ajax() - .module(this.module) - .target(this.target) - .data(this.vm) - .exports(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } - }); + new Ajax(this.module, this.target, "exports", this.vm).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } + }); }, onSave: function () { this.$refs['form'].validate(function (valid) { if (valid) { if (this.form.id) { - new Ajax() - .module(this.module) - .target(this.target) - .data(this.form) - .update(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } else { - this.onFind(); - this.$refs['form'].resetFields(); - this.form.dialog = false; - } - }.bind(this)) + new Ajax(this.module, this.target, "update", this.form).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + this.onFind(); + this.$refs['form'].resetFields(); + this.form.dialog = false; + } + }.bind(this)) } else { - new Ajax() - .module(this.module) - .target(this.target) - .data(this.form) - .create(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } else { - this.onFind(); - this.$refs['form'].resetFields(); - this.form.dialog = false; - } - }.bind(this)) + new Ajax(this.module, this.target, "create", this.form).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + this.onFind(); + this.$refs['form'].resetFields(); + this.form.dialog = false; + } + }.bind(this)) } } else { return false; @@ -354,18 +378,14 @@ }.bind(this)); }, onFind: function () { - new Ajax() - .module(this.module) - .target(this.target) - .data(this.vm) - .find(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } else { - this.result = response.result; - this.vm.totalCount = Number(response.totalCount); - } - }.bind(this)) + new Ajax(this.module, this.target, "find", this.vm).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + this.result = response.result; + this.vm.totalCount = Number(response.totalCount); + } + }.bind(this)) }, onDelete: function (item) { this.$confirm('将删除该项, 是否继续?', '提示', { @@ -373,18 +393,14 @@ cancelButtonText: '取消', type: 'warning' }).then(function () { - new Ajax() - .module(this.module) - .target(this.target) - .data({id: item.id}) - .delete(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } else { - nav.s("删除成功"); - this.onFind(); - } - }.bind(this)) + new Ajax(this.module, this.target, "delete", {id: item.id}).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + nav.s("删除成功"); + this.onFind(); + } + }.bind(this)) }.bind(this)).catch(function (action) { }); @@ -400,25 +416,21 @@ }).then(function () { for (var i = 0; i < this.select.length; i++) { (function (obj) { - new Ajax() - .module(this.module) - .target(this.target) - .data({id: obj.id}) - .delete(function (response) { - if (response.errors.length > 0) { - nav.e(response.errors[0].message); - } else { - for (var j = 0; j < this.select.length; j++) { - if (this.select[j].id === obj.id) { - this.select.splice(j, 1); - } - } - if (this.select.length === 0) { - nav.s("删除成功") - this.onFind(); - } + new Ajax(this.module, this.target, "delete", {id: obj.id}).post(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + for (var j = 0; j < this.select.length; j++) { + if (this.select[j].id === obj.id) { + this.select.splice(j, 1); } - }.bind(this)) + } + if (this.select.length === 0) { + nav.s("删除成功") + this.onFind(); + } + } + }.bind(this)) }.bind(this))(this.select[i]); } }.bind(this)).catch(function (action) {