cttq-mc
Version:
cttqtest
415 lines (412 loc) • 12.5 kB
JavaScript
import {
Toast
} from 'vant'
import Vue from 'vue'
import axios from 'axios'
import Utils from './utils.js'
axios.defaults.baseURL = process.env.VUE_APP_baseUrl
// 添加请求拦截器
axios.interceptors.request.use(config => {
if (config.mappingUrl) {
config.baseURL = process.env["VUE_APP_MappingUrls_" + config.mappingUrl];
}
if (config.method === "post") {
// config.data = JSON.stringify(config.data);
}
return config;
});
// 添加响应拦截器
axios.interceptors.response.use(function (response, res) {
// console.log('您发起的请求 - 结果:', response, res)
return response
}, function (error) {
// console.log(`您发起的请求存在 - 异常!!!!!!!!!`)
// console.log('异常信息如下:', error)
return Promise.reject(error)
})
export default {
setTimeout() {
axios.defaults.timeout = 5000
},
setBaseURL(url) {
axios.defaults.baseURL = url
},
axios,
// 并发请求
all(param) {
return new Promise((resolve, reject) => {
if (param.loading && param.loading.show) {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: param.loading.title ? param.loading.title : '正在努力加载中...'
})
}
axios.all(param.gather)
.then(res => {
if (param.loading && param.loading.show) {
Toast.clear()
}
resolve(res)
})
.catch(err => {
if (param.loading && param.loading.show) {
Toast.clear()
}
resolve(err)
})
})
},
// get请求
get(param) {
const that = this
if (param.publicError == undefined) {
param.publicError = true
}
return new Promise((resolve, reject) => {
if (param.loading && param.loading.show) {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: param.loading.title ? param.loading.title : '正在努力加载中...'
})
}
axios.get(param.url, {
mappingUrl: param.mappingUrl,
headers: param.headers || '',
params: param.data || ''
}).then(res => {
that.requestThen(resolve, res, param)
// if (param.loading && param.loading.show) {
// Toast.clear()
// }
// var requestType = true
// if (res.data.msgCode && res.data.msgCode !== "100") {
// requestType = false
// }
// if (res.data.status && res.data.status !== "0") {
// requestType = false
// }
// if (!requestType){
// console.log(`您发起的 - `+param.prompt+` - 请求报错!!!!!!!!!`)
// console.log('详细报错信息如下:', res)
// Toast.clear()
// Vue.prototype.router.push({
// name: 'otherError',
// query: {
// dd_full_screen: true
// }
// })
// } else {
// if (param.prompt) {
// console.log('' + param.prompt + ' - 请求结果如下:', res.data)
// } else {
// console.log('您发起的接口请求参数中没有 prompt 因此提示为接口地址+结果展示,请在接口请求中添加prompt接口功能描述!!!')
// console.log('' + param.url + ' - 请求结果如下:', res.data)
// }
// resolve(res.data)
// }
}).catch(err => {
that.requestThen(resolve, err, param)
})
})
},
// post请求
post(param) {
const that = this
if (param.publicError == undefined) {
param.publicError = true
}
return new Promise((resolve, reject) => {
if (param.loading && param.loading.show) {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: param.loading.title ? param.loading.title : '正在努力加载中...'
})
}
axios({
method: 'post',
url: param.url,
data: param.data,
mappingUrl: param.mappingUrl,
headers: param.headers || ''
}).then(res => {
that.requestThen(resolve, res, param)
}).catch(err => {
that.requestThen(resolve, err, param)
})
})
},
requestThen(resolve, res, param) {
if (param.loading && param.loading.show) {
Toast.clear()
}
var requestType = true
var errorMessage = ''
if (param.publicError) {
if (res.data && res.data.msgCode && res.data.msgCode !== "100" && res.data.msgCode !== "116") {
requestType = false
errorMessage = `环境:` + process.env.VUE_APP_domain + `
接口地址:` + param.url + `
msgCode:` + res.data.msgCode + `
message:` + res.data.message + `
sysMessage:` + res.data.sysMessage + `
`
}
if (res.data && res.data.status && res.data.status !== "0") {
requestType = false
// errorMessage = `
// 环境:`+process.env.VUE_APP_domain+`
// 接口地址:`+param.url+`
// msgCode:`+res.data.msgCode+`
// message:`+res.data.message+`
// sysMessage:`+res.data.sysMessage+`
// `
}
}
if (!requestType) {
if (res.data.msgCode !== "116") {
if (param.loading.showError == true || param.loading.showError == undefined || param.loading.showError == null) {
Toast(res.data.message);
}
} else {
if (param.prompt) {
console.log(`您发起的 - ` + param.prompt + ` - 请求报错!!!!!!!!!`)
} else {
console.log(`您发起的 - ` + param.url + ` - 请求报错!!!!!!!!!`)
}
console.log('详细报错信息如下:', res)
// 关闭动画
Toast.clear()
// 发送群消息
if (process.env.VUE_APP_domain == "PRE" || process.env.VUE_APP_domain == "PRD") {
this.hairMessage(res, errorMessage)
}
// 打开报错页面
if (process.env.VUE_APP_domain != "DEV") {
Vue.prototype.router.push({
name: 'otherError',
})
}
}
} else {
if (param.prompt) {
console.log('' + param.prompt + ' - 请求结果如下:', res.data)
} else {
console.log('您发起的接口请求参数中没有 prompt 因此提示为接口地址+结果展示,请在接口请求中添加prompt接口功能描述!!!')
console.log('' + param.url + ' - 请求结果如下:', res.data)
}
if (param.loading && param.loading.show) {
Toast.clear()
}
resolve(res.data)
}
},
requestCatch(resolve, err, param) {
console.log(`您发起的 - ` + param.prompt + ` - 请求异常!!!!!!!!!`)
console.log('详细报错信息如下:', err)
if (param.loading && param.loading.show) {
Toast.clear()
}
// resolve(err)
},
// 发送钉钉群消息
hairMessage(res, errorMessage) {
this.post({
// 请求描述
prompt: '钉钉发送接口报错消息',
url: process.env.VUE_APP_serverUrl + '/dingtalk/ding/message/chat/send',
data: {
chatid: process.env.VUE_APP_flockID,
msgtype: "text",
msgTempObj: {
content: errorMessage
}
}
})
},
// 上传图片,File对象
uploadToOSS(imageFile) {
return new Promise((resolve, reject) => {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: '正在上传图片...'
});
let formData = new FormData();
formData.append('file', imageFile);
var baseUrl = "https://crm.cttq.com";
if (process.env.VUE_APP_domain == "DEV") {
baseUrl = "https://crmdev.cttq.com";
} else if (process.env.VUE_APP_domain == "QAS") {
baseUrl = "https://crmqas.cttq.com";
} else if (process.env.VUE_APP_domain == "PRE") {
baseUrl = "https://crmpre.cttq.com";
}
axios({
method: 'post',
url: baseUrl + '/file/upload',
data: formData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(res => {
Toast.clear()
resolve(res.data)
}).catch(err => {
Toast.clear()
reject(err)
});
})
},
// 批量上传图片,File对象列表
uploadToOSSList(imageFiles, showToast = true) {
if (showToast) {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: '正在上传图片...'
});
}
let requests = [];
for (const imageFile of imageFiles) {
let promise = new Promise((resolve, reject) => {
let formData = new FormData();
formData.append('file', imageFile);
var baseUrl = "https://crm.cttq.com";
if (process.env.VUE_APP_domain == "DEV") {
baseUrl = "https://crmdev.cttq.com";
} else if (process.env.VUE_APP_domain == "QAS") {
baseUrl = "https://crmqas.cttq.com";
} else if (process.env.VUE_APP_domain == "PRE") {
baseUrl = "https://crmpre.cttq.com";
}
axios({
method: 'post',
url: baseUrl + '/file/upload',
data: formData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(res => {
resolve(res.data)
}).catch(err => {
reject(err)
});
});
requests.push(promise);
}
return new Promise((resolve, reject) => {
Promise.all(requests).then((results) => {
Toast.clear()
resolve(results);
}).catch((err) => {
Toast.clear()
reject(err);
});
});
},
// 营业执照OCR识别(废弃)
businessLicenseToOCR(imageFile) {
return new Promise((resolve, reject) => {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: '正在识别中...'
});
let formData = new FormData();
formData.append('file', imageFile);
var baseUrl = "https://crm.cttq.com";
if (process.env.VUE_APP_domain == "DEV") {
baseUrl = "https://crmdev.cttq.com";
} else if (process.env.VUE_APP_domain == "QAS") {
baseUrl = "https://crmqas.cttq.com";
} else if (process.env.VUE_APP_domain == "PRE") {
baseUrl = "https://crmpre.cttq.com";
}
axios({
method: 'post',
url: baseUrl + '/crm/ocr/busLicenseOcr',
data: formData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(res => {
Toast.clear()
resolve(res.data)
}).catch(err => {
Toast.clear()
reject(err)
});
})
},
/* 身份证OCR识别
* @param {string/object} imageData 可以是base64数据、或者图片文件
* @param {boolean=true} isCardFront 是否身份证正面,默认正面
*/
identityCardToOCR(imageData, isCardFront) {
if (!imageData && !["string", "object"].includes(typeof imageData)) {
return;
}
var baseUrl = "https://crm.cttq.com";
if (process.env.VUE_APP_domain == "DEV") {
baseUrl = "https://crmdev.cttq.com";
} else if (process.env.VUE_APP_domain == "QAS") {
baseUrl = "https://crmqas.cttq.com";
} else if (process.env.VUE_APP_domain == "PRE") {
baseUrl = "https://crmpre.cttq.com";
}
return new Promise((resolve, reject) => {
Toast.loading({
duration: 0, // 持续展示 toast
forbidClick: true, // 禁用背景点击
loadingType: 'spinner',
message: '正在识别中...'
});
// ORC识别接口调用
let ajaxOCR = (image) => {
let id_card_side = "front";
if (isCardFront != null && isCardFront != undefined && typeof isCardFront == "boolean" && !isCardFront) {
id_card_side = "back";
}
let formData = {
"idCardSide":id_card_side,
"image":image,
};
axios({
method: "post",
url: baseUrl + "/crm/ocr/idOcr",
data: formData,
}).then(res => {
Toast.clear()
if (res.data && res.data.msgCode == "100" && res.data.responseData) {
resolve(JSON.parse(res.data.responseData));
}
}).catch(err => {
Toast.clear()
reject(err)
});
}
if (typeof imageData == "string") {
// Base64图片处理
imageData = imageData.replace(/^data:image\/\w+;base64,/, "");
ajaxOCR(encodeURIComponent(imageData));
} else {
// 文件处理
Utils.file.convertToBase64(imageData).then((base64) => {
base64 = base64.replace(/^data:image\/\w+;base64,/, "");
ajaxOCR(encodeURIComponent(base64));
}).catch((err) => {
reject(err)
});
}
});
},
}