Browse Source

[完善]网盘基本功能完善

master
陈鹏飞 5 years ago
parent
commit
7de4c937e0
  1. 2
      package.json
  2. 2
      proxy.config.json
  3. 5
      src/app/http-interceptors/base-interceptor.ts
  4. 62
      src/app/is-login.service.ts
  5. 2
      src/app/navigation/navigation.component.ts

2
package.json

@ -3,7 +3,7 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.config.json --open --port 4200 ",
"start": "ng serve --proxy-config proxy.config.json --open --port 4100 ",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",

2
proxy.config.json

@ -1,6 +1,6 @@
{
"/api": {
"target": "http://39.106.78.171:8008",
"target": "http://192.168.1.250",
"secure": false,
"changeOrigin": true
}

5
src/app/http-interceptors/base-interceptor.ts

@ -44,10 +44,7 @@ export class BaseInterceptor implements HttpInterceptor {
private handleError(error: HttpErrorResponse) {
// 用户认证失败返回登录页
if (error.status === 401||error.status === 614) {
this.token.delete()
sessionStorage.clear()
window.localStorage.clear()
this.router.navigate(['/login'])
const config = new MatSnackBarConfig();
config.verticalPosition = 'top';
config.duration = 3000

62
src/app/is-login.service.ts

@ -1,13 +1,71 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class IsLoginService {
constructor() { }
constructor(private http: HttpClient,) { }
isLogin:boolean = false; //登录状态
xxx:any
//下载
download (e) {
let file = e
let fileSize = file.fileLength //下载文件的总大小
let shardSize = 10 * 1024 * 1024 //文件大小是否大于10MB
if (file && fileSize<=shardSize) { //<=10MB时直接下载
this.http.get(`/api/Objects/PlanPlatform/${file.objectName}`,{responseType: 'blob'},).subscribe(data=>{
let url = window.URL.createObjectURL(new Blob([data])); //createObjectURL创建一个下载Blob的url地址
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", e.fileName);
document.body.appendChild(link);
link.click();
})
} else if (file && fileSize>shardSize) { //>10MB时分块下载
this.blockingDownload(e) //分段下载
}
}
//分段下载并合并
async blockingDownload (e) {
let file = e
let fileSize = file.fileLength //下载文件的总大小
let shardSize = 3 * 1024 * 1024 //3MB一个分片
let allSlice = Math.ceil(fileSize / shardSize) //总文件/3MB===共分多少段
let allFile:any = [] //所有的file分段
for (let i=0;i<allSlice;i++) {
let start = i * shardSize //每次下载文件开始位置
let end = Math.min(fileSize, start + shardSize-1); //每次下载文件结束为止
let result = await new Promise ((result,reject)=>{
this.http.get(`/api/Objects/PlanPlatform/${file.objectName}`,{headers:{'range':`bytes= ${start}-${end}`},responseType:'blob'}).subscribe(data=>{
result(data) })
})
allFile.push(result)
if (allFile.length === allSlice) { //合并文件输出给浏览器
let url = window.URL.createObjectURL(new Blob(allFile)); //createObjectURL创建一个下载Blob的url地址
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", e.fileName);
document.body.appendChild(link);
link.click();
}
} //for循环
}
}

2
src/app/navigation/navigation.component.ts

@ -48,7 +48,7 @@ export class NavigationComponent implements OnInit {
);
}
xxx(e){
this.sss.xxx = e
}
}

Loading…
Cancel
Save