xbccs
Version:
customer service
45 lines (44 loc) • 1.72 kB
JavaScript
import { getEnv } from "./index";
import { GLOBAL_DATA } from "../config";
import { getSignature } from "./context";
export const uploadImage = async (options) => {
var _a;
if (!options.filePath) {
throw new Error('缺少上传图片路径');
}
const env = getEnv();
const { ssl, host } = GLOBAL_DATA.INIT_OPTIONS;
const url = `${ssl ? 'https' : 'http'}://${host}/cs/upload/image`;
if (env === 'uniapp') {
uni.uploadFile({
url,
name: (options === null || options === void 0 ? void 0 : options.name) || 'file',
filePath: options.filePath,
formData: {
uid: ((_a = options.formData) === null || _a === void 0 ? void 0 : _a.uid) || Date.now(),
...options === null || options === void 0 ? void 0 : options.formData,
},
header: {
'x-token': getSignature(),
...options === null || options === void 0 ? void 0 : options.header,
},
success: (res) => {
const data = JSON.parse(res.data);
if (res.statusCode === 200) {
if (data === null || data === void 0 ? void 0 : data.code) {
options.fail && options.fail(data);
}
else {
options.success && options.success(data === null || data === void 0 ? void 0 : data.data);
}
}
else {
options.fail && options.fail(data);
}
},
fail: (err) => {
options.fail && options.fail(err);
}
});
}
};