57 lines
1.7 KiB
TypeScript
57 lines
1.7 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
|
|
}
|
|
|
|
const basicAuth = () => {
|
|
const raw = `${env.clientId}:${env.clientSecret}`
|
|
return `Basic ${window.btoa(raw)}`
|
|
}
|
|
|
|
export const sendMobileCode = async (phone: string) => {
|
|
if (env.useMock) return mockApi.sendMobileCode(phone)
|
|
return await http.get<boolean>(joinUrl(env.adminBase, 'platformUser/sendMobileCode', phone), {
|
|
headers: {
|
|
clientId: env.clientId,
|
|
clientConfigId: env.clientConfigId
|
|
}
|
|
}) 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
|
|
}
|