Skip to content

Commit

Permalink
fix: fetch user infos
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Feb 25, 2024
1 parent b75d5e7 commit 6171110
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/main/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IPCFetch } from '@types'
import { userInfo } from '@core/utils'
import { userInfo } from '@core/services'
import { IPCMain } from '../../utils'

const channel = 'fetch'
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/renderer/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (!cookie)
<button
class="btn"
@click="async() => {
const res = await FetchIPC.userInfo('1111681197')
const res = await FetchIPC.userInfo({ id: '1111681197' })
user1 = JSON.stringify(res, null, 2)
}"
>
Expand Down
5 changes: 1 addition & 4 deletions apps/monkey/src/Ctrl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ watchEffect(() => {
window.$message = useMessage()
onMounted(async () => {
if (configStore.state.uid)
return
const id = document.URL.match(/\/(\d+)/)?.[1] ?? ''
const username = document.URL.match(/\/n\/(.+)/)?.[1] ?? ''
const { uid, name } = await userInfo(id, username)
const { uid, name } = await userInfo({ id, name: decodeURIComponent(username) })
configStore.setConfig({
uid,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './postService'
export * from './userService'
56 changes: 56 additions & 0 deletions packages/core/src/services/userService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
weiFetch,
} from '../utils'

export async function userInfo(
{ id, name }: { id?: string, name?: string },
) {
let params = { }
if (name)
params = { screen_name: name }
else if (id)
params = { uid: id }

const { data } = await weiFetch('/profile/info', { params })
const { user } = data

return {
uid: user.id.toString(),
name: user.screen_name,
avatar: user.avatar_large,
followers: user.followers_count,
followings: user.friends_count,
bio: user.description,
} as {
uid: string
name: string
avatar: string
followers: number
followings: number
bio: string
}
}

export async function userDetail(uid?: string) {
const { data } = await weiFetch('/profile/detail', {
params: {
uid: uid ?? '',
},
})

const _uid = data.verified_url.match(/(\d+)/)?.[1] ?? ''

const detail = {
createdAt: data.created_at,
birsday: data.birthday,
} as {
createdAt: string
birsday: string
}

const info = await userInfo({ id: _uid })
return {
...info,
...detail,
}
}
19 changes: 0 additions & 19 deletions packages/core/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,3 @@ export async function weiFetch<T extends { data: any }>(
...options,
})
}

export async function userInfo(
id?: string,
name?: string,
): Promise<{ uid: string, name: string }> {
const { data } = await weiFetch('/profile/info', {
params: {
uid: id ?? '',
screen_name: name ?? '',
},
})
const { idstr, screen_name } = data.user || {}

return {
uid: idstr,
name: screen_name,
...data,
}
}
2 changes: 1 addition & 1 deletion types/IPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface IPCFile {
}

export interface IPCFetch {
userInfo: (id?: string, name?: string) => Promise<{ uid: string, name: string }>
userInfo: (options: { id?: string, name?: string }) => Promise<{ uid: string, name: string }>
}
9 changes: 6 additions & 3 deletions types/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ declare global {
const useWindowScroll: typeof import('@vueuse/core')['useWindowScroll']
const useWindowSize: typeof import('@vueuse/core')['useWindowSize']
const user: typeof import('../packages/database/src/schema/user')['user']
const userInfo: typeof import('../packages/core/src/utils/fetch')['userInfo']
const userDetail: typeof import('../packages/core/src/services/userService')['userDetail']
const userInfo: typeof import('../packages/core/src/services/userService')['userInfo']
const userTable: typeof import('../packages/database/src/schema/user')['userTable']
const waitForElement: typeof import('../packages/core/src/utils/dom')['waitForElement']
const watch: typeof import('vue')['watch']
Expand Down Expand Up @@ -647,7 +648,8 @@ declare module 'vue' {
readonly useWindowFocus: UnwrapRef<typeof import('@vueuse/core')['useWindowFocus']>
readonly useWindowScroll: UnwrapRef<typeof import('@vueuse/core')['useWindowScroll']>
readonly useWindowSize: UnwrapRef<typeof import('@vueuse/core')['useWindowSize']>
readonly userInfo: UnwrapRef<typeof import('../packages/core/src/utils/fetch')['userInfo']>
readonly userDetail: UnwrapRef<typeof import('../packages/core/src/services/userService')['userDetail']>
readonly userInfo: UnwrapRef<typeof import('../packages/core/src/services/userService')['userInfo']>
readonly userTable: UnwrapRef<typeof import('../packages/database/src/schema/user')['userTable']>
readonly waitForElement: UnwrapRef<typeof import('../packages/core/src/utils/dom')['waitForElement']>
readonly watch: UnwrapRef<typeof import('vue')['watch']>
Expand Down Expand Up @@ -975,7 +977,8 @@ declare module '@vue/runtime-core' {
readonly useWindowFocus: UnwrapRef<typeof import('@vueuse/core')['useWindowFocus']>
readonly useWindowScroll: UnwrapRef<typeof import('@vueuse/core')['useWindowScroll']>
readonly useWindowSize: UnwrapRef<typeof import('@vueuse/core')['useWindowSize']>
readonly userInfo: UnwrapRef<typeof import('../packages/core/src/utils/fetch')['userInfo']>
readonly userDetail: UnwrapRef<typeof import('../packages/core/src/services/userService')['userDetail']>
readonly userInfo: UnwrapRef<typeof import('../packages/core/src/services/userService')['userInfo']>
readonly userTable: UnwrapRef<typeof import('../packages/database/src/schema/user')['userTable']>
readonly waitForElement: UnwrapRef<typeof import('../packages/core/src/utils/dom')['waitForElement']>
readonly watch: UnwrapRef<typeof import('vue')['watch']>
Expand Down

0 comments on commit 6171110

Please sign in to comment.