upload.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. let data = that.data.originFiles.find(e => e.name === res.filename);
  72. if (data) {
  73. data.status = '';
  74. data.attachmentids = rs.data.attachmentids[0]
  75. }
  76. that.setData({
  77. originFiles: that.data.originFiles
  78. })
  79. }).catch(err => {
  80. console.log(err)
  81. })
  82. }
  83. })
  84. },
  85. async handleRemove(data) {
  86. let file = data.detail.file;
  87. if (file.linksid) {
  88. const res = await api._post({
  89. "classname": "system.attachment.Attachment",
  90. "method": "deleteFileLink",
  91. "content": {
  92. "linksids": [file.linksid]
  93. }
  94. })
  95. if (res.code == '1') this.setData({
  96. originFiles: this.data.originFiles.filter(e => e.linksid !== file.linksid),
  97. })
  98. } else {
  99. waitBindings = waitBindings.filter(e => e !== file.attachmentids);
  100. this.setData({
  101. originFiles: this.data.originFiles.filter(e => e.attachmentids !== file.attachmentids),
  102. })
  103. }
  104. },
  105. /* 请求类型 */
  106. requestType(file) {
  107. var index = file.url.lastIndexOf(".");
  108. var ext = file.url.substr(index + 1);
  109. //文件名称
  110. return {
  111. "classname": "system.attachment.huawei.OBS",
  112. "method": "getFileName",
  113. "content": {
  114. "filename": file.name ? file.name.concat(Date.now(), ".").concat(ext) : Date.now() + '.' + ext,
  115. "filetype": ext,
  116. "parentid": this.data.parentid
  117. }
  118. }
  119. },
  120. handleBind() {
  121. let arr = JSON.parse(JSON.stringify(waitBindings));
  122. waitBindings = [];
  123. return arr;
  124. }
  125. }
  126. })