upload.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import api from '../../api/api.js'
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. source: {
  8. value: 'media',
  9. type: String
  10. },
  11. /* 文件夹ID */
  12. parentid: {
  13. type: String,
  14. value: wx.getStorageSync('siteP').appfolderid
  15. }
  16. },
  17. /**
  18. * 组件的初始数据
  19. */
  20. data: {
  21. originFiles: [],
  22. attachmentids: [],
  23. gridConfig: {
  24. column: 5,
  25. width: 120,
  26. height: 120,
  27. },
  28. config: {
  29. count: 1,
  30. },
  31. timer: null,
  32. uploadCount: 0
  33. },
  34. /**
  35. * 组件的方法列表
  36. */
  37. methods: {
  38. handleAdd(file) {
  39. let files = this.data.originFiles.concat(file.detail.files.map(e => {
  40. e.status = 'loading'
  41. return e
  42. }));
  43. this.setData({
  44. uploadCount: files.length,
  45. originFiles: files
  46. })
  47. this.toSetFileData()
  48. },
  49. handleRemove(data) {
  50. let file = data.detail.file;
  51. this.setData({
  52. originFiles: this.data.originFiles.filter(e => e.name !== file.name),
  53. attachmentids: this.data.attachmentids.filter(e => e !== file.attachmentids)
  54. })
  55. },
  56. toSetFileData() {
  57. let files = this.data.originFiles
  58. for (let i = 0; i < files.length; i++) {
  59. // 初始化数据
  60. let that = this,
  61. data = this.requestType(files[i]);
  62. data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
  63. //发送请求
  64. wx.getFileSystemManager().readFile({
  65. filePath: files[i].url,
  66. success: result => {
  67. //返回临时文件路径
  68. const fileData = result.data;
  69. api._post(data).then(res => {
  70. if (res.msg == "成功") {
  71. that.uploadFile(res.data, fileData)
  72. }
  73. })
  74. },
  75. fail: console.error
  76. })
  77. }
  78. return new Promise((reslove, reject) => {
  79. this.data.timer = setInterval(e => {
  80. if (this.data.uploadCount === 0) {
  81. clearInterval(this.data.timer)
  82. reslove()
  83. }
  84. }, 1000)
  85. })
  86. },
  87. /* 请求类型 */
  88. requestType(file) {
  89. var index = file.url.lastIndexOf(".");
  90. var ext = file.url.substr(index + 1);
  91. //文件名称
  92. return {
  93. "classname": "system.attachment.huawei.OBS",
  94. "method": "getFileName",
  95. "content": {
  96. "filename": file.name,
  97. "filetype": ext,
  98. "parentid": this.data.parentid
  99. }
  100. }
  101. },
  102. /* 上传成功反馈 */
  103. uploadFile(res, data) {
  104. var that = this;
  105. wx.request({
  106. url: res.uploadurl,
  107. method: "PUT",
  108. data: data,
  109. header: {
  110. 'content-type': 'application/octet-stream'
  111. },
  112. success(a) {
  113. api._post({
  114. "classname": "system.attachment.huawei.OBS",
  115. "method": "uploadSuccess",
  116. "content": {
  117. "serialfilename": res.serialfilename
  118. }
  119. }).then(rs => {
  120. console.log("上传附件反馈", rs)
  121. that.data.attachmentids.push(rs.data.attachmentids[0])
  122. that.setData({
  123. uploadCount: that.data.uploadCount - 1,
  124. originFiles: that.data.originFiles.map(e => {
  125. if (e.name === res.filename) {
  126. e.status = '';
  127. e.attachmentids = rs.data.attachmentids[0]
  128. }
  129. return e
  130. })
  131. })
  132. }).catch(err => {
  133. console.log(err)
  134. })
  135. }
  136. })
  137. },
  138. }
  139. })