upload.js 3.5 KB

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