123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import {
- baseUrl,
- apiPath
- } from '@/sheep/config';
- import {
- t
- } from '@/locale';
- import request from '@/sheep/request';
- const FileApi = {
- // 上传文件
- uploadFile: (file) => {
- // TODO 非繁人:访问令牌的接入;
- const token = uni.getStorageSync('token');
- uni.showLoading({
- title: t('common.uploading'),
- });
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: baseUrl + apiPath + '/infra/file/upload',
- filePath: file,
- name: 'file',
- header: {
- // Accept: 'text/json',
- Accept: '*/*',
- 'tenant-id': '1',
- // Authorization: 'Bearer test247',
- },
- success: (uploadFileRes) => {
- let result = JSON.parse(uploadFileRes.data);
- if (result.error === 1) {
- uni.showToast({
- icon: 'none',
- title: result.msg,
- });
- } else {
- return resolve(result);
- }
- },
- fail: (error) => {
- console.log('上传失败:', error);
- return resolve(false);
- },
- complete: () => {
- uni.hideLoading();
- },
- });
- });
- },
- // 通过图片url获得文件流
- getFileIO: async (fileUrl) => {
- // #ifdef MP-WEIXIN
- return new Promise((resolve, reject) => {
- wx.request({
- url: `${import.meta.env.SHOPRO_BASE_URL}/app-api/infra/file/downloadByUrl`,
- method: 'GET',
- data: {
- fileUrl
- },
- responseType: 'arraybuffer',
- success: (res) => {
- if (res.statusCode === 200) {
- const base64 = wx.arrayBufferToBase64(res.data);
- resolve(base64);
- } else {
- reject(new Error('网络响应不是OK状态'));
- }
- },
- fail: (err) => {
- reject(err);
- }
- });
- });
- // #endif
- // #ifndef MP-WEIXIN
- const response = await fetch(import.meta.env.SHOPRO_BASE_URL + '/app-api/infra/file/downloadByUrl?fileUrl=' + fileUrl, {method: 'GET',})
- if (!response.ok) {
- throw new Error('网络响应不是OK状态');
- }
- const blob = await response.blob();
- return blob;
- // #endif
- },
- };
- export default FileApi;
|