Stackedit/src/services/providers/helpers/customHelper.js
2022-07-10 11:48:28 +08:00

70 lines
1.8 KiB
JavaScript
Raw 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 md5 from 'js-md5';
import networkSvc from '../../networkSvc';
import store from '../../../store';
import userSvc from '../../userSvc';
import badgeSvc from '../../badgeSvc';
import utils from '../../utils';
/**
* 自定义账号前缀
*/
const subPrefix = 'cs';
export default {
subPrefix,
async addAccount({
name,
uploadUrl,
fileParamName,
customHeaders,
customParams,
resultUrlParam,
}) {
userSvc.addUserInfo({
id: `${subPrefix}:${utils.encodeBase64(name)}`,
name,
imageUrl: '',
});
// Build token object including sub
const token = {
uploadUrl,
fileParamName,
customHeaders,
customParams,
resultUrlParam,
name,
sub: utils.encodeBase64(name),
};
// Add token to smms tokens
store.dispatch('data/addCustomToken', token);
badgeSvc.addBadge('addCustomAccount');
return token;
},
async uploadFile({
token,
file,
}) {
const newFileName = `${md5(await utils.encodeFiletoBase64(file))}.${file.type.split('/')[1]}`;
const newfile = new File([file], newFileName, { type: file.type });
const headers = token.customHeaders || {};
const formData = token.customParams || {};
formData[token.fileParamName] = newfile;
const { body } = await networkSvc.request({
method: 'POST',
url: token.uploadUrl,
headers,
formData,
});
const paramArray = token.resultUrlParam.split('.');
let result = body;
paramArray.forEach((paramName) => {
result = result[paramName];
if (!result) {
store.dispatch('notification/error', `自定义图床上传图片失败响应Body为${JSON.stringify(body)}`);
throw new Error(`自定义图床上传图片失败响应Body为${JSON.stringify(body)}`);
}
});
return result;
},
};