Files
ttstd_family_care_miniapp/utils/api.ts
T

133 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { request } from './request'
/** 登录返回(后端 AuthenticationToken) */
export interface AuthToken {
tokenType: string
accessToken: string
refreshToken: string
expiresIn: number
}
/** 微信静默登录返回;needBindMobile 为 true 时没有令牌 */
export interface WxLoginResult {
isNewUser: boolean
needBindMobile: boolean
accessToken?: string
refreshToken?: string
tokenType?: string
expiresIn?: number
}
export interface Profile {
id: number
username: string
nickname: string
avatar?: string
signature?: string
}
export interface DeviceBrief {
serialno: string
snName?: string
snModel?: string
online?: number
status?: number
lastHeartbeatTime?: string
}
export interface DeviceLocation {
sn?: string
province?: string
city?: string
district?: string
street?: string
streetNumber?: string
latitude?: string
longitude?: string
address?: string
locationDescribe?: string
mapError?: string
lastSuccessfulTime?: string
}
export interface Screenshot {
id: number
url?: string
uploadTime?: string
fileName?: string
}
const query = (params: Record<string, string | number | undefined>): string =>
Object.keys(params)
.filter((key) => params[key] !== undefined && params[key] !== '')
.map((key) => `${key}=${encodeURIComponent(String(params[key]))}`)
.join('&')
/** 手机号验证码登录(未注册自动注册),与短信验证码共用同一验证码 */
export const loginBySms = (mobile: string, code: string) =>
request<AuthToken>({
url: '/v1/open/auth/login/mobile',
method: 'POST',
data: { mobile, code },
auth: false
})
export const loginByPassword = (username: string, password: string) =>
request<AuthToken>({
url: '/v1/open/auth/login',
method: 'POST',
data: { username, password },
auth: false
})
/** 发送登录/注册短信验证码(后端为 @RequestParam mobile) */
export const sendSmsCode = (mobile: string) =>
request<void>({
url: `/v1/open/auth/sms/code?${query({ mobile })}`,
method: 'POST',
auth: false
})
/** 微信静默登录:按 openid 查已绑定账号(后端为 @RequestParam code) */
export const wxSilentLogin = (code: string) =>
request<WxLoginResult>({
url: `/v1/open/auth/wxma/silent-login?${query({ code })}`,
method: 'POST',
auth: false
})
/** 微信手机号快捷登录(需企业主体小程序) */
export const wxPhoneLogin = (loginCode: string, phoneCode: string) =>
request<AuthToken>({
url: '/v1/open/auth/wxma/phone-login',
method: 'POST',
data: { loginCode, phoneCode },
auth: false
})
/** 微信绑定手机号:loginCode 必须是提交前重新 wx.login 取得的新 code */
export const wxBindMobile = (loginCode: string, mobile: string, smsCode: string) =>
request<AuthToken>({
url: '/v1/open/auth/wxma/bind-mobile',
method: 'POST',
data: { loginCode, mobile, smsCode },
auth: false
})
export const logout = (accessToken: string) =>
request<void>({
url: `/v1/open/auth/logout?${query({ accessToken })}`,
method: 'POST',
auth: false
})
export const getProfile = () => request<Profile>({ url: '/v1/client/account/profile' })
export const getMyDevices = () => request<DeviceBrief[]>({ url: '/v1/client/sn/my-devices' })
export const getDeviceLocation = (sn: string) =>
request<DeviceLocation>({ url: `/v1/client/sn/location?${query({ sn })}` })
export const getScreenshots = (sn: string) =>
request<Screenshot[]>({ url: `/v1/client/sn/upload-screenshot?${query({ sn })}` })