|
@@ -47,6 +47,36 @@ export default class NetService extends Service {
|
|
|
return this.fetch(url, { method: 'PUT', body: JSON.stringify(params) }, timeout)
|
|
return this.fetch(url, { method: 'PUT', body: JSON.stringify(params) }, timeout)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载文件
|
|
|
|
|
+ * @param url url|blob
|
|
|
|
|
+ * @param fileName
|
|
|
|
|
+ */
|
|
|
|
|
+ downloadFile(url: string | Blob, fileName: string) {
|
|
|
|
|
+ const download = (blob: Blob, fileName: string) => {
|
|
|
|
|
+ const a = document.createElement('a')
|
|
|
|
|
+ document.body.appendChild(a)
|
|
|
|
|
+ a.style.display = 'none'
|
|
|
|
|
+ // 使用获取到的blob对象创建的url
|
|
|
|
|
+ const url = window.URL.createObjectURL(blob)
|
|
|
|
|
+ a.href = url
|
|
|
|
|
+ // 指定下载的文件名
|
|
|
|
|
+ a.download = fileName
|
|
|
|
|
+ a.click()
|
|
|
|
|
+ document.body.removeChild(a)
|
|
|
|
|
+ // 移除blob对象的url
|
|
|
|
|
+ window.URL.revokeObjectURL(url)
|
|
|
|
|
+ }
|
|
|
|
|
+ return new Promise<void>((resolve, reject) => {
|
|
|
|
|
+ if (url instanceof Blob) {
|
|
|
|
|
+ download(url, fileName)
|
|
|
|
|
+ resolve()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ fetch(url).then((res) => res.blob()).then((blob) => download(blob, fileName)).finally(() => resolve())
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/** token 续期 */
|
|
/** token 续期 */
|
|
|
async refresh(token: string) {
|
|
async refresh(token: string) {
|
|
|
this.post('', { token }).then((res) => {
|
|
this.post('', { token }).then((res) => {
|