feat:蓝牙绑定和在线客服
This commit is contained in:
@@ -23,3 +23,11 @@ export function saveAuthToken(data: AuthTokenResult | unknown) {
|
||||
}
|
||||
return saveStoredToken(token)
|
||||
}
|
||||
|
||||
/** POST /app/auth/logout — 退出登录 */
|
||||
export function logout() {
|
||||
return request<void>({
|
||||
url: '/app/auth/logout',
|
||||
method: 'POST',
|
||||
})
|
||||
}
|
||||
|
||||
72
src/api/home.ts
Normal file
72
src/api/home.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { request } from '@/api/request'
|
||||
|
||||
import type { AppHomeVo } from '@/types/location'
|
||||
|
||||
import { parseId, sameId } from '@/utils/device-id'
|
||||
|
||||
|
||||
|
||||
/** GET /app/home/list — 查询当前用户的家庭列表 */
|
||||
|
||||
export function fetchHomeList() {
|
||||
|
||||
return request<AppHomeVo[]>({
|
||||
|
||||
url: '/app/home/list',
|
||||
|
||||
method: 'GET',
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** POST /app/home/create — 创建家庭(返回 RVoid,需再拉列表取 ID) */
|
||||
|
||||
export function createHome(data: { homeName: string }) {
|
||||
|
||||
return request<void>({
|
||||
|
||||
url: '/app/home/create',
|
||||
|
||||
method: 'POST',
|
||||
|
||||
data,
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function isDefaultHome(home: AppHomeVo): boolean {
|
||||
|
||||
const value = home.isDefault
|
||||
|
||||
return value === '1' || value === 1 || value === true
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function pickDefaultHome(homes: AppHomeVo[] | undefined): AppHomeVo | undefined {
|
||||
|
||||
if (!homes?.length) return undefined
|
||||
|
||||
return homes.find(isDefaultHome) ?? homes[0]
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function getHomeId(home?: AppHomeVo): string {
|
||||
|
||||
return parseId(home?.id)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function findHomeById(homes: AppHomeVo[] | undefined, homeId: string | number) {
|
||||
|
||||
67
src/api/location.ts
Normal file
67
src/api/location.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { request } from '@/api/request'
|
||||
import { resolveValidHomeId } from '@/api/home'
|
||||
import { mapAppLocationVo, type AppLocationVo, type RoomOption } from '@/types/location'
|
||||
import { ApiError } from '@/utils/api-error'
|
||||
|
||||
/** GET /app/location/list/{homeId} — 查询指定家庭的所有房间位置 */
|
||||
export function fetchLocationList(homeId: string | number) {
|
||||
return request<AppLocationVo[]>({
|
||||
url: `/app/location/list/${homeId}`,
|
||||
method: 'GET',
|
||||
})
|
||||
}
|
||||
|
||||
/** 拉取并映射房间列表(每次直接请求接口,不做本地缓存) */
|
||||
export async function fetchRoomList(homeId?: string | number): Promise<RoomOption[]> {
|
||||
const validHomeId = await resolveValidHomeId(homeId)
|
||||
if (!validHomeId) {
|
||||
throw new ApiError('未找到家庭信息')
|
||||
}
|
||||
const list = await fetchLocationList(validHomeId)
|
||||
return (list ?? [])
|
||||
.map(mapAppLocationVo)
|
||||
.filter((item): item is RoomOption => item != null)
|
||||
}
|
||||
|
||||
/** POST /app/location/create — 创建新的房间位置 */
|
||||
export async function createLocation(data: {
|
||||
homeId: string | number
|
||||
locationName: string
|
||||
icon?: string
|
||||
sortOrder?: number
|
||||
}) {
|
||||
const validHomeId = await resolveValidHomeId(data.homeId)
|
||||
if (!validHomeId) {
|
||||
throw new ApiError('未找到家庭信息')
|
||||
}
|
||||
return request<void>({
|
||||
url: '/app/location/create',
|
||||
method: 'POST',
|
||||
data: {
|
||||
...data,
|
||||
homeId: validHomeId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/** POST /app/location/update — 修改房间位置 */
|
||||
export function updateLocation(data: {
|
||||
locationId: string | number
|
||||
locationName: string
|
||||
icon?: string
|
||||
sortOrder?: number
|
||||
}) {
|
||||
return request<void>({
|
||||
url: '/app/location/update',
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/** DELETE /app/location/{locationId} — 删除房间位置 */
|
||||
export function deleteLocation(locationId: string | number) {
|
||||
return request<void>({
|
||||
url: `/app/location/${locationId}`,
|
||||
method: 'DELETE',
|
||||
})
|
||||
}
|
||||
53
src/api/message.ts
Normal file
53
src/api/message.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { request } from './request'
|
||||
import type { AppMessageVo, MessageListQuery, MessageTableData } from '@/types/message'
|
||||
|
||||
export interface MessagePageResult {
|
||||
rows: AppMessageVo[]
|
||||
total: number
|
||||
}
|
||||
|
||||
function normalizePageResult(
|
||||
data: AppMessageVo[] | MessageTableData<AppMessageVo> | undefined,
|
||||
): MessagePageResult {
|
||||
if (!data) return { rows: [], total: 0 }
|
||||
if (Array.isArray(data)) return { rows: data, total: data.length }
|
||||
const rows = data.rows ?? []
|
||||
return { rows, total: data.total ?? rows.length }
|
||||
}
|
||||
|
||||
/** GET /app/message/list — 分页查询当前用户的消息列表 */
|
||||
export function fetchMessagePage(query: MessageListQuery = {}) {
|
||||
return request<AppMessageVo[] | MessageTableData<AppMessageVo>>({
|
||||
url: '/app/message/list',
|
||||
method: 'GET',
|
||||
data: {
|
||||
pageNum: 1,
|
||||
pageSize: 20,
|
||||
...query,
|
||||
},
|
||||
}).then(normalizePageResult)
|
||||
}
|
||||
|
||||
/** GET /app/message/unreadCount — 未读消息数量 */
|
||||
export function fetchUnreadMessageCount() {
|
||||
return request<number>({
|
||||
url: '/app/message/unreadCount',
|
||||
method: 'GET',
|
||||
})
|
||||
}
|
||||
|
||||
/** PUT /app/message/read/{messageId} — 标记单条消息为已读 */
|
||||
export function markMessageRead(messageId: string) {
|
||||
return request<void>({
|
||||
url: `/app/message/read/${encodeURIComponent(messageId)}`,
|
||||
method: 'PUT',
|
||||
})
|
||||
}
|
||||
|
||||
/** PUT /app/message/readAll — 标记全部消息为已读 */
|
||||
export function markAllMessagesRead() {
|
||||
return request<void>({
|
||||
url: '/app/message/readAll',
|
||||
method: 'PUT',
|
||||
})
|
||||
}
|
||||
61
src/api/profile.ts
Normal file
61
src/api/profile.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { API_CLIENT_ID, request } from '@/api/request'
|
||||
import type { ApiResponse } from '@/types/api'
|
||||
import type { AppUserProfileVo, AvatarUploadVo, UpdateProfileBo } from '@/types/profile'
|
||||
import { getStoredToken } from '@/utils/auth-token'
|
||||
import { ApiError, isApiSuccess, unwrapApiData } from '@/utils/api-error'
|
||||
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE_URL ?? ''
|
||||
|
||||
/** GET /app/profile — 获取当前用户资料 */
|
||||
export function fetchProfile() {
|
||||
return request<AppUserProfileVo>({
|
||||
url: '/app/profile',
|
||||
method: 'GET',
|
||||
})
|
||||
}
|
||||
|
||||
/** POST /app/profile/update — 修改用户资料 */
|
||||
export function updateProfile(data: UpdateProfileBo) {
|
||||
return request<void>({
|
||||
url: '/app/profile/update',
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/** POST /app/profile/avatar — 上传用户头像 */
|
||||
export function uploadProfileAvatar(filePath: string): Promise<AvatarUploadVo> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = getStoredToken()
|
||||
uni.uploadFile({
|
||||
url: `${BASE_URL}/app/profile/avatar`,
|
||||
filePath,
|
||||
name: 'file',
|
||||
header: {
|
||||
clientId: API_CLIENT_ID,
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
reject(new ApiError(`上传失败(${res.statusCode})`, res.statusCode))
|
||||
return
|
||||
}
|
||||
let body: ApiResponse<AvatarUploadVo>
|
||||
try {
|
||||
body = JSON.parse(res.data) as ApiResponse<AvatarUploadVo>
|
||||
} catch {
|
||||
reject(new ApiError('上传响应解析失败'))
|
||||
return
|
||||
}
|
||||
if (!isApiSuccess(body)) {
|
||||
reject(new ApiError(body.msg || body.message || '上传失败', body.code))
|
||||
return
|
||||
}
|
||||
resolve(unwrapApiData(body))
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new ApiError(err.errMsg || '上传失败'))
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -2,9 +2,40 @@ import type { ApiResponse } from '@/types/api'
|
||||
import { extractAuthToken, getStoredToken, saveStoredToken } from '@/utils/auth-token'
|
||||
import { ApiError, isApiSuccess, unwrapApiData } from '@/utils/api-error'
|
||||
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE_URL ?? ''
|
||||
/** H5 开发走 Vite 代理;App/小程序使用完整后端地址 */
|
||||
function resolveBaseUrl(): string {
|
||||
const envUrl = import.meta.env.VITE_API_BASE_URL ?? ''
|
||||
// #ifdef H5
|
||||
if (import.meta.env.DEV) return ''
|
||||
// #endif
|
||||
return envUrl
|
||||
}
|
||||
|
||||
const BASE_URL = resolveBaseUrl()
|
||||
/** RuoYi-Vue-Plus App 端 clientId(请求头 clientid,与 Postman 一致) */
|
||||
export const API_CLIENT_ID = import.meta.env.VITE_API_CLIENT_ID || 'app'
|
||||
export const API_CLIENT_ID =
|
||||
import.meta.env.VITE_API_CLIENT_ID || '428a8310cd442757ae699df5d894f051'
|
||||
|
||||
/** 将响应 JSON 中的 int64 主键字段转为字符串,避免 JS 精度丢失 */
|
||||
function parseJsonPreserveIdFields(text: string): unknown {
|
||||
const safe = text.replace(
|
||||
/"(deviceId|homeId|locationId|productId|sessionId|messageId|alertId|seq|id)"\s*:\s*(\d+)/g,
|
||||
'"$1":"$2"',
|
||||
)
|
||||
return JSON.parse(safe)
|
||||
}
|
||||
|
||||
function parseResponseBody(data: unknown): unknown {
|
||||
if (typeof data === 'string') {
|
||||
if (!data) return {}
|
||||
try {
|
||||
return parseJsonPreserveIdFields(data)
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
return data ?? {}
|
||||
}
|
||||
|
||||
export interface RequestOptions extends Omit<UniApp.RequestOptions, 'url'> {
|
||||
url: string
|
||||
@@ -28,7 +59,7 @@ function handleSuccess<T>(
|
||||
saveTokenFromBody = false,
|
||||
) {
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
const body = res.data as ApiResponse | undefined
|
||||
const body = parseResponseBody(res.data) as ApiResponse | undefined
|
||||
reject(
|
||||
new ApiError(
|
||||
body?.msg || body?.message || `请求失败(${res.statusCode})`,
|
||||
@@ -38,7 +69,7 @@ function handleSuccess<T>(
|
||||
return
|
||||
}
|
||||
|
||||
const body = (res.data ?? {}) as ApiResponse<T> & Record<string, unknown>
|
||||
const body = parseResponseBody(res.data) as ApiResponse<T> & Record<string, unknown>
|
||||
if (!isApiSuccess(body)) {
|
||||
reject(new ApiError(body.msg || body.message || '请求失败', body.code))
|
||||
return
|
||||
@@ -58,6 +89,7 @@ export function request<T = unknown>(options: RequestOptions): Promise<T> {
|
||||
...options,
|
||||
url: `${BASE_URL}${options.url}`,
|
||||
header: buildAuthHeaders(options.header),
|
||||
dataType: 'text',
|
||||
success: (res) => handleSuccess<T>(res, resolve, reject),
|
||||
fail: (err) => {
|
||||
reject(new ApiError(err.errMsg || '网络异常'))
|
||||
@@ -73,6 +105,7 @@ export function requestLogin<T = unknown>(options: RequestOptions): Promise<T> {
|
||||
...options,
|
||||
url: `${BASE_URL}${options.url}`,
|
||||
header: buildAuthHeaders(options.header),
|
||||
dataType: 'text',
|
||||
success: (res) => handleSuccess<T>(res, resolve, reject, true),
|
||||
fail: (err) => {
|
||||
reject(new ApiError(err.errMsg || '网络异常'))
|
||||
|
||||
Reference in New Issue
Block a user