upload.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {
  2. ACCESS_TOKEN,
  3. USER_NAME,
  4. USER_INFO
  5. } from "@/common/util/constants" // 拉取登录token,userName,userInfo
  6. import http from '@/common/service/http.js'
  7. function uploaditem(item) {
  8. let token = uni.getStorageSync(ACCESS_TOKEN)
  9. return new Promise((resolve, reject) => {
  10. const path = item
  11. uni.uploadFile({
  12. // url: http.apiHosp + 'ibps/platform/v3/file/upload/mobile', //仅为示例,非真实的接口地址
  13. url: http.apiHosp + '/ibps/platform/v3/file/upload', //仅为示例,非真实的接口地址
  14. filePath: path,
  15. name: 'file',
  16. formData: {
  17. 'ext': '.jpg'
  18. },
  19. header: {
  20. 'X-Authorization-access_token': token,
  21. 'X-Authorization-systemid': ''
  22. },
  23. success: (uploadFileRes) => {
  24. let data = JSON.parse(uploadFileRes.data)
  25. let obj = {
  26. id: data.data.id,
  27. name: data.data.fileName + '.' + data.data.ext,
  28. ext: data.data.ext,
  29. uuid: data.data.id
  30. }
  31. resolve(obj)
  32. },
  33. fail: () => {
  34. reject()
  35. }
  36. })
  37. })
  38. }
  39. function selectUpload(file) {
  40. return new Promise((resolve, reject) => {
  41. uni.showLoading({
  42. title: '上传中',
  43. mask: true
  44. })
  45. let that = this
  46. let list = []
  47. //读取文件大小
  48. var fileSize = file.size;
  49. if (fileSize > 1048576) {
  50. that.$message({
  51. type: 'error',
  52. showClose: true,
  53. duration: 3000,
  54. message: '文件大于1M!'
  55. });
  56. return
  57. }
  58. let eList = []
  59. file.tempFilePaths.forEach(item => {
  60. eList.push(uploaditem(item))
  61. })
  62. Promise.all(eList).then((res3) => {
  63. uni.hideLoading()
  64. resolve(res3)
  65. }).catch(error => {
  66. setTimeout(() => {
  67. uni.hideLoading()
  68. }, 1000)
  69. reject()
  70. })
  71. })
  72. }
  73. function getFilesList(ids, that = this) {
  74. return new Promise((resolve, reject) => {
  75. let lists = ids.split(",")
  76. let params = {}
  77. params.ids = lists
  78. that.$http.post("/ibps/platform/v3/file/attachment/transfer", params).then(res => {
  79. if (res.data.state == 200) {
  80. const data = res.data.data
  81. let list = []
  82. lists.forEach(item => {
  83. let obj = {
  84. id: item,
  85. name: data[item].fileName + '.' + data[item].ext,
  86. ext: data[item].ext,
  87. uuid: item
  88. }
  89. list.push(obj)
  90. })
  91. resolve(list)
  92. }
  93. })
  94. })
  95. }
  96. function download(id, item, that = this) {
  97. uni.showLoading({
  98. title: '下载中',
  99. mask: true
  100. })
  101. let requestData = {
  102. attachmentId: id
  103. }
  104. that.$http.get(`/ibps/platform/v3/file/download?attachmentId=${id}`, {
  105. responseType: 'arraybuffer'
  106. }).then(res => {
  107. const blob = new Blob([
  108. res.data
  109. ], {
  110. type: 'application/octet-stream'
  111. })
  112. const url = window.URL.createObjectURL(blob)
  113. const link = document.createElement('a')
  114. link.style.display = 'none'
  115. link.href = url
  116. link.setAttribute('download', item)
  117. document.body.appendChild(link)
  118. link.click()
  119. window.URL.revokeObjectURL(link.href)
  120. document.body.removeChild(link)
  121. uni.hideLoading()
  122. })
  123. }
  124. export default {
  125. download,
  126. getFilesList,
  127. selectUpload
  128. }