uploadFile.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Api from "../../model/api";
  2. import Route from "../../model/route";
  3. import {getDataSet, showLoading, toast} from "../../utils/utils";
  4. import Upload from "../../model/upload";
  5. import Config from "../../model/config";
  6. Page({
  7. data: {
  8. xcpArr: [],
  9. pid: 0,
  10. barTitle: '文件上传',
  11. teamId: ''
  12. },
  13. async onLoad(options) {
  14. let {pid, title, teamId} = options;
  15. if (!pid) {
  16. pid = 0
  17. }
  18. wx.setNavigationBarTitle({title})
  19. this.setData({
  20. pid,
  21. teamId
  22. }, async () => {
  23. showLoading();
  24. await this.getData();
  25. wx.hideLoading();
  26. })
  27. },
  28. async getData() {
  29. const res = await Api.getUploadFile({pid: this.data.pid, teamId: this.data.teamId});
  30. if (res.data) {
  31. res.data.forEach(item => {
  32. item.size = this.formatSize(item.fileSize)
  33. })
  34. this.setData({
  35. xcpArr: res.data,
  36. })
  37. }
  38. },
  39. formatSize(fileSize) {
  40. let newSize = fileSize + "b";
  41. if (fileSize > 1024) {
  42. newSize = (fileSize / 1024).toFixed(1);
  43. fileSize = newSize + "kb"
  44. }
  45. if (newSize > 1024) {
  46. newSize = (newSize / 1024).toFixed(1);
  47. fileSize = newSize + "mb"
  48. }
  49. if (newSize > 1024) {
  50. newSize = (newSize / 1024).toFixed(1);
  51. fileSize = "大于1gb"
  52. }
  53. return fileSize
  54. },
  55. clickDir(e) {
  56. let item = getDataSet(e, "item");
  57. Route.toUploadFile(item.id, item.fileName, this.data.teamId)
  58. },
  59. async deleteFile(e) {
  60. let item = getDataSet(e, "item");
  61. wx.showModal({
  62. title: `确认删除${item.fileName}?`,
  63. success: async res => {
  64. if (!res.confirm) {
  65. return;
  66. }
  67. showLoading()
  68. await Api.deleteFile(item.id)
  69. await this.getData();
  70. wx.hideLoading();
  71. }
  72. });
  73. },
  74. async upload(e) {
  75. if (this.data.pid == 0) {
  76. toast("根目录不允许上传文件")
  77. return;
  78. }
  79. try {
  80. const data = await wx.chooseMessageFile({count: 1,})
  81. data.tempFiles[0].url = data.tempFiles[0].path;
  82. const file = data.tempFiles[0];
  83. showLoading()
  84. await Upload.uploadRes(Config.UPLOAD_PATH.CLASS_DIR, file);
  85. await Api.uploadFile({
  86. id: null,
  87. filePath: file.url,
  88. fileSize: file.size,
  89. fileName: file.name,
  90. fileType: '文件',
  91. fileExtension: file.fileExtension,
  92. pid: this.data.pid,
  93. teamId: this.data.teamId
  94. })
  95. await this.getData();
  96. wx.hideLoading();
  97. } catch (e) {
  98. console.log(e)
  99. if (e.errMsg == 'chooseMessageFile:fail cancel') {
  100. } else {
  101. toast("只支持选择手机文件")
  102. }
  103. }
  104. }
  105. })