upload.js 3.3 KB

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