upload.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if (data.state == 200) {
  26. let obj = {
  27. id: data.data.id,
  28. name: data.data.fileName + '.' + data.data.ext,
  29. ext: data.data.ext,
  30. uuid: data.data.id
  31. }
  32. resolve(obj)
  33. } else {
  34. reject()
  35. }
  36. },
  37. fail: () => {
  38. reject()
  39. }
  40. })
  41. })
  42. }
  43. function selectUpload(file) {
  44. return new Promise((resolve, reject) => {
  45. uni.showLoading({
  46. title: '上传中',
  47. mask: true
  48. })
  49. let that = this
  50. let list = []
  51. //读取文件大小
  52. var fileSize = file.size;
  53. if (fileSize > 1048576) {
  54. that.$message({
  55. type: 'error',
  56. showClose: true,
  57. duration: 3000,
  58. message: '文件大于1M!'
  59. });
  60. return
  61. }
  62. let eList = []
  63. file.tempFilePaths.forEach(item => {
  64. eList.push(uploaditem(item))
  65. })
  66. Promise.all(eList).then((res3) => {
  67. uni.hideLoading()
  68. resolve(res3)
  69. }).catch(error => {
  70. setTimeout(() => {
  71. uni.hideLoading()
  72. }, 1000)
  73. reject()
  74. })
  75. })
  76. }
  77. function getFilesList(ids, that = this) {
  78. return new Promise((resolve, reject) => {
  79. let lists = ids.split(",")
  80. let params = {}
  81. params.ids = lists
  82. that.$http.post("/ibps/platform/v3/file/attachment/transfer", params).then(res => {
  83. if (res.data.state == 200) {
  84. const data = res.data.data
  85. let list = []
  86. lists.forEach(item => {
  87. let obj = {
  88. id: item,
  89. name: data[item].fileName + '.' + data[item].ext,
  90. ext: data[item].ext,
  91. uuid: item
  92. }
  93. list.push(obj)
  94. })
  95. resolve(list)
  96. }
  97. })
  98. })
  99. }
  100. function download(id, item, that = this) {
  101. uni.showLoading({
  102. title: '下载中',
  103. mask: true
  104. })
  105. let requestData = {
  106. attachmentId: id
  107. }
  108. that.$http.get(`/ibps/platform/v3/file/download?attachmentId=${id}`, {
  109. responseType: 'arraybuffer'
  110. }).then(res => {
  111. const blob = new Blob([
  112. res.data
  113. ], {
  114. type: 'application/octet-stream'
  115. })
  116. const url = window.URL.createObjectURL(blob)
  117. const link = document.createElement('a')
  118. link.style.display = 'none'
  119. link.href = url
  120. link.setAttribute('download', item)
  121. document.body.appendChild(link)
  122. link.click()
  123. window.URL.revokeObjectURL(link.href)
  124. document.body.removeChild(link)
  125. uni.hideLoading()
  126. })
  127. }
  128. export default {
  129. download,
  130. getFilesList,
  131. selectUpload
  132. }