index.js 3.6 KB

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