|
|
|
@ -1,106 +1,101 @@
|
|
|
|
|
import axios from 'axios'
|
|
|
|
|
//创建axios实例
|
|
|
|
|
const instance = axios.create({
|
|
|
|
|
method: 'post',
|
|
|
|
|
timeout: 30000,
|
|
|
|
|
});
|
|
|
|
|
// 创建axios实例
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
|
|
|
|
|
withCredentials: true, // send cookies when cross-domain requests
|
|
|
|
|
method: 'post', // request method
|
|
|
|
|
timeout: 5000 // request timeout
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 添加请求拦截器
|
|
|
|
|
instance.interceptors.request.use(function (config) {
|
|
|
|
|
service.interceptors.request.use(config => {
|
|
|
|
|
// 在发送请求之前做些什么
|
|
|
|
|
if (config.url == '/upload') {
|
|
|
|
|
|
|
|
|
|
if (config.url === '/upload') {
|
|
|
|
|
console.log()
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
console.log()
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}, function (error) {
|
|
|
|
|
return config
|
|
|
|
|
}, error => {
|
|
|
|
|
// 对请求错误做些什么
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
});
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 添加响应拦截器
|
|
|
|
|
instance.interceptors.response.use(function (response) {
|
|
|
|
|
service.interceptors.response.use(response => {
|
|
|
|
|
// 对响应数据做点什么
|
|
|
|
|
try {//确保服务器正确返回Json
|
|
|
|
|
if(response.data.errors.length > 0){
|
|
|
|
|
console.error(response.data.errors)
|
|
|
|
|
}
|
|
|
|
|
}catch (e){
|
|
|
|
|
response.data = {errors: [{message: '服务器错误'}]};
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}, function (error) {
|
|
|
|
|
// 对响应错误做点什么,保准化返回结果
|
|
|
|
|
const rsp = {errors: []};
|
|
|
|
|
return response
|
|
|
|
|
}, error => {
|
|
|
|
|
// 对响应错误做点什么
|
|
|
|
|
const rsp = { errors: [] }
|
|
|
|
|
if (!error.response) {
|
|
|
|
|
rsp.errors.push({message: error.message});
|
|
|
|
|
rsp.errors.push({ message: error.message })
|
|
|
|
|
} else {
|
|
|
|
|
switch (error.response.status) {
|
|
|
|
|
case 401:
|
|
|
|
|
rsp.errors.push({message: "未授权,请登录(401)"});
|
|
|
|
|
rsp.errors.push({ message: '未授权,请登录(401)' })
|
|
|
|
|
break
|
|
|
|
|
case 403:
|
|
|
|
|
rsp.errors.push({message: "拒绝访问(403)"});
|
|
|
|
|
rsp.errors.push({ message: '拒绝访问(403)' })
|
|
|
|
|
break
|
|
|
|
|
case 404:
|
|
|
|
|
rsp.errors.push({message: "请求地址错误(404)"});
|
|
|
|
|
rsp.errors.push({ message: '请求地址错误(404)' })
|
|
|
|
|
break
|
|
|
|
|
case 408:
|
|
|
|
|
rsp.errors.push({message: "请求超时(408)"});
|
|
|
|
|
rsp.errors.push({ message: '请求超时(408)' })
|
|
|
|
|
break
|
|
|
|
|
case 500:
|
|
|
|
|
rsp.errors.push({message: "服务器内部错误(500)"});
|
|
|
|
|
rsp.errors.push({ message: '服务器内部错误(500)' })
|
|
|
|
|
break
|
|
|
|
|
case 501:
|
|
|
|
|
rsp.errors.push({message: "服务未实现(501)"});
|
|
|
|
|
rsp.errors.push({ message: '服务未实现(501)' })
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
rsp.errors.push({message: "请求错误(" + error.response.status + ")"});
|
|
|
|
|
rsp.errors.push({ message: '请求错误(' + error.response.status + ')' })
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(rsp);
|
|
|
|
|
});
|
|
|
|
|
let jsonRequest = function (config) {
|
|
|
|
|
return instance.request({
|
|
|
|
|
return Promise.reject(rsp)
|
|
|
|
|
})
|
|
|
|
|
export function jsonRequest(config) {
|
|
|
|
|
return service.request({
|
|
|
|
|
params: {
|
|
|
|
|
method: config.method
|
|
|
|
|
},
|
|
|
|
|
url: '${contextPath?default("")}/ajax',
|
|
|
|
|
headers: {'Content-Type': 'text/plain'},
|
|
|
|
|
url: '/ajax',
|
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
|
data: config.data
|
|
|
|
|
}).then(function (response) {
|
|
|
|
|
return Promise.resolve(response.data);
|
|
|
|
|
}, function (response) {
|
|
|
|
|
return Promise.resolve(response);
|
|
|
|
|
}).then(response => {
|
|
|
|
|
return Promise.resolve(response.data)
|
|
|
|
|
}, response => {
|
|
|
|
|
return Promise.resolve(response)
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
let fileRequest = function (config) {
|
|
|
|
|
return instance.request({
|
|
|
|
|
url: '${contextPath?default("")}/upload',
|
|
|
|
|
}
|
|
|
|
|
export function fileRequest(config) {
|
|
|
|
|
return service.request({
|
|
|
|
|
url: '/upload',
|
|
|
|
|
data: config.data,
|
|
|
|
|
headers: {'Content-Type': 'multipart/form-data'},
|
|
|
|
|
onUploadProgress: function (progressEvent) {
|
|
|
|
|
var complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
|
|
|
|
|
nav.tip.show("上传中(" + complete + ")")
|
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
|
|
onUploadProgress: progressEvent => {
|
|
|
|
|
console.log((progressEvent.loaded / progressEvent.total * 100 | 0) + '%')
|
|
|
|
|
}
|
|
|
|
|
}).then(function (response) {
|
|
|
|
|
return Promise.resolve(response.data);
|
|
|
|
|
}, function (response) {
|
|
|
|
|
return Promise.resolve(response);
|
|
|
|
|
}).then(response => {
|
|
|
|
|
return Promise.resolve(response.data)
|
|
|
|
|
}, response => {
|
|
|
|
|
return Promise.resolve(response)
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
let ajax = {
|
|
|
|
|
example: function (data) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ajax = {
|
|
|
|
|
example: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method: "ajax.example.example",
|
|
|
|
|
method: 'ajax.example.example',
|
|
|
|
|
data: data
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
fileUpload: function (file) {
|
|
|
|
|
var fd = new FormData();
|
|
|
|
|
fd.append("file", file);
|
|
|
|
|
fileUpload: file => {
|
|
|
|
|
const fd = new FormData()
|
|
|
|
|
fd.append('file', file)
|
|
|
|
|
return fileRequest({
|
|
|
|
|
data: fd
|
|
|
|
|
})
|
|
|
|
@ -108,57 +103,57 @@ let ajax = {
|
|
|
|
|
<#list modules as db>
|
|
|
|
|
<#list db.tables as table>
|
|
|
|
|
<#if table.getCreate()>
|
|
|
|
|
${table.getFName()}Create: function (data) {
|
|
|
|
|
${table.getFName()}Create: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.create",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.create',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
</#if>
|
|
|
|
|
<#if table.getDelete()>
|
|
|
|
|
${table.getFName()}Delete: function (data) {
|
|
|
|
|
${table.getFName()}Delete: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.delete",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.delete',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
</#if>
|
|
|
|
|
<#if table.getUpdate()>
|
|
|
|
|
${table.getFName()}Update: function (data) {
|
|
|
|
|
${table.getFName()}Update: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.update",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.update',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
</#if>
|
|
|
|
|
<#if table.getFind()>
|
|
|
|
|
${table.getFName()}Find: function (data) {
|
|
|
|
|
${table.getFName()}Find: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.find",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.find',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
</#if>
|
|
|
|
|
<#if table.getGet()>
|
|
|
|
|
${table.getFName()}Get: function(data) {
|
|
|
|
|
${table.getFName()}Get: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.get",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.get',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
</#if>
|
|
|
|
|
<#if table.getSearch()>
|
|
|
|
|
${table.getFName()}Search: function (data) {
|
|
|
|
|
${table.getFName()}Search: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.search",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.search',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
</#if>
|
|
|
|
|
<#if table.getGetAll()>
|
|
|
|
|
${table.getFName()}GetAll: function (data) {
|
|
|
|
|
${table.getFName()}GetAll: data => {
|
|
|
|
|
return jsonRequest({
|
|
|
|
|
method:"ajax.${db.moduleName}.${table.getLName()}.get.all",
|
|
|
|
|
method:'ajax.${db.moduleName}.${table.getLName()}.get.all',
|
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|