fix: 新增mock数据请求功能

This commit is contained in:
mtruning 2022-03-21 23:03:10 +08:00
parent d9ee41c892
commit bf020e3d3e
7 changed files with 48 additions and 32 deletions

View File

@ -26,11 +26,12 @@ axiosInstance.interceptors.response.use(
if (code === ResultEnum.DATA_SUCCESS) return Promise.resolve(res.data) if (code === ResultEnum.DATA_SUCCESS) return Promise.resolve(res.data)
// 重定向 // 重定向
if (ErrorPageNameMap.get(code)) redirectErrorPage(code) if (ErrorPageNameMap.get(code)) redirectErrorPage(code)
return Promise.reject(res.data) return Promise.resolve(res.data)
}, },
(err: AxiosResponse) => { (err: AxiosResponse) => {
const { code } = err.data as { code: number } const { code } = err.data as { code: number }
if (ErrorPageNameMap.get(code)) redirectErrorPage(code) if (ErrorPageNameMap.get(code)) redirectErrorPage(code)
window['$message'].error('接口异常,请检查!')
Promise.reject(err) Promise.reject(err)
} }
) )

View File

@ -1,11 +1,10 @@
import axiosInstance from './axios' import axiosInstance from './axios'
import { RequestHttpEnum, ContentTypeEnum } from '@/enums/httpEnum' import { RequestHttpEnum, ContentTypeEnum } from '@/enums/httpEnum'
export const get = (url: string, params: object) => { export const get = (url: string) => {
return axiosInstance({ return axiosInstance({
url: url, url: url,
method: RequestHttpEnum.GET, method: RequestHttpEnum.GET,
params
}) })
} }

View File

@ -4,37 +4,37 @@ export default {
status: 200, status: 200,
msg: "请求成功", msg: "请求成功",
data: { data: {
dimensions: ["product", "data1", "data2"], dimensions: ["product", "dataOne", "dataTwo"],
source: [ source: [
{ {
'product': '@name', 'product': '@name',
'data1|100-900': 3, 'dataOne|100-900': 3,
'data2|100-900': 3, 'dataTwo|100-900': 3,
}, },
{ {
'product': '@name', 'product': '@name',
'data1|100-900': 3, 'dataOne|100-900': 3,
'data2|100-900': 3, 'dataTwo|100-900': 3,
}, },
{ {
'product': '@name', 'product': '@name',
'data1|100-900': 3, 'dataOne1|100-900': 3,
'data2|100-900': 3, 'dataTwo|100-900': 3,
}, },
{ {
'product': '@name', 'product': '@name',
'data1|100-900': 3, 'dataOne|100-900': 3,
'data2|100-900': 3, 'dataTwo|100-900': 3,
}, },
{ {
'product': '@name', 'product': '@name',
'data1|100-900': 3, 'dataOne|100-900': 3,
'data2|100-900': 3, 'dataTwo|100-900': 3,
}, },
{ {
'product': '@name', 'product': '@name',
'data1|100-900': 3, 'dataOne|100-900': 3,
'data2|100-900': 3, 'dataTwo|100-900': 3,
}, },
] ]
} }

View File

@ -23,6 +23,7 @@ export class publicConfig implements PublicConfigType {
public data = { ...requestConfig } public data = { ...requestConfig }
// 数据获取 // 数据获取
public requestData = [] public requestData = []
// 设置坐标 // 设置坐标
public setPosition(x: number, y: number): void { public setPosition(x: number, y: number): void {
this.attr.x = x this.attr.x = x

View File

@ -79,7 +79,7 @@
} }
}, },
"dataset": { "dataset": {
"dimensions": [], "dimensions": [""],
"source": [] "source": [{}]
} }
} }

View File

@ -42,15 +42,17 @@
</n-button> </n-button>
</n-space> </n-space>
</setting-item-box> </setting-item-box>
<go-skeleton :load="loading" :repeat="3"></go-skeleton>
<chart-data-matching-and-show <chart-data-matching-and-show
v-show="showMatching" v-show="showMatching && !loading"
:targetData="targetData" :targetData="targetData"
:ajax="true"
></chart-data-matching-and-show> ></chart-data-matching-and-show>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, ref, toRefs } from 'vue' import { PropType, ref } from 'vue'
import { icon } from '@/plugins' import { icon } from '@/plugins'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { SettingItemBox } from '@/components/ChartItemSetting/index' import { SettingItemBox } from '@/components/ChartItemSetting/index'
@ -72,9 +74,8 @@ const props = defineProps({
// //
const ISDEV = import.meta.env.DEV === true const ISDEV = import.meta.env.DEV === true
// //
const loading = ref(false)
const showMatching = ref(false) const showMatching = ref(false)
//
const { requestUrl, requestHttpType } = toRefs(props.targetData.data)
// //
const selectOptions: SelectHttpType[] = [ const selectOptions: SelectHttpType[] = [
@ -90,16 +91,24 @@ const selectOptions: SelectHttpType[] = [
// //
const sendHandle = async () => { const sendHandle = async () => {
if(!requestUrl || !requestUrl.value) { loading.value = true
window['$message'].warn('请求参数不正确,请检查!') const { requestUrl, requestHttpType } = props.targetData.data
if(!requestUrl) {
window['$message'].warning('请求参数不正确,请检查!')
return return
} }
const res = await http(requestHttpType.value)(requestUrl.value as string, {}) const res = await http(requestHttpType)((requestUrl as string).trim(), {})
console.log(res) loading.value = false
if(res.status === 200) {
// @ts-ignore
props.targetData.option.dataset = res.data
showMatching.value = true
return
}
window['$message'].warning('数据异常,请检查接口数据!')
} }
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@include go('chart-configurations-data-ajax') { @include go('chart-configurations-data-ajax') {}
}
</style> </style>

View File

@ -36,7 +36,7 @@
@before-upload="beforeUpload" @before-upload="beforeUpload"
> >
<n-space> <n-space>
<n-button class="sourceBtn-item"> <n-button v-if="!ajax" class="sourceBtn-item">
<template #icon> <template #icon>
<n-icon> <n-icon>
<document-add-icon /> <document-add-icon />
@ -76,6 +76,11 @@ const props = defineProps({
targetData: { targetData: {
type: Object as PropType<CreateComponentType>, type: Object as PropType<CreateComponentType>,
required: true required: true
},
ajax: {
type: Boolean,
default: false,
required: true
} }
}) })
@ -103,10 +108,11 @@ watch(() => props.targetData?.option?.dataset, (newData) => {
// //
const matchingHandle = (mapping: string) => { const matchingHandle = (mapping: string) => {
let res = DataResultEnum.SUCCESS
for (let i = 0; i < source.value.length; i++) { for (let i = 0; i < source.value.length; i++) {
let res = DataResultEnum.FAILURE if (source.value[i][mapping] === undefined) {
if (source.value[i][mapping] !== undefined) { res = DataResultEnum.FAILURE
return DataResultEnum.SUCCESS break
} }
return res return res
} }