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