Explorar o código

fix(CollapsibleToolbar): 修复用户信息初始化加载问题

1. 新增onMounted钩子在组件挂载时加载用户信息
2. 抽离获取用户信息逻辑为getUserInfo公共方法
3. 简化watch监听中的重复请求代码
lsc hai 2 semanas
pai
achega
b9b6231dcc
Modificáronse 1 ficheiros con 13 adicións e 5 borrados
  1. 13 5
      src/components/CollapsibleToolbar/index2.vue

+ 13 - 5
src/components/CollapsibleToolbar/index2.vue

@@ -791,7 +791,7 @@
 </template>
 
 <script lang="ts" setup>
-import { ref, watch, computed, useTemplateRef, nextTick } from 'vue'
+import { ref, watch, computed, useTemplateRef, nextTick, onMounted } from 'vue'
 import { storeToRefs } from 'pinia'
 import useCreateElement from '@/hooks/useCreateElement'
 import useSlideHandler from '@/hooks/useSlideHandler'
@@ -838,14 +838,17 @@ const props = withDefaults(defineProps<{
   onlyEnglish: false,
 })
 
+const getUserInfo = async (userid: string) => {
+  const res = await axios.get('https://pbl.cocorobo.cn/api/pbl/selectUser2', {
+    params: { userid }
+  })
+  return res[0][0] || {}
+}
 
 const userJson = ref<any>({})
 watch(() => props.userid, async (newVal) => {
   if (!newVal) return
-  const res = await axios.get('https://pbl.cocorobo.cn/api/pbl/selectUser2', {
-    params: { userid: newVal }
-  })
-  userJson.value = res[0][0] || {}
+  userJson.value = await getUserInfo(newVal)
 })
 
 const emit = defineEmits<{
@@ -1593,6 +1596,11 @@ const addAiWeb = async (code: string) => {
   webId.value = ''
   isEdit.value = false
 }
+
+onMounted(async () => {
+  if (!props.userid) return
+  userJson.value = await getUserInfo(props.userid)
+})
 </script>
 
 <style lang="scss" scoped>