upload.js 3.6 KB

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