upload.js 4.8 KB

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