|
@@ -0,0 +1,209 @@
|
|
|
+import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
|
|
+import { ToastController, IonicModule } from '@ionic/angular';
|
|
|
+import * as Parse from 'parse';
|
|
|
+// import * as qiniu from 'qiniu-js'
|
|
|
+import { createDirectUploadTask, createMultipartUploadV2Task, FileData } from 'qiniu-js';
|
|
|
+@Component({
|
|
|
+ selector: 'app-upload',
|
|
|
+ templateUrl: './upload.component.html',
|
|
|
+ styleUrls: ['./upload.component.scss'],
|
|
|
+ standalone: true,
|
|
|
+ imports: [IonicModule],
|
|
|
+})
|
|
|
+export class UploadComponent implements OnInit {
|
|
|
+ @Input('title') title: string = '上传文件';
|
|
|
+ @Input('files') files: Array<any> = [];
|
|
|
+ @Input('boxWidth') boxWidth: number = 390; //盒子宽度
|
|
|
+ @Input('fileWidth') fileWidth: number = 86; //图片高度
|
|
|
+ @Input('fileHeight') fileHeight: number = 86; //图片宽度
|
|
|
+
|
|
|
+ @Input('maxlenght') maxlenght: number = 1; //文件数量限制
|
|
|
+ @Input('type') type: string = 'file';
|
|
|
+ @Input('size') size: number = 2048; //上传文件限制大小单位KB
|
|
|
+ @Output() change: EventEmitter<any> = new EventEmitter<any>();
|
|
|
+
|
|
|
+ showBlockNum: number = 4; //每行显示数量
|
|
|
+ // uptoken: string = '';
|
|
|
+ // domain: string = '';
|
|
|
+ // bucket: string = '';
|
|
|
+ get accept() {
|
|
|
+ let type;
|
|
|
+ switch (this.type) {
|
|
|
+ case 'image':
|
|
|
+ type = 'image/*';
|
|
|
+ break;
|
|
|
+ case 'pdf':
|
|
|
+ type = 'application/pdf';
|
|
|
+ break;
|
|
|
+ case 'audio':
|
|
|
+ type = 'audio/*';
|
|
|
+ break;
|
|
|
+ case 'video':
|
|
|
+ type = 'video/*';
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ type = 'file';
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return type;
|
|
|
+ }
|
|
|
+ disabled: boolean = false;
|
|
|
+ company: string = localStorage.getItem('company') || 'Qje9D4bqol';
|
|
|
+ config: { uptoken: string; domain: string; bucket: string } = {
|
|
|
+ uptoken: '',
|
|
|
+ domain: '',
|
|
|
+ bucket: '',
|
|
|
+ };
|
|
|
+ fileList: any = [
|
|
|
+ // {
|
|
|
+ // name: 'xxx.png',
|
|
|
+ // status: 'done',
|
|
|
+ // url: 'http://www.baidu.com/xxx.png'
|
|
|
+ // },
|
|
|
+ ];
|
|
|
+ Previewfilelist: any; //预览图片数组
|
|
|
+
|
|
|
+ constructor(public toastController: ToastController) {
|
|
|
+ Parse.Cloud.run('qiniu_uptoken', { company: this.company }).then((data) => {
|
|
|
+ this.config = {
|
|
|
+ uptoken: data.uptoken,
|
|
|
+ domain: data.domain,
|
|
|
+ bucket: data.bucket,
|
|
|
+ };
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ /* 计算文件布局 */
|
|
|
+ let n = Math.floor(this.boxWidth / this.fileWidth);
|
|
|
+ console.log(n);
|
|
|
+ console.log(this.boxWidth);
|
|
|
+ if (n * this.fileWidth + (n - 1) * 10 > this.boxWidth) {
|
|
|
+ this.showBlockNum = n - 1;
|
|
|
+ } else {
|
|
|
+ this.showBlockNum = n;
|
|
|
+ }
|
|
|
+ this.fileList = this.files.map((item: any) => {
|
|
|
+ // console.log(item);
|
|
|
+ return {
|
|
|
+ url: item?.url,
|
|
|
+ name: item?.name,
|
|
|
+ type: 'link',
|
|
|
+ status: 'done',
|
|
|
+ };
|
|
|
+ });
|
|
|
+ this.Previewfilelist = this.fileList;
|
|
|
+ }
|
|
|
+ onDelete(index:number){
|
|
|
+ console.log(index);
|
|
|
+ this.fileList.splice(index,1)
|
|
|
+ }
|
|
|
+ async onAdd(e: any) {
|
|
|
+ let files = e.target.files;
|
|
|
+ if (this.fileList.length + files.length > this.maxlenght) {
|
|
|
+ const toast = await this.toastController.create({
|
|
|
+ message: '超出上传文件数量',
|
|
|
+ color: 'warning',
|
|
|
+ duration: 1000,
|
|
|
+ });
|
|
|
+ toast.present();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (let index = 0; index < files.length; index++) {
|
|
|
+ const f = files[index];
|
|
|
+ this.changeFileReader(f);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //转图片格式
|
|
|
+ changeFileReader(file: any) {
|
|
|
+ return new Promise((res) => {
|
|
|
+ const reader: any = new FileReader();
|
|
|
+ reader.readAsDataURL(file);
|
|
|
+ reader.filename = file.name;
|
|
|
+ console.log(file);
|
|
|
+ // filelist.push(file);
|
|
|
+ // 当文件读取成功时执行的函数
|
|
|
+ let _this = this;
|
|
|
+ reader.onload = async function (e: any) {
|
|
|
+ let size = e.total / 1024; //KB
|
|
|
+ let baseUrl = e.target.result;
|
|
|
+ if (size > _this.size) {
|
|
|
+ const toast = await _this.toastController.create({
|
|
|
+ message: file.name + '文件过大',
|
|
|
+ color: 'warning',
|
|
|
+ duration: 1000,
|
|
|
+ });
|
|
|
+ toast.present();
|
|
|
+ res(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ _this.fileList.push({
|
|
|
+ url: baseUrl,
|
|
|
+ size: size,
|
|
|
+ name: file.name || '未知',
|
|
|
+ type: 'local',
|
|
|
+ });
|
|
|
+ res(true);
|
|
|
+ };
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ onUpload(){
|
|
|
+ let fs = this.fileList
|
|
|
+ for (let index = 0; index < fs.length; index++) {
|
|
|
+ const f = fs[index];
|
|
|
+ if(f.type == 'local'){
|
|
|
+ console.log(f);
|
|
|
+ this.onQiniuUpFile(f)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.change.emit(this.fileList)
|
|
|
+ }
|
|
|
+ onQiniuUpFile(file:any){
|
|
|
+ // return new Promise((resolve, reject) => {
|
|
|
+ // console.log("进入了上传")
|
|
|
+ // const fileData: FileData = { type: 'file', data: file.url}
|
|
|
+ const fileData: FileData = { type: 'string', data: file.url }
|
|
|
+ // const fileData: FileData = { type: 'array-buffer', data: new ArrayBuffer(1e3) }
|
|
|
+ const config = {
|
|
|
+ tokenProvider:async () => {
|
|
|
+ return this.config.uptoken
|
|
|
+ }
|
|
|
+ };
|
|
|
+ createDirectUploadTask(fileData, config).start()
|
|
|
+ .then((result:any) => {
|
|
|
+ // 处理任务完成结果
|
|
|
+ console.log(result);
|
|
|
+ let res = JSON.parse(result.result)
|
|
|
+ console.log(`${this.config.domain}${res.key}`)
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ console.error(error);
|
|
|
+ // 处理任务启动失败错误
|
|
|
+ });
|
|
|
+ // const observable = qiniu.createDirectUploadTask(file, qiniuFileKey, token, putExtra, config);
|
|
|
+ // observable.subscribe({
|
|
|
+ // next: (result) => {
|
|
|
+ // // 主要用来展示进度
|
|
|
+ // console.warn(result);
|
|
|
+ // },
|
|
|
+ // error: async (err) => {
|
|
|
+ // // this.$notify('上传图片失败');
|
|
|
+ // await _this.presentToast(`文件上传失败`, 1500, "danger");
|
|
|
+ // console.log(err)
|
|
|
+ // _this.loading.dismiss()
|
|
|
+ // },
|
|
|
+ // complete: (res) => {
|
|
|
+ // console.log("上传完成")
|
|
|
+ // console.log(res.key);
|
|
|
+ // console.log(`${_this.domain}${res.key}`)
|
|
|
+ // resolve(`${_this.domain}${res.key}`)
|
|
|
+ // _this.loading.dismiss()
|
|
|
+ // },
|
|
|
+ // });
|
|
|
+ // }).catch(err => {
|
|
|
+ // console.log(err)
|
|
|
+ // _this.loading.dismiss()
|
|
|
+ // })
|
|
|
+ }
|
|
|
+}
|