/ clis / weibo / utils.js
utils.js
 1  /**
 2   * Shared Weibo utilities — uid extraction.
 3   */
 4  import { AuthRequiredError } from '@jackwener/opencli/errors';
 5  /** Get the currently logged-in user's uid from Vue store or config API. */
 6  export async function getSelfUid(page) {
 7      const uid = await page.evaluate(`
 8      (() => {
 9        const app = document.querySelector('#app')?.__vue_app__;
10        const store = app?.config?.globalProperties?.$store;
11        const uid = store?.state?.config?.config?.uid;
12        if (uid) return String(uid);
13        return null;
14      })()
15    `);
16      if (uid)
17          return uid;
18      // Fallback: config API
19      const config = await page.evaluate(`
20      (async () => {
21        const resp = await fetch('/ajax/config/get_config', {credentials: 'include'});
22        if (!resp.ok) return null;
23        const data = await resp.json();
24        return data.ok && data.data?.uid ? String(data.data.uid) : null;
25      })()
26    `);
27      if (config)
28          return config;
29      throw new AuthRequiredError('weibo.com');
30  }