Files

241 lines
7.0 KiB
JavaScript
Raw Permalink Normal View History

2026-06-05 12:28:36 +08:00
import { req } from '@/common/js/index.js';
const SALESPERSON_PREFIX = '/newApi/salesperson-upload/';
const PLATFORM_PREFIX = '/newApi/platform-admin-upload/';
2026-06-09 17:02:13 +08:00
const CLINIC_ADMIN_PREFIX = '/newApi/clinic-admin-upload/';
const CLINIC_SALESPERSON_PREFIX = '/newApi/clinic-salesperson-upload/';
2026-06-05 12:28:36 +08:00
function uploadChatFileByPrefix(prefix, headerKey, params) {
return req.request({
url: `${prefix}upload-chat-file`,
type: 'upload',
method: 'POST',
filePath: params.filePath,
name: params.name || 'file',
formData: params.formData || {},
header: {
[headerKey]: '1',
},
});
}
2026-06-18 10:20:24 +08:00
function getOssSignatureByPrefix(prefix, headerKey) {
return req.request({
url: `${prefix}oss-signature`,
method: 'GET',
header: {
[headerKey]: '1',
},
});
}
2026-06-18 17:04:50 +08:00
function registerOssFileByPrefix(prefix, headerKey, url, fileSize = 0, fileName = '') {
const data = { url, source: 2, file_size: fileSize }
if (fileName) {
data.file_name = fileName
}
2026-06-18 13:49:43 +08:00
return req.request({
url: `${prefix}register-oss-file`,
method: 'POST',
2026-06-18 17:04:50 +08:00
data,
2026-06-18 13:49:43 +08:00
header: {
[headerKey]: '1',
},
});
}
2026-06-18 17:04:50 +08:00
function registerBusinessOssFile(url, fileSize = 0, fileName = '') {
2026-06-18 13:49:43 +08:00
const mode = uni.getStorageSync('loginMode');
if (mode === 'platform_admin') {
2026-06-18 17:04:50 +08:00
return registerOssFileByPrefix(PLATFORM_PREFIX, '_platformAdmin', url, fileSize, fileName);
2026-06-18 13:49:43 +08:00
}
if (mode === 'clinic_admin') {
2026-06-18 17:04:50 +08:00
return registerOssFileByPrefix(CLINIC_ADMIN_PREFIX, '_clinicAdmin', url, fileSize, fileName);
2026-06-18 13:49:43 +08:00
}
2026-06-18 17:04:50 +08:00
return registerOssFileByPrefix(SALESPERSON_PREFIX, '_salesperson', url, fileSize, fileName);
2026-06-18 13:49:43 +08:00
}
export { registerBusinessOssFile };
2026-06-05 12:28:36 +08:00
/**
* 业务员录入/聊天同款服务端转存 OSS返回 url
*/
export function uploadSalespersonChatFileApi(params) {
return uploadChatFileByPrefix(SALESPERSON_PREFIX, '_salesperson', params);
}
/**
* 平台管理员录入上传与业务员对称
*/
export function uploadPlatformAdminChatFileApi(params) {
return uploadChatFileByPrefix(PLATFORM_PREFIX, '_platformAdmin', params);
}
2026-06-09 17:02:13 +08:00
/**
* 诊所管理员上传结算凭证等
*/
export function uploadClinicAdminChatFileApi(params) {
return uploadChatFileByPrefix(CLINIC_ADMIN_PREFIX, '_clinicAdmin', params);
}
2026-06-05 12:28:36 +08:00
/**
* 按当前 loginMode 选择上传接口
*/
export function uploadBusinessChatFileApi(params) {
const mode = uni.getStorageSync('loginMode');
if (mode === 'platform_admin') {
return uploadPlatformAdminChatFileApi(params);
}
2026-06-09 17:02:13 +08:00
if (mode === 'clinic_admin') {
return uploadClinicAdminChatFileApi(params);
}
2026-06-05 12:28:36 +08:00
return uploadSalespersonChatFileApi(params);
}
2026-06-09 17:02:13 +08:00
/**
* 推广员管理结算凭证上传超管/诊所
*/
export function uploadSalespersonManageFileApi(params) {
const mode = uni.getStorageSync('loginMode');
if (mode === 'platform_admin') {
return uploadPlatformAdminChatFileApi(params);
}
return uploadClinicAdminChatFileApi(params);
}
/**
* 个人中心头像上传xk-api POST {end}-upload/imageUploadController::image
* 按当前 loginMode 选择四端各自的上传路由与请求头标识
* uni.uploadFile 返回的 body 是字符串这里手动 JSON.parse 后按 jok 形态code/result.url解包
* @param {string} filePath 本地临时文件路径
* @returns {Promise<{ok: boolean, url: string|null, message?: string}>}
*/
export function uploadProfileAvatarApi(filePath) {
const mode = uni.getStorageSync('loginMode');
let prefix = CLINIC_ADMIN_PREFIX;
let headerKey = '_clinicAdmin';
if (mode === 'platform_admin') {
prefix = PLATFORM_PREFIX;
headerKey = '_platformAdmin';
} else if (mode === 'salesperson') {
prefix = SALESPERSON_PREFIX;
headerKey = '_salesperson';
} else if (mode === 'clinic_salesperson') {
prefix = CLINIC_SALESPERSON_PREFIX;
headerKey = '_clinicSalesperson';
}
return req.request({
url: `${prefix}image`,
type: 'upload',
method: 'POST',
filePath,
name: 'file',
formData: {},
header: { [headerKey]: '1' },
}).then((res) => {
let body = res;
if (typeof body === 'string') {
try {
body = JSON.parse(body);
} catch (e) {
body = null;
}
}
// 该接口是 xk-api成功 code=0业务数据在 result
if (body && Number(body.code) === 0 && body.result && body.result.url) {
return { ok: true, url: body.result.url };
}
return { ok: false, url: null, message: (body && body.message) || '上传失败' };
});
}
2026-06-05 12:28:36 +08:00
function unwrapUploadResult(res) {
if (res == null) return null;
if (res.result !== undefined && res.result !== null) return res.result;
if (res.data && res.data.result !== undefined) return res.data.result;
return res.data != null ? res.data : res;
}
export function extractUploadUrl(res) {
const inner = unwrapUploadResult(res);
if (!inner) return null;
return inner.url || inner.path || null;
}
2026-06-18 10:20:24 +08:00
/**
* Laravel jok 响应解包 OSS 签名
*/
export function unwrapOssSignature(res) {
const inner = unwrapUploadResult(res);
if (!inner) return null;
if (!inner.accessKeyId || !inner.policy || !inner.signature || !inner.host || !inner.bucket) {
return null;
}
return inner;
}
export function getSalespersonOssSignatureApi() {
return getOssSignatureByPrefix(SALESPERSON_PREFIX, '_salesperson');
}
export function getPlatformAdminOssSignatureApi() {
return getOssSignatureByPrefix(PLATFORM_PREFIX, '_platformAdmin');
}
export function getClinicAdminOssSignatureApi() {
return getOssSignatureByPrefix(CLINIC_ADMIN_PREFIX, '_clinicAdmin');
}
/**
* 按当前 loginMode 获取 OSS 直传签名
*/
export function getBusinessOssSignatureApi() {
const mode = uni.getStorageSync('loginMode');
if (mode === 'platform_admin') {
return getPlatformAdminOssSignatureApi();
}
if (mode === 'clinic_admin') {
return getClinicAdminOssSignatureApi();
}
return getSalespersonOssSignatureApi();
}
2026-06-05 12:28:36 +08:00
const LEGACY_ADMIN_UPLOAD_URL = 'https://api.xiaokang88.com/open-api/upload/old-admin-img';
/**
* 录入/管理端旧版图片上传 d-uploadselect-avatar-image 一致
*/
export function uploadLegacyAdminImage(params) {
const { filePath, name = 'file', formData = { store_id: '11001' } } = params || {};
return new Promise((resolve, reject) => {
uni.uploadFile({
url: LEGACY_ADMIN_UPLOAD_URL,
filePath,
name,
formData,
header: {
authorization: `Bearer ${uni.getStorageSync('token')}`,
},
success: (uploadRes) => {
try {
const data =
typeof uploadRes.data === 'string' ? JSON.parse(uploadRes.data) : uploadRes.data;
if (![200, 201, 204].includes(uploadRes.statusCode)) {
reject(new Error((data && data.msg) || '上传失败'));
return;
}
const url = data && data.data && data.data.url;
if (!url) {
reject(new Error('上传失败'));
return;
}
resolve(url);
} catch (e) {
reject(e);
}
},
fail: reject,
});
});
}