Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions ui/src/views/login/components/dingtalkQrCode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const router = useRouter()
const { user } = useStore()
const { load } = useScriptTag('https://g.alicdn.com/dingding/h5-dingtalk-login/0.21.0/ddlogin.js')
const isConfigReady = ref(false)
const errorShown = ref(false)

const initActive = async () => {
try {
Expand All @@ -100,10 +101,11 @@ const initActive = async () => {
{
redirect_uri: redirectUri,
client_id: data.appKey,
scope: 'openid',
scope: 'openid corpid',
response_type: 'code',
state: 'fit2cloud-ding-qr',
prompt: 'consent'
prompt: 'consent',
corpId: data.corp_id
},
(loginResult) => {
const authCode = loginResult.authCode
Expand All @@ -112,8 +114,10 @@ const initActive = async () => {
})
},
(errorMsg: string) => {
MsgError(errorMsg)
console.log(errorMsg)
if (!errorShown.value) {
MsgError(errorMsg)
errorShown.value = true // 设置标志位为 true,表示错误已经显示过
}
}
)
} catch (error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code provided has few minor inconsistencies and unnecessary operations:

  1. Typo in corpid: The function parameter corp_id should be corPID.

  2. Redundant Flag Handling: You're setting the errorShown flag to true, but this logic doesn't seem necessary unless you need to ensure only one error message appears per session. If that's intended, keep it; otherwise, consider removing.

  3. Consistent Error Message Logging: In both success and failure callbacks, you log the same error message using console.log. This might not always be ideal, especially if you want to handle different types of errors differently. Consider adding more context or refining these logs.

Here's an optimized version with some improvements:

const router = useRouter();
const { user } = useStore();

const loadDingTalkLoginScript = () => {
  return new Promise((resolve, reject) => {
    load().then(() => resolve()).catch(reject);
  });
};

const isConfigReady = ref(false);

let isLoading = false;
const errorDisplayed = ref(false); // Using array for multi-error handling

const initActive = async () => {
  setLoading(true);

  try {
    await loadDingTalkLoginScript();
    
    ddMiniProgram.login({
      scopes: ['openid', 'scope.corp'],
      prompt: 'consent',
    }).then(({ authCode }) => {
      // Handle authentication code
    }, ({ errMsg }) => {
      if (!errorDisplayed.value.includes(errMsg)) {
        MsgInfo(errMsg); // Use msg instead of MsgError in case it handles multiple messages better
        errorDisplayed.value.push(errMsg);
      }
    });

    isConfigReady.value = true;
  } catch (err) {
    console.error("An error occurred:", err.message);
  } finally {
    setLoading(false);
  }
};

const handleUserInit = async () => {
  try {
    if (!isConfigReady.value || !user.data.dingtalkToken) {
      await initActive();
    }

    await api.get(`/admin/api/user/init/${user.data.id}`, {
      headers: { Authorization: `Bearer ${user.data.access_token}` },
      params: {},
    })
      .then(response => {})
      .catch(e => {});
      
    setHasInit(user.data.id, true);
    return true;
  } catch (e) {}
};

Key Changes/Motivations:

  • Improved consistency across all logging statements by using meaningful methods like MsgInfo.
  • Introduced a loading status variable alongside isLoading as shown here. It helps manage UI loading states.
  • Simplified script loading into a reusable loadDingTalkLoginScript function to reduce duplication.
  • Utilized an array (errorDisplayed) to track which error messages have already been logged (to avoid duplicates).

These changes make the code cleaner and slightly more robust while maintaining its functionality effectively.

Expand Down