230 lines
6.6 KiB
JavaScript
230 lines
6.6 KiB
JavaScript
import axios from 'axios'
|
|
// import { getToken } from './auth';
|
|
import Vue from 'vue'
|
|
import store from '@/store'
|
|
// import { Encrypt, Decrypt} from '@/utils/secret.js'
|
|
import { encryptStr } from 'pwd-restclient'
|
|
|
|
const service = axios.create({
|
|
baseURL: process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API : '',
|
|
timeout: 50000 // request timeout
|
|
})
|
|
// service.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
|
// service.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
// request拦截器
|
|
service.interceptors.request.use(config => {
|
|
return config
|
|
}, error => {
|
|
console.log(error)
|
|
Promise.reject(error)
|
|
})
|
|
service.interceptors.response.use(
|
|
response => {
|
|
const res = response.data
|
|
if (res.code == '-1') {
|
|
const toast = Toast.$create({
|
|
txt: res.message,
|
|
type: 'error'
|
|
})
|
|
toast.show()
|
|
setTimeout(() => {
|
|
toast.hide()
|
|
}, 3000)
|
|
}
|
|
return res
|
|
},
|
|
error => {
|
|
const toast = Toast.$create({
|
|
txt: error.message || '系统接口请求异常',
|
|
type: 'error'
|
|
})
|
|
toast.show()
|
|
setTimeout(() => {
|
|
toast.hide()
|
|
}, 3000)
|
|
// alert(result.message)
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
const keyValue = (params) => {
|
|
var str = ''
|
|
for (const key in params) {
|
|
if (Object.hasOwnProperty.call(params, key)) {
|
|
if (typeof params[key] === 'string') {
|
|
str += `"${key}":"${params[key]}",`
|
|
} else if (typeof params[key] === 'number') {
|
|
str += `"${key}":${params[key]},`
|
|
} else if (typeof params[key] === 'object') {
|
|
str += `"${key}":${JSON.stringify(params[key])},`
|
|
}
|
|
}
|
|
}
|
|
return str.substring(0, str.length - 1)
|
|
}
|
|
export function callApi(funName, params) {
|
|
// debugger
|
|
let userInfo = store.state.user.userInfo || {}
|
|
|
|
if(params){
|
|
params['partyActType']=store.state.user.partyActType || ''
|
|
params['erpId']=userInfo.sapUserId || null
|
|
}else{
|
|
params={
|
|
partyActType:store.state.user.partyActType || '',
|
|
erpId:userInfo.sapUserId || ''
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (sessionStorage.getItem('USER_INFO')) {
|
|
// userInfo = JSON.parse(sessionStorage.getItem('USER_INFO'))
|
|
// } else {
|
|
// userInfo = store.getters['d2admin/user/userInfo']
|
|
// }
|
|
// console.log('==>请求:', userInfo)
|
|
const requsetParams = {
|
|
...params || params,
|
|
fun_code: funName
|
|
// userId:userInfo.id
|
|
// habitId: userInfo.appuserid || '',
|
|
// habitName: userInfo.username || ''
|
|
}
|
|
|
|
const requsetParams1 = {
|
|
...requsetParams
|
|
}
|
|
if (process.env.NODE_ENV === 'production') {
|
|
// to do
|
|
// test cordova ready
|
|
// if (process.env.VUE_APP_TEST === '1') {
|
|
// return service({
|
|
// url: 'https://mam1.ft-power.com.cn:10030/xtkb-web/djAction.do',
|
|
// method: 'post',
|
|
// data: requsetParams1
|
|
// })
|
|
// }
|
|
return new Promise((resolve, reject) => {
|
|
function success(result) {
|
|
if(typeof result !== 'string' && result.code){
|
|
resolve(result)
|
|
}
|
|
else{
|
|
reject(result)
|
|
}
|
|
}
|
|
let appCode = 'JSINTXTKB',
|
|
params;
|
|
if(process.env.VUE_APP_ENV === '0'){ //走测试, 电科院加密
|
|
params = {"data":encryptStr(JSON.stringify(requsetParams1))}
|
|
console.log('加密前参数:', requsetParams1)
|
|
}else{
|
|
params = {"data":Encrypt(JSON.stringify(requsetParams1))}
|
|
}
|
|
// mam.navigator.ajax.post(appCode, params, '/xtkb-web/djAction.do', success)
|
|
try{
|
|
mam.navigator.ajax.postJSON(appCode, params, '/xtkb-web/djAction.do', success)
|
|
}catch (e){
|
|
reject(e)
|
|
}
|
|
})
|
|
} else {
|
|
keyValue(requsetParams1)
|
|
// Encrypt()
|
|
const str = '{' + keyValue(requsetParams1) + '}'
|
|
// const str = 'data='+Encrypt(str)
|
|
// 加密
|
|
// const a = process.env.VUE_APP_ISENCRYPTION === 'yes' ? Encrypt(str) : str
|
|
//let a = "data="+Encrypt(str)
|
|
// 不加密
|
|
// let a = "data="+str
|
|
return service({
|
|
url: 'xtkb-web/djAction.do',
|
|
method: 'post',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
},
|
|
data: 'data=' + str
|
|
// data: Encrypt(str)
|
|
})
|
|
}
|
|
}
|
|
|
|
export function fileDown (downLoadUrl, fileName, id,data) {
|
|
// 测试app
|
|
|
|
//正式app
|
|
if (process.env.NODE_ENV === 'production') {
|
|
let url = `/xtkb-web/file/fileDownload?id=${downLoadUrl}`
|
|
mam.navigator.appfilemanage.openFile(url, fileName);
|
|
}else{
|
|
|
|
}
|
|
}
|
|
// function diyDownload()
|
|
// {
|
|
|
|
// var filename = 'xtkb.doc';
|
|
// var fileCode = '1_diyFile';
|
|
|
|
// var param={fileName:filename};
|
|
|
|
// var downLoadUrl = 'https://mam.zeus2.top:10030/xtkb-web/file/fileDownload?path=/domain/upload/xtkb.doc'
|
|
|
|
|
|
// var saveUrl = '/diy';
|
|
// var fileName='local_'+filename;
|
|
|
|
// navigator.appfilemanage.fileDownload2(fileDownloadCallback,'JSINTXTKB','',downLoadUrl,saveUrl,fileName,fileCode,param);
|
|
|
|
// }
|
|
// function fileDownloadCallback(obj) {
|
|
// console.log("fileDownloadCallback 下载文件 回调 " + JSON.stringify(obj));
|
|
// }
|
|
|
|
/**
|
|
* 通过 POST 接口下载图片并返回 blob URL
|
|
* @param {string} attach_id 文件ID
|
|
* @returns {Promise<string>} blob URL
|
|
*/
|
|
export async function getImageBlobUrl(attach_id) {
|
|
try {
|
|
const { downloadFile } = await import('@/api/api')
|
|
const blob = await downloadFile(attach_id)
|
|
if (blob instanceof Blob && blob.size > 0) {
|
|
const type = blob.type && blob.type.startsWith('image/') ? blob.type : 'image/jpeg'
|
|
const imgBlob = new Blob([blob], { type })
|
|
const url = URL.createObjectURL(imgBlob)
|
|
console.log('getImageBlobUrl ok: id=', attach_id, 'type=', blob.type, 'size=', blob.size, 'url=', url)
|
|
return url
|
|
}
|
|
console.error('getImageBlobUrl 无效响应: id=', attach_id, 'isBlob=', blob instanceof Blob, 'size=', blob && blob.size, 'type=', blob && blob.type)
|
|
return ''
|
|
} catch (e) {
|
|
console.error('getImageBlobUrl 请求失败: id=', attach_id, e)
|
|
return ''
|
|
}
|
|
}
|
|
/**
|
|
* 通过 POST 接口下载文件并触发浏览器保存
|
|
* 注意:后端返回的是文件流(Blob),浏览器不会自动下载,需手动触发
|
|
* @param {string} attachId 文件ID
|
|
* @param {string} [fileName] 保存的文件名(带后缀),缺省为 'download'
|
|
*/
|
|
export async function downloadFileById(attachId, fileName) {
|
|
const { downloadFile } = await import('@/api/api')
|
|
const blob = await downloadFile(attachId)
|
|
if (!(blob instanceof Blob)) return
|
|
const url = URL.createObjectURL(blob)
|
|
const a = document.createElement('a')
|
|
a.href = url
|
|
a.download = fileName || 'download'
|
|
document.body.appendChild(a)
|
|
a.click()
|
|
a.remove()
|
|
URL.revokeObjectURL(url)
|
|
}
|
|
|
|
export default service
|