Commit c91b002e authored by leon's avatar leon

feat: add dynamic params

parent cd51368d
...@@ -30,6 +30,7 @@ export default { ...@@ -30,6 +30,7 @@ export default {
const that = this const that = this
let { dataUrl, dataMethod, dataFormatter, dataProcessing } = { ...value } let { dataUrl, dataMethod, dataFormatter, dataProcessing } = { ...value }
dataUrl = dataUrl.replace(/^(\/(dashboardCharts|dashboardAPI))?/, '') dataUrl = dataUrl.replace(/^(\/(dashboardCharts|dashboardAPI))?/, '')
dataFormatter = that.$u.common.filterRequestParams({...dataFormatter})
let res = await that.$u.api[`${dataMethod.toLowerCase()}Http`](dataUrl, dataFormatter, { let res = await that.$u.api[`${dataMethod.toLowerCase()}Http`](dataUrl, dataFormatter, {
custom: { loading: false } custom: { loading: false }
}) })
......
...@@ -236,7 +236,8 @@ ...@@ -236,7 +236,8 @@
async getGlobalData(info) { async getGlobalData(info) {
const that = this const that = this
const dataUrl = info.dataUrl.replace(/^(\/(dashboardCharts|dashboardAPI))?/, '') const dataUrl = info.dataUrl.replace(/^(\/(dashboardCharts|dashboardAPI))?/, '')
let res = await that.$u.api[`${info.dataMethod.toLowerCase()}Http`](dataUrl, info.dataFormatter, { const dataFormatter = this.$u.common.filterRequestParams({...info.dataFormatter})
let res = await that.$u.api[`${info.dataMethod.toLowerCase()}Http`](dataUrl, dataFormatter, {
custom: { loading: false } custom: { loading: false }
}) })
that.$u.vuex('vuex_globalData', res.data) that.$u.vuex('vuex_globalData', res.data)
......
...@@ -274,7 +274,8 @@ ...@@ -274,7 +274,8 @@
async getGlobalData(info) { async getGlobalData(info) {
const that = this const that = this
const dataUrl = info.dataUrl.replace(/^(\/(dashboardCharts|dashboardAPI))?/, '') const dataUrl = info.dataUrl.replace(/^(\/(dashboardCharts|dashboardAPI))?/, '')
let res = await that.$u.api[`${info.dataMethod.toLowerCase()}Http`](dataUrl, info.dataFormatter, { const dataFormatter = this.$u.common.filterRequestParams({...info.dataFormatter})
let res = await that.$u.api[`${info.dataMethod.toLowerCase()}Http`](dataUrl, dataFormatter, {
custom: { loading: false } custom: { loading: false }
}) })
that.$u.vuex('vuex_globalData', res.data) that.$u.vuex('vuex_globalData', res.data)
......
import { Function } from '../static/plugin/parse/parse.js' import {
Function
} from '../static/plugin/common/index.js'
const install = (Vue, vm) => { const install = (Vue, vm) => {
/** /**
* 数据转换 * 数据转换
*/ */
const converFunction = (func, data) => { const converFunction = (func, data) => {
if(!func) return if (!func) return
return new Function('"use strict";return (' + func + ')')()(data); return new Function('"use strict";return (' + func + ')')()(data);
} }
...@@ -103,23 +105,87 @@ const install = (Vue, vm) => { ...@@ -103,23 +105,87 @@ const install = (Vue, vm) => {
*/ */
const getQueryFromString = (string) => { const getQueryFromString = (string) => {
const num = string.indexOf("?") const num = string.indexOf("?")
const qs = string.substr(num+1) // 获取url中"?"符后的字串 const qs = string.substr(num + 1) // 获取url中"?"符后的字串
let args = {} // 保存参数数据的对象 let args = {} // 保存参数数据的对象
const items = qs.length ? qs.split("&") : [] // 取得每一个参数项, const items = qs.length ? qs.split("&") : [] // 取得每一个参数项,
let item = null let item = null
const len = items.length; const len = items.length;
for(var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
item = items[i].split("="); item = items[i].split("=");
var name = decodeURIComponent(item[0]), var name = decodeURIComponent(item[0]),
value = decodeURIComponent(item[1]); value = decodeURIComponent(item[1]);
if(name) { if (name) {
args[name] = value; args[name] = value;
} }
} }
return args; return args;
} }
const filterRequestParams = (params) => {
let paramStr = JSON.stringify(params)
if (paramStr.includes('@year')) {
paramStr = paramStr.replace(/@year/g, yearly())
}
if (paramStr.includes('@month')) {
paramStr = paramStr.replace(/@month/g, monthly())
}
if (paramStr.includes('@day')) {
paramStr = paramStr.replace(/@day/g, day())
}
if (paramStr.includes('@daily')) {
const d = daily()
paramStr = paramStr.replace(/@daily/g, daily())
}
if (paramStr.includes('@yesterday')) {
paramStr = paramStr.replace(/@yesterday/g, yesterday())
}
return JSON.parse(paramStr)
}
const yearly = () => {
const date = new Date();
const year = date.getFullYear();
return year
}
const monthly = () => {
const date = new Date();
const month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
return month
}
const day = () => {
const date = new Date();
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return day
}
const daily = () => {
const date = new Date();
const year = date.getFullYear();
// 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一 如 09:11:05
const month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
// 拼接
return year + "-" + month + "-" + day;
}
const yesterday = () => {
const date = new Date()
const timestamp = date.getTime() - 1000 * 24 * 60 * 60
date.setTime(timestamp)
console.log(date)
const year = date.getFullYear();
// 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一 如 09:11:05
const month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
// 拼接
return year + "-" + month + "-" + day;
}
vm.$u.common = { vm.$u.common = {
converFunction, converFunction,
fillDigit, fillDigit,
...@@ -128,7 +194,8 @@ const install = (Vue, vm) => { ...@@ -128,7 +194,8 @@ const install = (Vue, vm) => {
pxToRpx, pxToRpx,
rpxToPx, rpxToPx,
chunk, chunk,
getQueryFromString getQueryFromString,
filterRequestParams
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment