535 lines
16 KiB
TypeScript
535 lines
16 KiB
TypeScript
import { request, uploadFile, uploadFiles } 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
|
||
}
|
||
|
||
/** 设备策略:字段较多,按后端字段名索引,值为 0 关闭 / 1 开启 */
|
||
export interface DevicePolicy {
|
||
[field: string]: string | number | undefined
|
||
}
|
||
|
||
export interface HomeLayoutPage {
|
||
slot: string
|
||
position?: number
|
||
hidden?: boolean
|
||
}
|
||
|
||
export interface DeviceHomeLayout {
|
||
serialno?: string
|
||
pages?: HomeLayoutPage[]
|
||
defaultPage?: string
|
||
}
|
||
|
||
export interface IconPackEntry {
|
||
packageName: string
|
||
label: string
|
||
available: boolean
|
||
/** 图标 PNG 的 base64(后端可能省略或超长,展示前需自行解码) */
|
||
iconPngBase64?: string
|
||
}
|
||
|
||
export interface IconPackState {
|
||
selectedPackage?: string
|
||
entries?: IconPackEntry[]
|
||
}
|
||
|
||
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 })}` })
|
||
|
||
const opsUrl = (path: string, sn: string) => `/v1/client/sn/ops/${path}?${query({ sn })}`
|
||
const postOp = (path: string, sn: string) =>
|
||
request<void>({ url: opsUrl(path, sn), method: 'POST' })
|
||
|
||
/** 设备操作:屏幕截图/远程相机/定位/刷新 */
|
||
export const rebootDevice = (sn: string) => postOp('reboot', sn)
|
||
export const shutdownDevice = (sn: string) => postOp('shutdown', sn)
|
||
export const captureScreenshot = (sn: string) => postOp('screenshot', sn)
|
||
export const refreshDevice = (sn: string) => postOp('refresh', sn)
|
||
export const locateDevice = (sn: string) => postOp('locate', sn)
|
||
|
||
/** 远程相机:camera 0 后置 / 1 前置 */
|
||
export const transferFile = (sn: string) => postOp('file_transfer', sn)
|
||
export const takeDevicePhoto = (sn: string, camera: 0 | 1) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/ops/take_photo?${query({ sn, camera })}`,
|
||
method: 'POST'
|
||
})
|
||
export const syncMessages = (sn: string) =>
|
||
request<string>({ url: opsUrl('msg_sync', sn), method: 'POST' })
|
||
export const cleanDevice = (sn: string) => postOp('clean', sn)
|
||
export const mirrorScreen = (sn: string) => postOp('mirror', sn)
|
||
export const lockScreen = (sn: string) => postOp('screen_lock', sn)
|
||
export const restoreDevice = (sn: string) => postOp('restore', sn)
|
||
export const toggleDeveloperMode = (sn: string) => postOp('developer', sn)
|
||
|
||
/** 系统设置:读取策略并按单个字段切换(0 关闭 / 1 开启) */
|
||
export const getDevicePolicy = (sn: string) =>
|
||
request<DevicePolicy>({ url: `/v1/client/sn/policy?${query({ sn })}` })
|
||
export const toggleDevicePolicy = (sn: string, field: string, value: 0 | 1) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/policy/toggle?${query({ sn })}`,
|
||
method: 'PUT',
|
||
data: { field, value }
|
||
})
|
||
|
||
/** 壁纸配置:mode 0 单个 / 1 轮换 */
|
||
export interface WallpaperConfig {
|
||
serialno?: string
|
||
mode?: number
|
||
images?: string[]
|
||
}
|
||
|
||
export interface WallpaperUploadResult {
|
||
url?: string
|
||
fileSize?: number
|
||
}
|
||
|
||
export const getWallpaper = (sn: string) =>
|
||
request<WallpaperConfig>({ url: `/v1/client/sn/wallpaper?${query({ sn })}` })
|
||
|
||
/** 上传单张壁纸并立即推送设备(mode=0),返回保存后的图片 URL */
|
||
export const uploadWallpaperSingle = (sn: string, filePath: string) =>
|
||
uploadFile<WallpaperUploadResult>({
|
||
url: `/v1/client/sn/wallpaper/single?${query({ sn })}`,
|
||
filePath,
|
||
name: 'file'
|
||
})
|
||
|
||
/** 整组上传轮换壁纸并推送设备(mode=1,最多 7 张),返回按序保存的图片 URL */
|
||
export const uploadWallpaperRotation = (sn: string, filePaths: string[]) =>
|
||
uploadFiles<Array<{ url?: string }>>({
|
||
url: `/v1/client/sn/wallpaper/rotation?${query({ sn })}`,
|
||
files: filePaths.map((filePath) => ({ name: 'files', filePath }))
|
||
}).then((list) => (list || []).map((item) => item.url || ''))
|
||
|
||
/** 调整轮换壁纸展示顺序(按传入 URL 顺序更新 sort_no) */
|
||
export const reorderWallpaper = (sn: string, images: string[]) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/wallpaper/reorder?${query({ sn })}`,
|
||
method: 'POST',
|
||
data: { images }
|
||
})
|
||
|
||
/** 首页布局:读取与恢复默认;完整拖拽编辑需后续单独页面 */
|
||
export const getHomeLayout = (sn: string) =>
|
||
request<DeviceHomeLayout>({ url: `/v1/client/sn/home-layout?${query({ sn })}` })
|
||
export const resetHomeLayout = (sn: string) =>
|
||
request<DeviceHomeLayout>({
|
||
url: `/v1/client/sn/home-layout?${query({ sn })}`,
|
||
method: 'DELETE'
|
||
})
|
||
|
||
/** 图标包:读取设备候选列表并下发选择 */
|
||
export const getIconPacks = (sn: string) =>
|
||
request<IconPackState>({ url: `/v1/client/sn/icon-packs?${query({ sn })}` })
|
||
export const setIconPack = (sn: string, packageName: string) =>
|
||
request<IconPackState>({
|
||
url: `/v1/client/sn/icon-packs?${query({ sn })}`,
|
||
method: 'PUT',
|
||
data: { packageName }
|
||
})
|
||
|
||
/** 统一分页返回;后端不同接口可能返回 list 或 records */
|
||
export interface PageResult<T> {
|
||
list?: T[]
|
||
records?: T[]
|
||
total?: number
|
||
pageNum?: number
|
||
pageSize?: number
|
||
}
|
||
|
||
export const pageItems = <T>(result: PageResult<T>): T[] => result.list ?? result.records ?? []
|
||
|
||
/** 修改资料 / 头像 */
|
||
export const updateProfile = (nickname: string, signature: string) =>
|
||
request<Profile>({
|
||
url: '/v1/client/account/profile',
|
||
method: 'PUT',
|
||
data: { nickname, signature }
|
||
})
|
||
|
||
export const uploadProfileAvatar = (filePath: string) =>
|
||
uploadFile<Profile>({
|
||
url: '/v1/client/account/profile/avatar',
|
||
filePath,
|
||
name: 'file'
|
||
})
|
||
|
||
/** 账号安全 */
|
||
export const changePassword = (
|
||
oldPassword: string,
|
||
newPassword: string,
|
||
confirmPassword: string
|
||
) =>
|
||
request<AuthToken>({
|
||
url: '/v1/client/account/password',
|
||
method: 'PUT',
|
||
data: { oldPassword, newPassword, confirmPassword }
|
||
})
|
||
|
||
export const sendPasswordSmsCode = () =>
|
||
request<void>({ url: '/v1/client/account/password/sms/code', method: 'POST' })
|
||
|
||
export const changePasswordBySmsCode = (
|
||
code: string,
|
||
newPassword: string,
|
||
confirmPassword: string
|
||
) =>
|
||
request<AuthToken>({
|
||
url: '/v1/client/account/password/sms',
|
||
method: 'PUT',
|
||
data: { code, newPassword, confirmPassword }
|
||
})
|
||
|
||
/** 设备管理 */
|
||
export const bindDevice = (sn: string) =>
|
||
request<boolean>({ url: `/v1/client/sn/bind?${query({ sn })}`, method: 'POST' })
|
||
|
||
export const bindDeviceRequest = (sn: string, remark?: string) =>
|
||
request<string>({
|
||
url: `/v1/client/sn/bind-request?${query({ sn, remark })}`,
|
||
method: 'POST'
|
||
})
|
||
|
||
export const unbindDevice = (sn: string) =>
|
||
request<boolean>({ url: `/v1/client/sn/unbind?${query({ sn })}`, method: 'POST' })
|
||
|
||
/** 设备通讯录 */
|
||
export interface ContactItem {
|
||
id?: number
|
||
name: string
|
||
nickName?: string
|
||
phoneNumber: string
|
||
avatar?: string
|
||
position?: number
|
||
emergency?: boolean
|
||
showDesktop?: boolean
|
||
bindPhone?: string
|
||
bindSn?: string
|
||
}
|
||
|
||
export const listContacts = (sn: string) =>
|
||
request<ContactItem[]>({ url: `/v1/client/sn/contact/list?${query({ sn })}` })
|
||
|
||
export const getContactForm = (sn: string, id: number) =>
|
||
request<ContactItem>({
|
||
url: `/v1/client/sn/contact/form?${query({ sn, id })}`
|
||
})
|
||
|
||
export const insertContact = (sn: string, form: ContactItem) =>
|
||
request<number>({
|
||
url: `/v1/client/sn/contact/insert?${query({ sn })}`,
|
||
method: 'POST',
|
||
data: { ...form }
|
||
})
|
||
|
||
export const updateContact = (sn: string, form: ContactItem) =>
|
||
request<boolean>({
|
||
url: `/v1/client/sn/contact/update?${query({ sn, id: form.id })}`,
|
||
method: 'PUT',
|
||
data: { ...form }
|
||
})
|
||
|
||
export const deleteContact = (sn: string, id: number) =>
|
||
request<boolean>({
|
||
url: `/v1/client/sn/contact/delete?${query({ sn, id })}`,
|
||
method: 'DELETE'
|
||
})
|
||
|
||
/** 设备图片 / 截图 */
|
||
export interface DevicePicture {
|
||
url?: string
|
||
favorite?: number
|
||
fileSize?: number
|
||
}
|
||
|
||
export const listDevicePictures = (sn: string) =>
|
||
request<DevicePicture[]>({ url: `/v1/client/sn/picture/list?${query({ sn })}` })
|
||
|
||
export const sendPictureToDevice = (sn: string, filePath: string, favorite = 0) =>
|
||
uploadFile<{ url?: string; fileSize?: number }>({
|
||
url: `/v1/client/sn/picture/send?${query({ sn, favorite })}`,
|
||
filePath,
|
||
name: 'file'
|
||
})
|
||
|
||
export const setPictureFavorite = (sn: string, url: string, favorite: 0 | 1) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/picture/favorite?${query({ sn, url, favorite })}`,
|
||
method: 'POST'
|
||
})
|
||
|
||
export const deletePicture = (sn: string, url: string) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/picture?${query({ sn, url })}`,
|
||
method: 'DELETE'
|
||
})
|
||
|
||
export const deleteScreenshot = (sn: string, id: number) =>
|
||
request<boolean>({
|
||
url: `/v1/client/sn/ops/screenshots/${id}?${query({ sn })}`,
|
||
method: 'DELETE'
|
||
})
|
||
|
||
/** 通话 / 短信记录 */
|
||
export interface CallRecord {
|
||
sourceId?: string
|
||
number?: string
|
||
contactName?: string
|
||
direction?: string
|
||
calledAt?: string
|
||
durationSeconds?: number
|
||
}
|
||
|
||
export interface SmsRecord {
|
||
sourceId?: number
|
||
direction?: string
|
||
address?: string
|
||
contactName?: string
|
||
sentTime?: string
|
||
simSlot?: number
|
||
}
|
||
|
||
export const getCallPage = (sn: string, pageNum = 1, pageSize = 30, direction?: string) =>
|
||
request<PageResult<CallRecord>>({
|
||
url: `/v1/client/sn/records/calls?${query({ sn, pageNum, pageSize, direction })}`
|
||
})
|
||
|
||
export const syncCallRecords = (sn: string) =>
|
||
request<void>({ url: `/v1/client/sn/records/calls/sync?${query({ sn })}`, method: 'POST' })
|
||
|
||
export const getSmsPage = (sn: string, pageNum = 1, keyword?: string, direction?: string) =>
|
||
request<PageResult<SmsRecord>>({
|
||
url: `/v1/client/sn/records/sms?${query({ sn, pageNum, pageSize: 30, keyword, direction })}`
|
||
})
|
||
|
||
export const getSmsContent = (sn: string, sourceId: number) =>
|
||
request<{ sourceId?: number; content?: string }>({
|
||
url: `/v1/client/sn/records/sms/content?${query({ sn, sourceId })}`
|
||
})
|
||
|
||
/** 设备应用 */
|
||
export interface DeviceApp {
|
||
packageName?: string
|
||
appName?: string
|
||
versionName?: string
|
||
versionCode?: number
|
||
installTime?: string
|
||
apkSize?: number
|
||
systemApp?: boolean
|
||
iconUrl?: string
|
||
}
|
||
|
||
export const listDeviceApps = (sn: string) =>
|
||
request<DeviceApp[]>({ url: `/v1/client/sn/apks?${query({ sn })}` })
|
||
|
||
const appAction = (sn: string, packageName: string, action: string) =>
|
||
request<boolean>({
|
||
url: `/v1/client/sn/ops/app/${action}?${query({ sn })}`,
|
||
method: 'POST',
|
||
data: { packageName }
|
||
})
|
||
|
||
export const launchApp = (sn: string, packageName: string) => appAction(sn, packageName, 'launch')
|
||
export const stopApp = (sn: string, packageName: string) => appAction(sn, packageName, 'stop')
|
||
export const clearAppData = (sn: string, packageName: string) => appAction(sn, packageName, 'clear_data')
|
||
export const uninstallApp = (sn: string, packageName: string) => appAction(sn, packageName, 'uninstall')
|
||
|
||
/** 服务端文件:仅提供上传/删除,暂无列表接口 */
|
||
export interface ClientFileInfo {
|
||
name?: string
|
||
url?: string
|
||
md5?: string
|
||
sha1?: string
|
||
sha256?: string
|
||
}
|
||
|
||
export const uploadClientFile = (filePath: string) =>
|
||
uploadFile<ClientFileInfo>({ url: '/v1/client/files', filePath, name: 'file' })
|
||
|
||
export const deleteClientFile = (filePath: string) =>
|
||
request<void>({ url: `/v1/client/files?${query({ filePath })}`, method: 'DELETE' })
|
||
|
||
/** 首页布局编辑 */
|
||
export type HomeLayout = DeviceHomeLayout
|
||
|
||
export interface HomeLayoutSavePage {
|
||
slot: string
|
||
position: number
|
||
hidden: boolean
|
||
}
|
||
|
||
export const saveHomeLayout = (
|
||
sn: string,
|
||
pages: HomeLayoutSavePage[],
|
||
defaultPage: string
|
||
) =>
|
||
request<HomeLayout>({
|
||
url: `/v1/client/sn/home-layout?${query({ sn })}`,
|
||
method: 'PUT',
|
||
data: { pages, defaultPage }
|
||
})
|
||
|
||
/** 设备闹钟(非告警记录) */
|
||
export interface DeviceAlarm {
|
||
id?: number
|
||
hour?: number
|
||
minute?: number
|
||
label?: string
|
||
repeatType?: number
|
||
customDays?: string
|
||
isEnabled?: number
|
||
vibration?: number
|
||
volume?: number
|
||
ringtoneUri?: string
|
||
snoozeInterval?: number
|
||
}
|
||
|
||
export const listAlarms = (sn: string) =>
|
||
request<DeviceAlarm[]>({ url: `/v1/client/sn/alarm?${query({ sn })}` })
|
||
|
||
export const addAlarm = (sn: string, alarm: DeviceAlarm) =>
|
||
request<number>({
|
||
url: `/v1/client/sn/alarm?${query({ sn })}`,
|
||
method: 'POST',
|
||
data: { ...alarm }
|
||
})
|
||
|
||
export const updateAlarm = (sn: string, alarm: DeviceAlarm) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/alarm?${query({ sn })}`,
|
||
method: 'PUT',
|
||
data: { ...alarm }
|
||
})
|
||
|
||
export const deleteAlarm = (sn: string, id: number) =>
|
||
request<void>({
|
||
url: `/v1/client/sn/alarm?${query({ sn, id })}`,
|
||
method: 'DELETE'
|
||
})
|