upload.js 4.8 KB

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