/*
|
* @Author: lixiong
|
* @Date: 2019-08-20 09:41:24
|
* @Last Modified by: lixiong
|
* @Last Modified time: 2020-01-07 10:15:30
|
*/
|
import dayjs from 'dayjs'
|
import CommAxios from '@comprehensive/utils/commAxios'
|
// 贷后实例
|
let apiOrigin;
|
if (location.hostname === "scp.jycash.cn") {
|
apiOrigin = process.env.VUE_APP_XXXFINTECH_API_ORIGIN;
|
} else if (location.host === '10.10.16.114') {
|
apiOrigin = process.env.VUE_APP_HTTPS_API_ORIGIN;
|
} else if (location.host === 'localhost:8080') {
|
apiOrigin = 'http://localhost:8080'
|
} else {
|
apiOrigin = process.env.VUE_APP_API_ORIGIN;
|
}
|
const malAxios = new CommAxios(
|
{},
|
{
|
apiOrigin: apiOrigin,
|
apiPath: "/rlc-mal/"
|
}
|
);
|
|
/**
|
* ApiModel 接口映射模型
|
*/
|
|
export default class ApiModel {
|
/**
|
* constructor 初始化
|
* @param {Object} options 配置项 @example
|
* {
|
* api: '接口地址', // required
|
* formList: [ // 表单映射信息(通常用来渲染表单)
|
* {
|
* type: 'input',label: '客户名称:',value: '',name: 'customerName'
|
* }
|
* ],
|
* responseMap: [ // 接口响应映射信息(通常用来表格或类似表单展示的数据)
|
* {
|
* label: '车位首付(元)',field: 'downpayparkingamount',isMoney: true,isDesc: true
|
* }
|
* ],
|
* computedResponse: item => {
|
* return {...item}
|
* },
|
* computedItem: item => {
|
* return {...item}
|
* },
|
*
|
*/
|
|
constructor(options = {}) {
|
const { axiosConf = {}, ...other } = options
|
// 默认日期格式
|
this.dateFormate = 'YYYY/MM/DD'
|
// this.ruleReduce
|
this.ruleAction = (rule, message) => {
|
if (typeof rule === 'string') {
|
if (rule === 'required') {
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value
|
if (value.toString().trim() === '') {
|
callback(new Error(message || '该字段必填'))
|
} else {
|
callback()
|
}
|
}
|
}
|
if (rule === 'phone') {
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value.trim()
|
if (!/1\d{10}/.test(value)) {
|
callback(new Error('手机号格式有误'))
|
} else {
|
callback()
|
}
|
}
|
}
|
if (rule === 'number') {
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value.trim()
|
if (isNaN(value)) {
|
callback(new Error('必须输入数字'))
|
} else {
|
callback()
|
}
|
}
|
}
|
}
|
|
if (typeof rule === 'object') {
|
let { required, pattern, maxLength, minLength } = rule
|
if (required) {
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value.trim()
|
if (value === '') {
|
callback(new Error('该字段必填'))
|
} else {
|
callback()
|
}
|
}
|
}
|
|
if (maxLength) {
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value.trim()
|
if (value.length > maxLength) {
|
callback(new Error(`最多输入${maxLength}个字符`))
|
} else {
|
callback()
|
}
|
}
|
}
|
|
if (minLength) {
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value.trim()
|
if (value.length < minLength) {
|
callback(new Error(`最少输入${minLength}个字符`))
|
} else {
|
callback()
|
}
|
}
|
}
|
|
if (pattern) {
|
if (pattern === 'string') {
|
pattern = new RegExp(pattern)
|
}
|
|
return (rule, value, callback) => {
|
value = Array.isArray(value) ? value.join('') : value.trim()
|
if (!pattern.test(value)) {
|
callback(new Error('输入格式有误'))
|
} else {
|
callback()
|
}
|
}
|
}
|
}
|
return false
|
}
|
|
this.commAxios = new CommAxios(axiosConf)
|
this.setOptions(other)
|
}
|
|
/**
|
* 获取表单信息
|
* @param {Object} initValues 初始值对象
|
*/
|
getFormList(initValues = {}, list) {
|
const { formList } = this
|
if (typeof list === 'undefined') {
|
list = [...formList]
|
}
|
const keys = Object.keys(initValues)
|
if (keys.length > 0) {
|
return list.reduce((pre, curr) => {
|
let { value, name, children } = curr
|
if (typeof value === 'undefined' && Array.isArray(children)) {
|
// has children
|
pre.push({
|
...curr,
|
children: this.getFormList(initValues, children)
|
})
|
} else {
|
value =
|
typeof initValues[name] === 'undefined' ? value : initValues[name]
|
pre.push({
|
...curr,
|
value
|
})
|
}
|
return pre
|
}, [])
|
}
|
return [...list]
|
}
|
|
// 获取表单数据值
|
getFormValues(list) {
|
const { formList } = this
|
if (typeof list === 'undefined') {
|
list = [...formList]
|
}
|
return list.reduce((pre, curr) => {
|
const { name, names, value, children } = curr
|
// 父级不包含value,则取子级
|
if (typeof value === 'undefined' && Array.isArray(children)) {
|
children.forEach(subItem => {
|
const { name: subName, value: subValue } = subItem
|
pre[subName] = this.formatInput(subValue, subName, list)
|
})
|
} else if (Array.isArray(names) && Array.isArray(value)) {
|
// 将数组值传送到names字段对应的key中
|
names.forEach((subName, index) => {
|
pre[subName] = this.formatInput(value[index] || '', subName, list)
|
})
|
} else {
|
pre[name] = this.formatInput(value, name, list)
|
}
|
return pre
|
}, {})
|
}
|
|
// 获取表单验证规则
|
getFormRules(list) {
|
const { formList } = this
|
if (typeof list === 'undefined') {
|
list = [...formList]
|
}
|
return list.reduce((pre, curr) => {
|
const { rules = [], name, children } = curr
|
if (Array.isArray(children)) {
|
pre = { ...pre, ...this.getFormRules(children) }
|
} else {
|
const ruleArray = rules.reduce((rulePre, ruleCurr) => {
|
if (this.getRule(ruleCurr)) {
|
rulePre.push(this.getRule(ruleCurr))
|
}
|
return rulePre
|
}, [])
|
if (ruleArray.length > 0) {
|
pre[name] = ruleArray
|
}
|
}
|
return pre
|
}, {})
|
}
|
|
getRule(rule) {
|
if (typeof rule === 'string') {
|
// rule: required phone number
|
const temp = rule === 'required' ? { required: true } : {}
|
return { validator: this.ruleAction(rule), ...temp, trigger: 'blur' }
|
}
|
if (typeof rule === 'object') {
|
const { validator, trigger = 'blur', message } = rule
|
if (typeof validator === 'function') {
|
return { trigger, ...rule }
|
} else {
|
return { trigger, validator: this.ruleAction(rule, message) }
|
}
|
}
|
return false
|
}
|
|
// 统一输出数据格式,如日期格式化
|
formatInput(val, name, list) {
|
const { dateFormate } = this
|
if (typeof this.computedValue === 'function') {
|
val = this.computedValue(val, name, list)
|
}
|
if (Array.isArray(val) && val.length > 0) {
|
return val.reduce((pre, curr) => {
|
pre.push(this.formatInput(curr, name, list))
|
return pre
|
}, [])
|
}
|
if (val instanceof Date) {
|
val = dayjs(val).format(dateFormate)
|
}
|
|
return val
|
}
|
|
/**
|
* 获取表格信息
|
*/
|
getTableList() {
|
const { tableList } = this
|
return [...tableList]
|
}
|
|
/**
|
* post 请求处理
|
* @param {String} api 接口地址
|
* @param {Object|String} params 请求参数
|
*/
|
post(params = {}, conf = {}) {
|
return this.toRequest(params, { method: 'post', ...conf })
|
}
|
|
/**
|
* get 请求处理
|
* @param {String} api 接口地址
|
* @param {Object|String} params 请求参数
|
*/
|
get(params = {}, conf = {}) {
|
return this.toRequest(params, { method: 'get', ...conf })
|
}
|
|
// 遍历options内容到实例
|
setOptions(options) {
|
Object.keys(options).forEach(key => {
|
this[key] = options[key]
|
})
|
}
|
|
// 请求统一处理
|
toRequest(params = {}, conf = {}) {
|
const { api } = this
|
return new Promise((resolve, reject) => {
|
if (api === 'server/claimRefundInfo'
|
|| api === 'openserver/queryCodeValueList'
|
|| api === 'server/claimRefundApplyInfo'
|
|| api === 'server/claimRefundApply'
|
|| api === 'server/queryCodeObject'
|
|| api === 'server/claimRefundEditCommit') {
|
malAxios
|
.request(api, params, conf)
|
.then(res => {
|
resolve(this.formateResponse(res))
|
})
|
.catch(reson => {
|
reject(reson)
|
})
|
return
|
}
|
this.commAxios
|
.request(api, params, conf)
|
.then(res => {
|
resolve(this.formateResponse(res))
|
})
|
.catch(reson => {
|
reject(reson)
|
})
|
})
|
}
|
|
// 输出数据统一处理(这里为将列表项转换为list字段值)
|
formateResponse(res) {
|
let { result = {}, ...other } = res
|
// let list = {}
|
if (Array.isArray(result)) {
|
result = { list: this.computedList(result) }
|
} else {
|
const { records, ...other } = result
|
if (Array.isArray(records)) {
|
result = { list: this.computedList(records), ...other }
|
}
|
}
|
const temp = {
|
...other,
|
...result
|
}
|
if (typeof this.computedResponse === 'function') {
|
// 响应数据的进一步处理
|
return this.computedResponse(temp)
|
}
|
return temp
|
}
|
|
// 列表数据处理
|
computedList(list = []) {
|
const { computedItem } = this
|
if (typeof computedItem === 'function') {
|
return list.reduce((pre, curr) => {
|
pre.push(computedItem(curr))
|
return pre
|
}, [])
|
}
|
return [...list]
|
}
|
}
|