123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import Api from "../../model/api";
- import Route from "../../model/route";
- import {getDataSet, showLoading, toast} from "../../utils/utils";
- import Upload from "../../model/upload";
- import Config from "../../model/config";
- Page({
- data: {
- xcpArr: [],
- pid: 0,
- barTitle: '文件上传',
- teamId: ''
- },
- async onLoad(options) {
- let {pid, title, teamId} = options;
- if (!pid) {
- pid = 0
- }
- wx.setNavigationBarTitle({title})
- this.setData({
- pid,
- teamId
- }, async () => {
- showLoading();
- await this.getData();
- wx.hideLoading();
- })
- },
- async getData() {
- const res = await Api.getUploadFile({pid: this.data.pid, teamId: this.data.teamId});
- if (res.data) {
- res.data.forEach(item => {
- item.size = this.formatSize(item.fileSize)
- })
- this.setData({
- xcpArr: res.data,
- })
- }
- },
- formatSize(fileSize) {
- let newSize = fileSize + "b";
- if (fileSize > 1024) {
- newSize = (fileSize / 1024).toFixed(1);
- fileSize = newSize + "kb"
- }
- if (newSize > 1024) {
- newSize = (newSize / 1024).toFixed(1);
- fileSize = newSize + "mb"
- }
- if (newSize > 1024) {
- newSize = (newSize / 1024).toFixed(1);
- fileSize = "大于1gb"
- }
- return fileSize
- },
- clickDir(e) {
- let item = getDataSet(e, "item");
- Route.toUploadFile(item.id, item.fileName, this.data.teamId)
- },
- async deleteFile(e) {
- let item = getDataSet(e, "item");
- wx.showModal({
- title: `确认删除${item.fileName}?`,
- success: async res => {
- if (!res.confirm) {
- return;
- }
- showLoading()
- await Api.deleteFile(item.id)
- await this.getData();
- wx.hideLoading();
- }
- });
- },
- async upload(e) {
- if (this.data.pid == 0) {
- toast("根目录不允许上传文件")
- return;
- }
- try {
- const data = await wx.chooseMessageFile({count: 1,})
- data.tempFiles[0].url = data.tempFiles[0].path;
- const file = data.tempFiles[0];
- showLoading()
- await Upload.uploadRes(Config.UPLOAD_PATH.CLASS_DIR, file);
- await Api.uploadFile({
- id: null,
- filePath: file.url,
- fileSize: file.size,
- fileName: file.name,
- fileType: '文件',
- fileExtension: file.fileExtension,
- pid: this.data.pid,
- teamId: this.data.teamId
- })
- await this.getData();
- wx.hideLoading();
- } catch (e) {
- console.log(e)
- if (e.errMsg == 'chooseMessageFile:fail cancel') {
- } else {
- toast("只支持选择手机文件")
- }
- }
- }
- })
|