@flyriselink/pai-sp-mobile
Version:
pai-sp-mobile 组件库
88 lines (83 loc) • 2.22 kB
JavaScript
import { imageBase64 } from './imageBase64.js'
export function suffixImg(s) {
return imageBase64(fileType(s))
}
export function deleteImg(s) {
return imageBase64('delete')
}
function fileType(s) {
let type = 'unknow'
if (s == '') {
type = 'unknow'
} else if (s == '.jpg' || s == '.jpeg') {
type = 'jpg'
} else if (s == '.png') {
type = 'png'
} else if (s == '.gif') {
type = 'gif'
} else if (s == '.svg') {
type = 'svg'
} else if (s == '.doc' || s == '.docx' || s == '.rtf') {
type = 'doc'
} else if (s == '.xls' || s == '.xlsx') {
type = 'xls'
} else if (s == '.ppt' || s == '.pptx') {
type = 'ppt'
} else if (s == '.pdf') {
type = 'pdf'
} else if (s == '.txt') {
type = 'txt'
} else if (s == '.apk') {
type = 'apk'
} else if (s == '.ai') {
type = 'ai'
} else if (s == '.avi') {
type = 'avi'
} else if (s == '.eps') {
type = 'eps'
} else if (s == '.html') {
type = 'html'
} else if (s == '.mp3') {
type = 'mp3'
} else if (s == '.mp4') {
type = 'mp4'
} else if (s == '.psd') {
type = 'psd'
} else if (s == '.ttf') {
type = 'ttf'
} else if (s == '.zip') {
type = 'zip'
}
return type
}
//封装xhr
export const xhrRequest = (options = {}) => {
const { url, method = "GET", onProgress, data = null } = options
return new Promise((resolve) => {
//创建xhr对象
const xhr = new XMLHttpRequest()
//监听状态改变
xhr.addEventListener("readystatechange", () => {
if (xhr.readyState === xhr.DONE) {
resolve(xhr.response)
// resolve(xhr.responseText)
}
})
//下载监听进度
xhr.addEventListener("progress", (e) => {
//进度根据loaded和total的返回值进行动画显示
// console.log(e.loaded, e.total)
onProgress &&
onProgress({
loaded: e.loaded,
total: e.total,
})
})
//上传监听进度与下载类似
xhr.upload.addEventListener("progress", (e) => { })
//发起http请求
xhr.responseType = 'blob'
xhr.open(method, url, true)
xhr.send(data)
})
}