chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
// api-service.uts
|
||||
// 缘友(wxchat)插件 前端 API 服务层
|
||||
// 统一对接 addon/wxchat 后端接口。
|
||||
//
|
||||
// 约定:
|
||||
// - 基础路径前缀:/wxchat (完整 URL 形如 /wxchat/<controller>/<action>)
|
||||
// - 真实网络客户端:@/utlis/http-api.uts 的 httpApi(已内置 token 注入与无感刷新)
|
||||
// - 后端统一响应结构:{ code, message, data, timestamp }
|
||||
// - 成功判定:code == 0(success())或 code == 200(部分自定义接口),其余视为业务失败
|
||||
//
|
||||
// 使用前请确保 utlis/http-api.uts 中的 baseURL 指向正确的后端域名。
|
||||
|
||||
import httpApi, { type IResponse } from '@/utlis/http-api.uts'
|
||||
import { setUserToken, setUserInfo, clearToken } from '@/stores/user.uts'
|
||||
import { IUserInfo } from '@/types/user.uts'
|
||||
|
||||
// ===================== 业务异常 =====================
|
||||
class ApiError extends Error {
|
||||
public code : number
|
||||
constructor(message : string, code : number) {
|
||||
super(message)
|
||||
this.name = 'ApiError'
|
||||
this.code = code
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 基础请求封装 =====================
|
||||
// 统一处理成功/失败:成功返回 data(UTSJSONObject),失败抛出 ApiError
|
||||
function handleResponse(res : IResponse) : UTSJSONObject {
|
||||
const code = res.code ?? 1
|
||||
if (code == 0 || code == 200) {
|
||||
return res.data as UTSJSONObject
|
||||
}
|
||||
throw new ApiError(res.message ?? '请求失败', code)
|
||||
}
|
||||
|
||||
async function getJSON(url : string, data : any = {}, needToken : boolean = false) : Promise<UTSJSONObject> {
|
||||
const res = await httpApi.get(url, data, { url: '', needToken: needToken })
|
||||
return handleResponse(res)
|
||||
}
|
||||
|
||||
async function postJSON(url : string, data : any = {}, needToken : boolean = false) : Promise<UTSJSONObject> {
|
||||
const res = await httpApi.post(url, data, { url: '', needToken: needToken })
|
||||
return handleResponse(res)
|
||||
}
|
||||
|
||||
// 把任意对象安全序列化为目标类型(兼容 APP / 非APP 平台的 JSON 解析差异)
|
||||
function toObject<T>(source : any) : T | null {
|
||||
if (source == null) return null
|
||||
// #ifndef APP
|
||||
const str = JSON.stringify(source)
|
||||
const obj = JSON.parse(str) as T
|
||||
return obj
|
||||
// #endif
|
||||
// #ifdef APP
|
||||
return source as T
|
||||
// #endif
|
||||
}
|
||||
|
||||
// ===================== 1. 认证模块(登录/注册) =====================
|
||||
const auth = {
|
||||
// 1.1 发送短信验证码(登录/注册场景,event: login|register)
|
||||
sendSmsCode(phone : string, event : string = 'login') : Promise<UTSJSONObject> {
|
||||
return postJSON('/wxchat/api/login/sendSmsCode', { phone: phone, event: event }, false)
|
||||
},
|
||||
|
||||
// 1.2 短信验证码登录
|
||||
// 成功后后端返回用户信息(code=0),此处持久化用户信息
|
||||
async smsLogin(mobile : string, captcha : string, event : string = 'login') : Promise<IUserInfo | null> {
|
||||
const data = await postJSON('/wxchat/api/login/smsLogin', { mobile: mobile, captcha: captcha, event: event }, false)
|
||||
// 后端在响应顶层附带 access/refresh token,拦截器已自动持久化;这里仅解析 user_info
|
||||
const info = toObject<IUserInfo>(data.get('user_info'))
|
||||
if (info != null) {
|
||||
setUserInfo(info)
|
||||
}
|
||||
return info
|
||||
},
|
||||
|
||||
// 1.3 本机号码一键登录:携带预凭证 pre_token 换取登录态(token 由拦截器自动持久化)
|
||||
async oneClickLogin(preToken : string) : Promise<UTSJSONObject> {
|
||||
const data = await postJSON('/wxchat/api/login/oneClickLogin', { pre_token: preToken }, false)
|
||||
const info = toObject<IUserInfo>(data.get('user_info'))
|
||||
if (info != null) {
|
||||
setUserInfo(info)
|
||||
}
|
||||
return data
|
||||
},
|
||||
|
||||
// 1.3.1 获取一键登录预凭证(模拟运营商 SDK 下发的本机号码凭证)
|
||||
getOneClickPreToken(phone : string) : Promise<UTSJSONObject> {
|
||||
return postJSON('/wxchat/api/login/getOneClickPreToken', { phone: phone }, false)
|
||||
},
|
||||
|
||||
// 1.4 完善用户信息(新用户首次登录后补全资料)
|
||||
completeUserInfo(profile : any) : Promise<UTSJSONObject> {
|
||||
return postJSON('/wxchat/api/login/completeUserInfo', profile, true)
|
||||
},
|
||||
|
||||
// 1.5 退出登录(清理本地 token / 用户信息)
|
||||
logout() : void {
|
||||
clearToken()
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 2. 用户模块 =====================
|
||||
const user = {
|
||||
profile() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/profile', {}, true) },
|
||||
// 查看他人公开资料(资料卡页)
|
||||
detail(uid : number) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/detail', { uid: uid }, true) },
|
||||
updateProfile(profile : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/user/updateProfile', profile, true) },
|
||||
nearby(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/nearby', params, true) },
|
||||
local() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/local', {}, true) },
|
||||
online() : Promise<UTSJSONObject> { return postJSON('/wxchat/api/user/online', {}, true) },
|
||||
heartbeat() : Promise<UTSJSONObject> { return postJSON('/wxchat/api/user/heartbeat', {}, true) },
|
||||
privacy() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/privacy', {}, true) },
|
||||
updatePrivacy(privacy : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/user/updatePrivacy', privacy, true) },
|
||||
cancelAccount() : Promise<UTSJSONObject> { return postJSON('/wxchat/api/user/cancelAccount', {}, true) },
|
||||
ranking(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/ranking', params, true) }
|
||||
// 用户概要(首页/我的 初始化使用;后端若未实现该 action 会走失败,调用方需自行兜底)
|
||||
summary() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/summary', {}, true) },
|
||||
// 我的钱包(余额/金币/汇总/明细)
|
||||
wallet() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/user/wallet', {}, true) }
|
||||
}
|
||||
|
||||
// ===================== 3. 关注模块 =====================
|
||||
const follow = {
|
||||
// 切换关注状态(关注/取消)。
|
||||
// 后端 Follow::toggleFollow 通过 uid(当前用户) + target_uid(目标) + action(1关注/0取消) 判定,
|
||||
// 注意它从请求参数读取 uid,而非登录态,调用方需传入当前用户 uid。
|
||||
toggle(uid : number, targetUid : number, action : number = 1) : Promise<UTSJSONObject> {
|
||||
return postJSON('/wxchat/api/follow/toggleFollow', { uid: uid, target_uid: targetUid, action: action }, true)
|
||||
},
|
||||
followingList(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/follow/getFollowingList', params, true) },
|
||||
followerList(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/follow/getFollowerList', params, true) },
|
||||
mutualList(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/follow/getMutualList', params, true) }
|
||||
}
|
||||
|
||||
// ===================== 4. 好友模块 =====================
|
||||
const friend = {
|
||||
list(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/friend', params, true) },
|
||||
requests(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/friend/requests', params, true) },
|
||||
create(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/friend/create', data, true) },
|
||||
detail(id : number) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/friend/read', { id: id }, true) },
|
||||
respond(id : number, action : number = 1) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/friend/save', { id: id, action: action }, true) },
|
||||
update(id : number, data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/friend/update', { id: id, ...data }, true) },
|
||||
delete(id : number) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/friend/delete', { id: id }, true) }
|
||||
}
|
||||
|
||||
// ===================== 5. 动态/朋友圈模块 =====================
|
||||
const moment = {
|
||||
// 动态广场列表:scope = recommend|follow|nearby|hot
|
||||
list(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/moment/index', params, true) },
|
||||
detail(id : number) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/moment/red', { id: id }, true) },
|
||||
create(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/moment/create', data, true) },
|
||||
comment(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/moment/comment', data, true) },
|
||||
like(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/moment/like', data, true) },
|
||||
collect(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/moment/collect', data, true) },
|
||||
follow(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/moment/follow', data, true) },
|
||||
collectedList(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/moment/collectedList', params, true) },
|
||||
historyList(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/moment/historyList', params, true) },
|
||||
mine(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/moment/mine', params, true) }
|
||||
upload(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/moment/upload', data, true) }
|
||||
}
|
||||
|
||||
// ===================== 6. 消息模块 =====================
|
||||
const message = {
|
||||
send(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/message/send', data, true) },
|
||||
history(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/message/history', params, true) },
|
||||
// 会话列表(聚合最新一条消息 + 未读 + 对方资料/在线)
|
||||
conversations() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/message/conversations', {}, true) },
|
||||
unread() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/message/unread', {}, true) },
|
||||
recall(id : number) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/message/recall', { id: id }, true) }
|
||||
}
|
||||
|
||||
// ===================== 7. 礼物模块 =====================
|
||||
const gift = {
|
||||
list(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/gift/index', params, true) },
|
||||
send(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/gift/send', data, true) },
|
||||
received(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/gift/received', params, true) }
|
||||
}
|
||||
|
||||
// ===================== 8. 抽奖模块 =====================
|
||||
const lottery = {
|
||||
config() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/lottery/config', {}, true) },
|
||||
draw() : Promise<UTSJSONObject> { return postJSON('/wxchat/api/lottery/draw', {}, true) }
|
||||
}
|
||||
|
||||
// ===================== 8.1 每日任务模块 =====================
|
||||
const task = {
|
||||
// 任务列表(含真实进度与领取状态)
|
||||
list() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/task/index', {}, true) },
|
||||
// 领取任务奖励
|
||||
receive(taskId : number) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/task/receive', { task_id: taskId }, true) }
|
||||
}
|
||||
|
||||
// ===================== 9. 推荐模块 =====================
|
||||
const recommend = {
|
||||
index(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/recommend/index', params, true) }
|
||||
}
|
||||
|
||||
// ===================== 10. 搜索模块 =====================
|
||||
const search = {
|
||||
index(params : any = {}) : Promise<UTSJSONObject> { return getJSON('/wxchat/api/search/index', params, true) }
|
||||
}
|
||||
|
||||
// ===================== 11. 未读消息模块 =====================
|
||||
const unread = {
|
||||
count() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/unread/count', {}, true) },
|
||||
read(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/unread/read', data, true) }
|
||||
}
|
||||
|
||||
// ===================== 12. 应用基础配置模块 =====================
|
||||
const app = {
|
||||
appConf() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/index/appConf', {}, false) },
|
||||
launchData() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/index/launchData', {}, false) },
|
||||
baseData() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/index/baseData', {}, false) }
|
||||
}
|
||||
|
||||
// ===================== 13. 意见反馈模块 =====================
|
||||
const feedback = {
|
||||
// 提交反馈
|
||||
save(data : any) : Promise<UTSJSONObject> { return postJSON('/wxchat/api/feedback/save', data, true) },
|
||||
// 我的反馈列表
|
||||
list() : Promise<UTSJSONObject> { return getJSON('/wxchat/api/feedback/index', {}, true) }
|
||||
}
|
||||
|
||||
// ===================== 统一导出 =====================
|
||||
const Api = {
|
||||
auth, user, follow, friend, moment, message, gift, lottery, task, recommend, search, unread, app, feedback
|
||||
}
|
||||
|
||||
export default Api
|
||||
export { Api, ApiError }
|
||||
export type { IUserInfo }
|
||||
Reference in New Issue
Block a user