75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import http from './request'
|
|
import { mockApi } from './mock'
|
|
import { env } from '@/utils/env'
|
|
import type { TokenResponse } from '@/types/api'
|
|
import { joinUrl } from '@/utils/url'
|
|
|
|
export interface OrganizationMemberInfo {
|
|
memberId?: string
|
|
organizationId?: string
|
|
memberName?: string
|
|
userPhone?: string
|
|
memberRoleId?: string
|
|
}
|
|
|
|
export interface SendMobileCodePayload {
|
|
phone: string
|
|
imageRandomStr: string
|
|
imageCode: string
|
|
}
|
|
|
|
const basicAuth = () => {
|
|
const raw = `${env.clientId}:${env.clientSecret}`
|
|
return `Basic ${window.btoa(raw)}`
|
|
}
|
|
|
|
export const getImageCodeUrl = (imageRandomStr: string) => {
|
|
const params = new URLSearchParams({
|
|
randomStr: imageRandomStr,
|
|
t: String(Date.now())
|
|
})
|
|
return `${joinUrl(env.authBase, 'code/image')}?${params.toString()}`
|
|
}
|
|
|
|
export const sendMobileCode = async (payload: SendMobileCodePayload) => {
|
|
if (env.useMock) return mockApi.sendMobileCode(payload)
|
|
return await http.get<boolean>(joinUrl(env.adminBase, 'platformUser/sendMobileCode', payload.phone), {
|
|
headers: {
|
|
clientId: env.clientId,
|
|
clientConfigId: env.clientConfigId
|
|
},
|
|
params: {
|
|
imageRandomStr: payload.imageRandomStr,
|
|
imageCode: payload.imageCode
|
|
}
|
|
}) as unknown as boolean
|
|
}
|
|
|
|
export const loginByMobile = async (phone: string, code: string): Promise<TokenResponse> => {
|
|
if (env.useMock) return mockApi.loginByMobile(phone)
|
|
|
|
const form = new URLSearchParams()
|
|
form.set('grant_type', 'mobile')
|
|
form.set('mobile', phone)
|
|
form.set('code', code)
|
|
form.set('scope', 'server')
|
|
if (env.clientConfigId) {
|
|
form.set('clientConfigId', env.clientConfigId)
|
|
}
|
|
|
|
return await http.post<TokenResponse>(joinUrl(env.authBase, 'oauth2/token'), form, {
|
|
headers: {
|
|
Authorization: basicAuth(),
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
}
|
|
}) as unknown as TokenResponse
|
|
}
|
|
|
|
export const bindUserInfoAndGetUserMemberInfoByPhone = async (phone: string) => {
|
|
if (env.useMock) return mockApi.bindOrganization(phone)
|
|
return await http.post<OrganizationMemberInfo>(
|
|
joinUrl(env.staffBase, 'organizationMember/bindUserInfoAndGetUserMemberInfoByPhone'),
|
|
{ userPhone: phone }
|
|
) as unknown as OrganizationMemberInfo
|
|
}
|