Commit e42dc58e8396891980b4609cfe45e6dbc0951b2c

Authored by xp.Huang
2 parents 760dd9a8 365cee17

Merge branch 'ww' into 'main_dev'

fix(public interface): 修复动态请求无法选择参数

See merge request yunteng/thingskit-view!29
@@ -8,7 +8,7 @@ export enum ParamsType { @@ -8,7 +8,7 @@ export enum ParamsType {
8 OPTIONAL 8 OPTIONAL
9 } 9 }
10 10
11 -const getOriginUrl = (originUrl: string) => { 11 +export const getOriginUrl = (originUrl: string) => {
12 const locationUrl = 'localhost' 12 const locationUrl = 'localhost'
13 return originUrl === locationUrl ? location.origin : originUrl 13 return originUrl === locationUrl ? location.origin : originUrl
14 } 14 }
@@ -6,7 +6,7 @@ import { useChartEditStore } from "@/store/modules/chartEditStore/chartEditStore @@ -6,7 +6,7 @@ import { useChartEditStore } from "@/store/modules/chartEditStore/chartEditStore
6 import { getJwtToken } from "@/utils/external/auth"; 6 import { getJwtToken } from "@/utils/external/auth";
7 import { useWebSocket, WebSocketResult } from "@vueuse/core"; 7 import { useWebSocket, WebSocketResult } from "@vueuse/core";
8 import { onMounted, unref } from "vue"; 8 import { onMounted, unref } from "vue";
9 -import { useFilterFn } from "./useFilterFn"; 9 +import { ExtraRequestConfigType } from "@/store/external/modules/extraComponentInfo.d";
10 10
11 11
12 interface SocketConnectionPoolType { 12 interface SocketConnectionPoolType {
@@ -24,14 +24,22 @@ const parse = (value: string) => { @@ -24,14 +24,22 @@ const parse = (value: string) => {
24 } 24 }
25 } 25 }
26 26
27 -const getSocketInstance = (requestUrl: string) => { 27 +const getOriginUrl = (url: string) => {
  28 + const { host, protocol } = location
  29 + const isLocationUrl = url === 'localhost'
  30 + const isContentSafetyProtocol = protocol === 'https'
  31 + return isLocationUrl ? `${isContentSafetyProtocol ? 'wss' : 'ws'}://${host}` : host
  32 +}
  33 +
  34 +const getSocketInstance = (request: ExtraRequestConfigType) => {
  35 + const { requestUrl, requestOriginUrl } = request
28 const socketStore = useSocketStore() 36 const socketStore = useSocketStore()
29 const index = socketConnectionPool.findIndex(item => item.url === requestUrl) 37 const index = socketConnectionPool.findIndex(item => item.url === requestUrl)
30 if (~index) { 38 if (~index) {
31 return socketConnectionPool[index].ws 39 return socketConnectionPool[index].ws
32 } 40 }
33 const token = getJwtToken() 41 const token = getJwtToken()
34 - const socketUrl = `${requestUrl}?token=${token}` 42 + const socketUrl = `${getOriginUrl(requestOriginUrl || '')}${requestUrl}?token=${token}`
35 43
36 const instance = useWebSocket(socketUrl.replace('undefined', ''), { 44 const instance = useWebSocket(socketUrl.replace('undefined', ''), {
37 onMessage() { 45 onMessage() {
@@ -44,7 +52,7 @@ const getSocketInstance = (requestUrl: string) => { @@ -44,7 +52,7 @@ const getSocketInstance = (requestUrl: string) => {
44 } 52 }
45 }) 53 })
46 54
47 - socketConnectionPool.push({ url: requestUrl, ws: instance }) 55 + socketConnectionPool.push({ url: requestUrl!, ws: instance })
48 56
49 return instance 57 return instance
50 } 58 }
@@ -62,7 +70,7 @@ export const useChartDataSocket = () => { @@ -62,7 +70,7 @@ export const useChartDataSocket = () => {
62 70
63 if ((requestContentType as RequestContentTypeEnum) !== RequestContentTypeEnum.WEB_SOCKET) return 71 if ((requestContentType as RequestContentTypeEnum) !== RequestContentTypeEnum.WEB_SOCKET) return
64 72
65 - const { send } = getSocketInstance(requestUrl!) 73 + const { send } = getSocketInstance(request!)
66 74
67 onMounted(() => { 75 onMounted(() => {
68 const message = socketStore.subscribe(targetComponent) 76 const message = socketStore.subscribe(targetComponent)
@@ -72,9 +80,8 @@ export const useChartDataSocket = () => { @@ -72,9 +80,8 @@ export const useChartDataSocket = () => {
72 80
73 const sendMessage = async (targetComponent: CreateComponentType) => { 81 const sendMessage = async (targetComponent: CreateComponentType) => {
74 const { request } = unref(targetComponent) 82 const { request } = unref(targetComponent)
75 - const { requestUrl } = request  
76 const message = socketStore.subscribe(unref(targetComponent)) 83 const message = socketStore.subscribe(unref(targetComponent))
77 - const { send, data } = getSocketInstance(requestUrl!) 84 + const { send, data } = getSocketInstance(request)
78 message && send(JSON.stringify(message)) 85 message && send(JSON.stringify(message))
79 return socketStore.getComponentValueByKeys(targetComponent, parse(unref(data))) 86 return socketStore.getComponentValueByKeys(targetComponent, parse(unref(data)))
80 } 87 }
@@ -16,7 +16,7 @@ const componentMap: { [key in ComponentType]?: any } = { @@ -16,7 +16,7 @@ const componentMap: { [key in ComponentType]?: any } = {
16 [ComponentType.DATE_PICKER]: NDatePicker 16 [ComponentType.DATE_PICKER]: NDatePicker
17 } 17 }
18 18
19 -const getParamsItemList = computed(() => { 19 +const getParamsItemList = computed(() => {
20 return props.paramsItemList 20 return props.paramsItemList
21 }) 21 })
22 22
@@ -35,7 +35,7 @@ const validate = async () => { @@ -35,7 +35,7 @@ const validate = async () => {
35 return unref(validFlag) 35 return unref(validFlag)
36 } 36 }
37 const setConfigurationData = async (params: Recordable) => { 37 const setConfigurationData = async (params: Recordable) => {
38 - await nextTick() 38 + await nextTick()
39 setDynamicFormValue(params) 39 setDynamicFormValue(params)
40 } 40 }
41 41
@@ -324,8 +324,7 @@ export const useDynamicPublicForm = (paramsItemList: Ref<ParamsItemType[]>) => { @@ -324,8 +324,7 @@ export const useDynamicPublicForm = (paramsItemList: Ref<ParamsItemType[]>) => {
324 getDeviceOption() 324 getDeviceOption()
325 } 325 }
326 326
327 - const setParams = (Params: Recordable) => {  
328 - 327 + const setParams = (Params: Recordable = {}) => {
329 for (const { key, value } of unref(getParams)) { 328 for (const { key, value } of unref(getParams)) {
330 const splitKeys = value ? value.split(GROUP_SEPARATOR) : key.split(GROUP_SEPARATOR) 329 const splitKeys = value ? value.split(GROUP_SEPARATOR) : key.split(GROUP_SEPARATOR)
331 if (isDateComponent(key as BuiltInVariable)) { 330 if (isDateComponent(key as BuiltInVariable)) {
@@ -350,7 +349,7 @@ export const useDynamicPublicForm = (paramsItemList: Ref<ParamsItemType[]>) => { @@ -350,7 +349,7 @@ export const useDynamicPublicForm = (paramsItemList: Ref<ParamsItemType[]>) => {
350 } 349 }
351 350
352 const setDynamicFormValue = (params: Recordable) => { 351 const setDynamicFormValue = (params: Recordable) => {
353 - setParams(params) 352 + setParams(params)
354 getOrgOption() 353 getOrgOption()
355 getDeviceProfileOption() 354 getDeviceProfileOption()
356 getDeviceOption() 355 getDeviceOption()
@@ -72,12 +72,14 @@ const paramsDynamicFormEl = ref<Nullable<InstanceType<typeof DynamicForm>>>(null @@ -72,12 +72,14 @@ const paramsDynamicFormEl = ref<Nullable<InstanceType<typeof DynamicForm>>>(null
72 const bodyContentEl = ref<Nullable<InstanceType<typeof BodyContent>>>(null) 72 const bodyContentEl = ref<Nullable<InstanceType<typeof BodyContent>>>(null)
73 const socketDynamicFormEl = ref<Nullable<InstanceType<typeof DynamicForm>>>(null) 73 const socketDynamicFormEl = ref<Nullable<InstanceType<typeof DynamicForm>>>(null)
74 74
75 -const handleSelectedInterfaceChange = (_value: string, option: PublicInterfaceRecord) => { 75 +const handleSelectedInterfaceChange = async (_value: string, option: PublicInterfaceRecord) => {
76 requestContentTypeRef.value = option.requestContentType as RequestContentTypeEnum 76 requestContentTypeRef.value = option.requestContentType as RequestContentTypeEnum
77 requestHttpTypeRef.value = option.requestHttpType as RequestHttpEnum 77 requestHttpTypeRef.value = option.requestHttpType as RequestHttpEnum
78 const { Header = [], Body } = JSON.parse(option.requestParams) as PublicInterfaceRequestParams 78 const { Header = [], Body } = JSON.parse(option.requestParams) as PublicInterfaceRequestParams
79 headerRef.value = isArray(Header) ? (Header as ParamsItemType[]).reduce((prev, next) => ({ ...prev, [next.key]: next.value }), {}) : {} 79 headerRef.value = isArray(Header) ? (Header as ParamsItemType[]).reduce((prev, next) => ({ ...prev, [next.key]: next.value }), {}) : {}
80 - unref(bodyContentEl)?.setConfigurationData(Body) 80 + unref(bodyContentEl)?.setConfigurationData(unref(getSelectedInterfaceBody), Body)
  81 + await nextTick()
  82 + setDynamicFormValue(option as unknown as ExtraRequestConfigType)
81 } 83 }
82 84
83 const getGetRequestTypeName = (key: RequestContentTypeEnum) => { 85 const getGetRequestTypeName = (key: RequestContentTypeEnum) => {
@@ -124,7 +126,6 @@ const setConfigurationData = async (request: ExtraRequestConfigType) => { @@ -124,7 +126,6 @@ const setConfigurationData = async (request: ExtraRequestConfigType) => {
124 requestParamsBodyTypeRef.value = requestParamsBodyType 126 requestParamsBodyTypeRef.value = requestParamsBodyType
125 requestBodyRef.value = requestParams 127 requestBodyRef.value = requestParams
126 await nextTick() 128 await nextTick()
127 - console.log(request)  
128 setDynamicFormValue(request) 129 setDynamicFormValue(request)
129 } 130 }
130 131