Files
xk-doctor-wx/common/js/select-avatar-image.js
李琦 27202c0740 1. 优化了一些交互,中药药品编辑交互
2. dev和生产环境统一鉴别
2026-05-30 13:53:14 +08:00

61 lines
1.5 KiB
JavaScript

import { prepareImagePath } from '@/common/js/image-compress.js';
export function chooseAvatarImage(options = {}) {
const {
uploadUrl = 'https://api.xiaokang88.com/open-api/upload/old-admin-img',
formData = { store_id: '11001' },
onSuccess,
onFail,
onCancel,
} = options;
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
uni.showLoading({ title: '请稍后...' });
try {
const tempFile = res.tempFiles[0];
const prepared = await prepareImagePath({
path: tempFile.path,
size: tempFile.size,
});
if (!prepared) {
uni.hideLoading();
onCancel && onCancel();
return;
}
uni.uploadFile({
url: uploadUrl,
filePath: prepared.path,
name: 'file',
header: {
authorization: `Bearer ${uni.getStorageSync('token')}`,
},
formData,
success: (uploadRes) => {
uni.hideLoading();
onSuccess && onSuccess(uploadRes);
},
fail: (error) => {
uni.hideLoading();
onFail && onFail(error);
},
});
} catch (error) {
uni.hideLoading();
onFail && onFail(error);
}
},
complete: (res) => {
if (res.errMsg === 'chooseImage:fail cancel') {
onCancel && onCancel();
}
},
});
}