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(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 => { 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(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( joinUrl(env.staffBase, 'organizationMember/bindUserInfoAndGetUserMemberInfoByPhone'), { userPhone: phone } ) as unknown as OrganizationMemberInfo }