index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const _Http = getApp().globalData.http;
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. /* 最大上传数量 */
  8. maxCount: {
  9. type: String,
  10. value: 9
  11. },
  12. /* 文件上传类型限制 all media image file video */
  13. accept: {
  14. type: String,
  15. value: "all"
  16. },
  17. /* 上传回调 */
  18. uploadCallback: {
  19. type: Function
  20. },
  21. /* 文件夹ID */
  22. parentid: {
  23. type: String,
  24. value: wx.getStorageSync('siteP').appfolderid
  25. },
  26. changeState: {
  27. type: Function
  28. }
  29. },
  30. /**
  31. * 组件的初始数据
  32. */
  33. data: {
  34. fileList: []
  35. },
  36. /**
  37. * 组件的方法列表
  38. */
  39. methods: {
  40. /* 上传文件 */
  41. afterRead(event) {
  42. this.triggerEvent("changeState", true)
  43. for (let i = 0; i < event.detail.file.length; i++) {
  44. // 初始化数据
  45. let that = this,
  46. data = this.requestType(event.detail.file[i]);
  47. data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
  48. //发送请求
  49. wx.getFileSystemManager().readFile({
  50. filePath: event.detail.file[i].url,
  51. success: result => {
  52. //返回临时文件路径
  53. const fileData = result.data;
  54. _Http.basic(data).then(res => {
  55. if (res.msg == "成功") {
  56. that.uploadFile(res.data, fileData)
  57. } else {
  58. wx.showToast({
  59. title: `${fileData.filename}.${fileData.serialfilename}`,
  60. icon: "none"
  61. })
  62. }
  63. })
  64. },
  65. fail: console.error
  66. })
  67. }
  68. },
  69. /* 请求类型 */
  70. requestType(file) {
  71. //获取文件后缀
  72. var index = file.url.lastIndexOf(".");
  73. var ext = file.url.substr(index + 1);
  74. //文件名称
  75. return {
  76. "classname": "system.attachment.huawei.OBS",
  77. "method": "getFileName",
  78. "content": {
  79. "filename": file.name,
  80. "filetype": ext,
  81. "parentid": this.data.parentid
  82. }
  83. }
  84. },
  85. /* 上传成功反馈 */
  86. uploadFile(res, data) {
  87. var that = this;
  88. wx.request({
  89. url: res.uploadurl,
  90. method: "PUT",
  91. data: data,
  92. header: {
  93. 'content-type': 'application/octet-stream'
  94. },
  95. success(a) {
  96. _Http.basic({
  97. "classname": "system.attachment.huawei.OBS",
  98. "method": "uploadSuccess",
  99. "content": {
  100. "serialfilename": res.serialfilename
  101. }
  102. }).then(s => {
  103. console.log("文件上传反馈", s)
  104. // if (s.msg != "成功") return;
  105. that.triggerEvent("uploadCallback", s.data.attachmentids);
  106. that.triggerEvent("changeState", false)
  107. }).catch(err => {
  108. console.log(err)
  109. })
  110. }
  111. })
  112. },
  113. }
  114. })