fcc-core
Version:
Fusion communication center.
179 lines (176 loc) • 4.52 kB
JavaScript
import httpRequst from '../../ajaxRequest/instance'
import { Message } from 'element-ui'
// import DB from './log'
const userInfo = {}
// Object.defineProperty(userInfo, 'USER_CODE', {
// get () {
// return window.app.$store.getters.g_user.USER_CODE
// }
// })
/**
* agentgateway cms unionaccess通用接口请求
* @param {Object} params [description]
* @param {[type]} url [请求地址]
* @param {String} method [description]
*/
export const Request = (function () {
const heads = {}
// const db = new DB({
// databasename: 'xwlog',
// tablename: 'agentlog'
// })
return function (params = {}, url, method = 'post', apiType) {
return new Promise(function (resolve, reject) {
// 写接口调用日志
// db.addLog({
// action: `调用接口`,
// usercode: userInfo.USER_CODE,
// interface: url,
// state: '',
// params
// })
try {
httpRequst({
method: method,
url: url,
data: params,
apiType,
headers: heads,
json: true
}).then(function (response) {
const {
data,
request
} = response
// 写接口调用日志
if (url.indexOf('agentevent') < 0) {
// db.addLog({
// action: `调用接口成功`,
// usercode: userInfo.USER_CODE,
// interface: url,
// state: '',
// params: data.result
// })
}
// 登录成功 华为平台获取GUID fs平台获取token
const cookie = request.getResponseHeader('Set-GUID')
const token = request.getResponseHeader('token')
if (cookie) {
heads.Guid = cookie.split('=')[1]
}
if (token) {
heads.token = token
}
if (data.retcode !== '0') {
console.error(data.message)
reject(data.retcode)
return
}
resolve(data)
}).catch(e => {
// db.addLog({
// action: `接口调用失败`,
// usercode: userInfo.USER_CODE,
// interface: url,
// state: '',
// params: e
// })
console.error(e)
})
} catch (err) {
reject(err)
}
})
}
})()
/**
* ucc系统通用请求
* @param {Object} params [私有参数]
* @param {String} service [接口号]
* @param {String} method [请求方式默认为post]
* @return {[type]} [description]
*/
export function uccRequest (params = {}, service = '', method = 'post') {
if (!service) {
return false
}
params.service = service
return new Promise(function (resolve, reject) {
try {
httpRequst({
method: method,
apiType: 'UCC',
url: `/njdf_ajax?returnType=json&service=${service}&encoderType=none`,
json: true,
data: params
}).then(function (response) {
if (response.code === '0') {
return resolve(response.data)
}
}).catch(e => {
console.error(e)
Message({
type: 'error',
message: '404'
})
})
} catch (err) {
reject(err)
}
})
}
/**
* 对象属性混合(浅)
*/
export function extend (to, _from) {
return Object.assign(to, _from)
}
/**
* 函数防抖(简单版)
* @param fn
* @param interval
* @param first
* @returns {Function}
*/
export function debounce (fn, interval, first) {
let timer = null
return function (...params) {
const self = this
if (first === true) {
fn.apply(self, params)
first = false
return
}
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(function () {
return fn.apply(self, params)
}, interval)
}
}
/**
* 函数节流(简单版)
* @param fn
* @param interval
* @param first
* @returns {Function}
*/
export function throttle (fn, interval, first) {
let timer = null
return function (...params) {
const self = this
if (first) {
fn.apply(self, params)
first = false
return
}
if (timer) {
return
}
timer = setTimeout(function () {
fn.apply(self, params)
timer = null
}, interval)
}
}