index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. disabled: {
  31. type: Boolean,
  32. value: false
  33. }
  34. },
  35. /**
  36. * 组件的初始数据
  37. */
  38. data: {
  39. fileList: []
  40. },
  41. /**
  42. * 组件的方法列表
  43. */
  44. methods: {
  45. /* 上传文件 */
  46. afterRead(event) {
  47. this.triggerEvent("changeState", true)
  48. for (let i = 0; i < event.detail.file.length; i++) {
  49. // 初始化数据
  50. let that = this,
  51. data = this.requestType(event.detail.file[i]);
  52. data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
  53. //发送请求
  54. wx.getFileSystemManager().readFile({
  55. filePath: event.detail.file[i].url,
  56. success: result => {
  57. //返回临时文件路径
  58. const fileData = result.data;
  59. _Http.basic(data).then(res => {
  60. if (res.code == '1') {
  61. that.uploadFile(res.data, fileData)
  62. } else {
  63. wx.showToast({
  64. title: `${fileData.filename}.${fileData.serialfilename}`,
  65. icon: "none"
  66. })
  67. }
  68. })
  69. },
  70. fail: console.error
  71. })
  72. }
  73. },
  74. /* 请求类型 */
  75. requestType(file) {
  76. //获取文件后缀
  77. var index = file.url.lastIndexOf(".");
  78. var ext = file.url.substr(index + 1);
  79. //文件名称
  80. return {
  81. "classname": "system.attachment.huawei.OBS",
  82. "method": "getFileName",
  83. "content": {
  84. "filename": file.name ? file.name.concat(Date.now(), ".").concat(ext) : Date.now() + '.' + ext,
  85. "filetype": ext,
  86. "parentid": this.data.parentid
  87. }
  88. }
  89. },
  90. /* 上传成功反馈 */
  91. uploadFile(res, data) {
  92. var that = this;
  93. wx.request({
  94. url: res.uploadurl,
  95. method: "PUT",
  96. data: data,
  97. header: {
  98. 'content-type': res.filetype == 'pdf' ? "application/pdf" : 'application/octet-stream'
  99. },
  100. success(a) {
  101. _Http.basic({
  102. "classname": "system.attachment.huawei.OBS",
  103. "method": "uploadSuccess",
  104. "content": {
  105. "serialfilename": res.serialfilename
  106. }
  107. }).then(s => {
  108. console.log("文件上传反馈", s)
  109. // if (s.code != 1) return;
  110. that.triggerEvent("uploadCallback", s.data.attachmentids);
  111. that.triggerEvent("changeState", false)
  112. }).catch(err => {
  113. console.log(err)
  114. })
  115. }
  116. })
  117. },
  118. }
  119. })