upload.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import api from '../../api/api.js'
  2. let waitBindings = []; //等待绑定的附件
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. source: {
  9. value: 'media',
  10. type: String
  11. },
  12. bindData: {
  13. value: {},
  14. type: Object
  15. },
  16. /* 文件夹ID */
  17. parentid: {
  18. type: String,
  19. value: wx.getStorageSync('siteP').appfolderid
  20. }
  21. },
  22. data: {
  23. originFiles: [],
  24. gridConfig: {
  25. column: 5,
  26. width: 120,
  27. height: 120,
  28. }
  29. },
  30. lifetimes: {
  31. attached() {
  32. setTimeout(() => {
  33. this.fileData()
  34. }, 1000);
  35. }
  36. },
  37. methods: {
  38. handleAdd(file) {
  39. this.toSetFileData(file.detail.files.map(e => {
  40. e.status = 'loading'
  41. return e
  42. }))
  43. },
  44. toSetFileData(files) {
  45. this.setData({
  46. originFiles: this.data.originFiles.concat(files)
  47. })
  48. for (let i = 0; i < files.length; i++) {
  49. // 初始化数据
  50. let that = this,
  51. data = this.requestType(files[i]);
  52. data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
  53. //发送请求
  54. wx.getFileSystemManager().readFile({
  55. filePath: files[i].url,
  56. success: result => {
  57. //返回临时文件路径
  58. const fileData = result.data;
  59. api._post(data).then(res => {
  60. console.log("文件上传", res)
  61. if (res.msg == "成功") {
  62. that.uploadFile(res.data, fileData, files.length);
  63. }
  64. })
  65. },
  66. fail: console.error
  67. });
  68. }
  69. },
  70. /* 上传成功反馈 */
  71. uploadFile(res, data, count) {
  72. var that = this;
  73. wx.request({
  74. url: res.uploadurl,
  75. method: "PUT",
  76. data: data,
  77. header: {
  78. 'content-type': 'application/octet-stream'
  79. },
  80. success(a) {
  81. api._post({
  82. "classname": "system.attachment.huawei.OBS",
  83. "method": "uploadSuccess",
  84. "content": {
  85. "serialfilename": res.serialfilename
  86. }
  87. }).then(rs => {
  88. console.log('上传成功反馈', rs)
  89. waitBindings.push(rs.data.attachmentids[0] || "");
  90. let data = that.data.originFiles.find(e => e.name === res.filename);
  91. if (data) {
  92. data.status = '';
  93. data.attachmentids = rs.data.attachmentids[0]
  94. }
  95. if (waitBindings.length == count) {
  96. that.filebindData();
  97. that.setData({
  98. originFiles: that.data.originFiles
  99. })
  100. }
  101. }).catch(err => {
  102. console.log(err)
  103. })
  104. }
  105. })
  106. },
  107. handleClick() {
  108. let that = this
  109. wx.chooseMessageFile({
  110. count: 10,
  111. type: 'file',
  112. success(res) {
  113. let arr = res.tempFiles.map(e => {
  114. return {
  115. name: e.name,
  116. url: e.path,
  117. type: e.type,
  118. }
  119. })
  120. that.handleAdd({
  121. detail: {
  122. files: arr
  123. }
  124. })
  125. }
  126. })
  127. },
  128. handleRemove(data) {
  129. let file = {
  130. currentTarget: {
  131. dataset: {
  132. item: data.detail.file
  133. }
  134. }
  135. }
  136. this.deleteFile(file)
  137. },
  138. /* 请求类型 */
  139. requestType(file) {
  140. var index = file.url.lastIndexOf(".");
  141. var ext = file.url.substr(index + 1);
  142. //文件名称
  143. return {
  144. "classname": "system.attachment.huawei.OBS",
  145. "method": "getFileName",
  146. "content": {
  147. "filename": file.name,
  148. "filetype": ext,
  149. "parentid": this.data.parentid
  150. }
  151. }
  152. },
  153. async filebindData() {
  154. this.data.bindData.attachmentids = JSON.parse(JSON.stringify(waitBindings));
  155. const res = await api._post({
  156. "classname": "system.attachment.Attachment",
  157. "method": "createFileLink",
  158. "content": this.data.bindData
  159. })
  160. console.log("绑定", res)
  161. if (res.msg != '成功') return;
  162. this.fileData()
  163. waitBindings = [];
  164. },
  165. async fileData() {
  166. const res = await api._post({
  167. "classname": "system.attachment.Attachment",
  168. "method": "queryFileLink",
  169. "content": this.data.bindData
  170. })
  171. console.log('获取文件', res)
  172. this.setData({
  173. originFiles: res.data
  174. });
  175. },
  176. async deleteFile(data) {
  177. let item = data.currentTarget.dataset.item
  178. const res = await api._post({
  179. "classname": "system.attachment.Attachment",
  180. "method": "deleteFileLink",
  181. "content": {
  182. "linksids": [item.linksid]
  183. }
  184. })
  185. this.fileData()
  186. }
  187. }
  188. })