Files
2026-09-23 16:52:48 +08:00

131 lines
3.8 KiB
JavaScript
Raw Permalink 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 { IM_HTTP, OA_BASE } from './config.js'
function unwrap(json) {
if (!json || typeof json !== 'object') return json
const code = json.code
if (code != null && code !== 0 && code !== 200) {
const err = new Error(json.message || '请求失败')
err.status = code
throw err
}
return Object.prototype.hasOwnProperty.call(json, 'data') ? json.data : json
}
export function request(path, { method = 'GET', data, token } = {}) {
const url = path.startsWith('http') ? path : `${IM_HTTP}${path.startsWith('/') ? path : '/' + path}`
return new Promise((resolve, reject) => {
uni.request({
url,
method,
data,
timeout: 20000,
header: {
Accept: 'application/json',
...(data != null ? { 'Content-Type': 'application/json' } : {}),
...(token ? { Authorization: `Bearer ${token}` } : {})
},
success(res) {
const body = res.data
if (res.statusCode >= 400) {
const msg = body && body.message ? body.message : `HTTP ${res.statusCode}`
const err = new Error(msg)
err.status = res.statusCode
reject(err)
return
}
try {
resolve(unwrap(body))
} catch (e) {
reject(e)
}
},
fail(e) {
reject(new Error((e && e.errMsg) || '网络异常'))
}
})
})
}
export function loginOptions() {
return request('/api/im/sms/login-options')
}
export function login(body) {
return request('/api/im/login', { method: 'POST', data: body })
}
export function sendLoginCode(account) {
return request('/api/im/sms/login-code', { method: 'POST', data: { account } })
}
export function changePassword(body, token) {
return request('/api/im/change-password', { method: 'POST', data: body, token })
}
export function bootstrap(token) {
return request('/api/im/bootstrap', { token })
}
export function messages(sessionId, token) {
return request(`/api/im/messages?sessionId=${encodeURIComponent(sessionId)}`, { token })
}
export function markRead(sessionId, token) {
return request('/api/im/read', { method: 'POST', data: { sessionId }, token })
}
export function createGroup(body, token) {
return request('/api/im/groups', { method: 'POST', data: body, token })
}
export function sessionAction(body, token) {
return request('/api/im/sessions/action', { method: 'POST', data: body, token })
}
export function recall(body, token) {
return request('/api/im/recall', { method: 'POST', data: body, token })
}
export function livekitToken(body, token) {
return request('/api/im/livekit/token', { method: 'POST', data: body, token })
}
export function oaGet(path, token) {
return request(`/api/im/oa${path}`, { token })
}
export function oaPost(path, data, token) {
return request(`/api/im/oa${path}`, { method: 'POST', data, token })
}
export function oaPut(path, data, token) {
return request(`/api/im/oa${path}`, { method: 'PUT', data, token })
}
export function absUrl(path) {
if (!path) return ''
const s = String(path)
if (s.startsWith('http') || s.startsWith('data:') || s.startsWith('blob:')) return s
// 证件照 / 附件票在 OA 域名,不能拼到 /im 后面
if (
s.startsWith('/api/org/') ||
s.startsWith('/api/files') ||
s.startsWith('/api/auth/') ||
s.startsWith('/api/profile/') ||
s.startsWith('/api/hr/')
) {
return `${OA_BASE}${s}`
}
const base = IM_HTTP || OA_BASE
return s.startsWith('/') ? `${base}${s}` : `${base}/${s}`
}
/** 证件照完整地址:优先 avatar 票,否则用用户 ID 拼 org/photo(需票,一般走 refreshPeoplePhotos */
export function photoUrl(raw) {
if (!raw) return ''
if (typeof raw === 'string') return absUrl(raw)
const av = raw.avatar || raw.photo || raw.faceUrl || ''
if (av) return absUrl(av)
return ''
}