d2p-extends
Version:
d2-crud-plus的扩展
62 lines (61 loc) • 1.41 kB
JavaScript
import ajax from './ajax'
import lodash from 'lodash'
import log from '../../../utils/util.log'
export default {
options: undefined,
init (options) {
this.options = options
},
/**
*
* @param option {file,filename,action,data,headers}
* @param config
* @returns {Promise<unknown>}
*/
async upload ({ file, fileName, onProgress, onError, config }) {
const options = lodash.cloneDeep(this.options)
lodash.merge(options, config)
config = options
const option = {
file,
onProgress,
onError,
...config
}
let key = config.buildKey(fileName, {
file,
...(config.custom || {})
})
if (key instanceof Promise) {
key = await key
}
if (options.data == null) {
options.data = {}
}
options.data.key = key
log.debug('upload option ', options)
return new Promise((resolve, reject) => {
// onProgress({
// total: 0,
// percent: 0
// })
ajax(option,
async (res) => {
try {
const url = await config.successHandle(res, option)
if (url && typeof url === 'object' && url.key == null) {
url.key = key
}
resolve(url)
} catch (e) {
onError(e)
reject(e)
}
},
(e) => {
onError(e)
reject(e)
})
})
}
}