Browse Source

适配2.1.0版本,取消RequestBody、ResponseBody的结构自动刷新。

jmxd 2 years ago
parent
commit
bac39c58bd

+ 2 - 1
src/components/common/dialog/magic-notify.vue

@@ -33,7 +33,8 @@ const removeMessage = message => {
 }
 const appendMessage = message => {
 	message.icon = icons[message.icon] || icons.info
-	message.id = parseInt(Math.random() * 10000000)
+	message.id = message.id || parseInt(Math.random() * 10000000)
+	removeMessage(message)
 	messages.push(message)
 	if(message.duration){
 		setTimeout(() => removeMessage(message), parseInt(message.duration))

+ 9 - 2
src/components/magic-editor.vue

@@ -291,9 +291,16 @@ bus.$event(Socket.LOGIN_RESPONSE, ([ret, user]) => {
 		// 登录失败
 	}
 })
-bus.$on(Message.LOGOUT, () => {
+bus.$event(Socket.REFRESH_TOKEN, ([newToken]) => {
+	console.log('refresh-token', newToken)
+	constants.HEADER_MAGIC_TOKEN_VALUE = newToken
+	store.set(constants.STORE.token, constants.HEADER_MAGIC_TOKEN_VALUE)
+})
+const showLoginFunc = () => {
 	showLogin.value = true
 	websocket && websocket.close()
 	websocket = null
-})
+}
+bus.$on(Message.LOGOUT, showLoginFunc)
+bus.$on(Message.SHOW_LOGIN, showLoginFunc)
 </script>

+ 23 - 3
src/components/panel/api/magic-api-request-body.vue

@@ -4,7 +4,10 @@
 <script setup>
 import { inject } from 'vue'
 import { parseJson } from '../../../scripts/parsing/parser.js'
-import { definition2TreeList } from '../../../scripts/utils.js'
+import { definition2TreeList, structureHasChanged } from '../../../scripts/utils.js'
+import bus from '../../../scripts/bus.js'
+import $i from '../../../scripts/i18n.js'
+import Message from '../../../scripts/constants/message.js'
 const info = inject('info')
 const opened = inject('opened')
 const onChange = e => {
@@ -13,8 +16,25 @@ const onChange = e => {
     }
     const definition = parseJson(info.value.requestBody, info.value.requestBodyDefinition)
     if(definition){
-        info.value.requestBodyDefinition = definition
-        opened.value.requestBodyTree = definition2TreeList(definition)
+        if(structureHasChanged(info.value.requestBodyDefinition, definition)){
+            bus.$emit(Message.NOTIFY, {
+                title: $i('message.tips'),
+                id: 'requestBodyStructure',
+                icon: 'info',
+                content: $i('api.structure.content', 'RequestBody'),
+                buttons: [{
+                    title: $i('api.structure.ok'),
+                    onClick: () => {
+                        info.value.requestBodyDefinition = definition
+                        opened.value.requestBodyTree = definition2TreeList(definition)
+                    }
+                }, {
+                    title: $i('message.cancel'),
+                    onClick: () => {}
+                }]
+            })
+            
+        }
     }
 }
 </script>

+ 2 - 2
src/components/panel/footer/magic-status-bar.vue

@@ -36,7 +36,7 @@ const icons = [{
     icon: 'qq',
     title: $i('message.joinGroup'),
     displayKey: 'qqGroup',
-    onClick: () => window.open('https://qm.qq.com/cgi-bin/qm/qr?k=Q6dLmVS8cHwoaaP18A3tteK_o0244e6B&jump_from=webapi')
+    onClick: () => window.open('https://www.ssssssss.org/magic-api/pages/group/')
 }, {
     icon: 'help',
     title: $i('message.document'),
@@ -98,4 +98,4 @@ bus.$on(Message.STATUS, msg => content.value = msg)
 .magic-status-bar ul li:hover{
     background-color: var(--main-hover-icon-background-color);
 }
-</style>
+</style>

+ 1 - 0
src/scripts/constants/message.js

@@ -3,6 +3,7 @@ export default {
     DO_TEST: 'doTest',
     LOGINED: 'logined',
     LOGOUT: 'logout',
+    SHOW_LOGIN: 'showLogin',
     DO_DOWNLOAD: 'do-download',
     DO_UPLOAD: 'do-upload',
     DO_PUSH: 'do-push',

+ 1 - 0
src/scripts/constants/socket.js

@@ -4,6 +4,7 @@ export default {
     LOGIN: 'login',
     RESUME_BREAKPOINT: 'resume_breakpoint',
     LOGIN_RESPONSE: 'login_response',
+    REFRESH_TOKEN: 'refresh_token',
     LOG: 'log',
     LOGS: 'logs',
     EXCEPTION: 'exception',

+ 4 - 0
src/scripts/i18n/en.js

@@ -213,6 +213,10 @@ export default {
             requestError: 'Failed to send Request「{0}」',
             success: '「{0}」Request finish, Status:<em>{1}</em> Size:<em>{2}</em> Time:<em>{3} ms</em>',
             begin: 'Start Request For「{0}」'
+        },
+        structure: {
+            content: '{0} Structure has changed, Do you wang to change?',
+            ok: 'Change',
         }
     },
     datasource: {

+ 4 - 0
src/scripts/i18n/zh-cn.js

@@ -214,6 +214,10 @@ export default {
             requestError: '请求「{0}」出错',
             success: '「{0}」测试完毕,状态:<em>{1}</em> 大小:<em>{2}</em> 耗时:<em>{3} ms</em>',
             begin: '开始测试「{0}」'
+        },
+        structure: {
+            content: '当前 {0} 结构发生变化,是否更新?',
+            ok: '更新',
         }
     },
     datasource: {

+ 1 - 1
src/scripts/parsing/parser.js

@@ -945,7 +945,7 @@ function processBody(body, srcObj) {
 				name,
 				defaultValue: srcObj[name]?.defaultValue,
 				value: isSimpleObject(value) ? value.span.getText().trim() : '',
-				dataType: srcObj[name]?.dataType || getType(value),
+				dataType: getType(value),
 			}
 			if (value instanceof MapLiteral || value instanceof ListLiteral) {
 				param.children = processBody(value, objToMap(srcObj[name]));

+ 5 - 4
src/scripts/request.js

@@ -3,6 +3,8 @@ import Qs from 'qs'
 import constants from './constants.js'
 import modal from '../components/common/dialog/magic-modal.js'
 import { default as $i, translateCode } from './i18n.js'
+import bus from './bus.js'
+import Message from './constants/message.js'
 const config = {
 	// 请求路径
 	baseURL: '',
@@ -154,11 +156,10 @@ class HttpRequest {
 			} else if (data.code === 1) {
 				successed = true
 				httpResponse.successHandle && httpResponse.successHandle(data.data, response)
+			} else if (data.code === 401) {
+				bus.$emit(Message.SHOW_LOGIN)
+				console.log('未登录')
 			} else {
-				if (data.code === 401) {
-					// bus.$emit('showLogin')
-					console.log('未登录')
-				}
 				httpResponse.exceptionHandle && httpResponse.exceptionHandle(data.code, data.message, response)
 			}
 		}

+ 20 - 3
src/scripts/service/magic-api.js

@@ -4,7 +4,7 @@ import request from '../request.js'
 import bus from '../bus.js'
 import $i from '../i18n.js'
 import modal from '../../components/common/dialog/magic-modal.js'
-import { definition2TreeList, getSizeUnit, formatJson } from '../utils.js'
+import { definition2TreeList, getSizeUnit, formatJson, structureHasChanged } from '../utils.js'
 import { parseJson } from '../parsing/parser.js'
 import { nextTick } from 'vue'
 function sendApiTestRequest(opened, requestConfig) {
@@ -49,8 +49,25 @@ function sendApiTestRequest(opened, requestConfig) {
 		} else {
 			opened.item.responseBody = formatJson(data)
 			let definition = parseJson(opened.item.responseBody, opened.item.responseBodyDefinition)
-			opened.item.responseBodyDefinition = definition
-			opened.responseBodyTree = definition2TreeList(definition)
+			if(structureHasChanged(opened.item.responseBodyDefinition, definition)){
+				bus.$emit(Message.NOTIFY, {
+					title: $i('message.tips'),
+					id: 'responseBodyStructure',
+					icon: 'info',
+					content: $i('api.structure.content', 'ResponseBody'),
+					buttons: [{
+						title: $i('api.structure.ok'),
+						onClick: () => {
+							opened.item.responseBodyDefinition = definition
+							opened.responseBodyTree = definition2TreeList(definition)
+						}
+					}, {
+						title: $i('message.cancel'),
+						onClick: () => {}
+					}]
+				})
+				
+			}
 		}
 		opened.responseHeaders = res.headers
 		nextTick(() => bus.$emit(Message.SWITCH_TOOLBAR, 'response'))

+ 16 - 1
src/scripts/utils.js

@@ -269,4 +269,19 @@ export function formatJson(val, defaultVal) {
         }
     }
     return defaultVal || ''
-};
+};
+
+export function structureHasChanged (obj, newObj){
+    if ((!obj && newObj) || (!newObj && obj)) {
+        return true
+    }
+    if (obj.dataType !== newObj.dataType || obj.name !== newObj.name || obj?.children.length !== newObj.children.length) {
+        return true
+    }
+    for(let i = 0, len = obj.children.length; i < len; i++){
+        if (structureHasChanged(obj.children[i], newObj.children[i])) {
+            return true
+        }
+    }
+    return false
+}