| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import {
- ACCESS_TOKEN,
- USER_NAME,
- USER_INFO
- } from "@/common/util/constants" // 拉取登录token,userName,userInfo
- import http from '@/common/service/http.js'
- function uploaditem(item) {
- let token = uni.getStorageSync(ACCESS_TOKEN)
- return new Promise((resolve, reject) => {
- const path = item
- uni.uploadFile({
- // url: http.apiHosp + 'ibps/platform/v3/file/upload/mobile', //仅为示例,非真实的接口地址
- url: http.apiHosp + '/ibps/platform/v3/file/upload', //仅为示例,非真实的接口地址
- filePath: path,
- name: 'file',
- // formData: {
- // 'ext': '.jpg'
- // },
- header: {
- 'X-Authorization-access_token': token,
- 'X-Authorization-systemid': ''
- },
- success: (uploadFileRes) => {
- let data = JSON.parse(uploadFileRes.data)
- if (data.state == 200) {
- let obj = {
- id: data.data.id,
- name: data.data.fileName + '.' + data.data.ext,
- ext: data.data.ext,
- uuid: data.data.id
- }
- resolve(obj)
- } else {
- reject()
- }
- },
- fail: () => {
- reject()
- }
- })
- })
- }
- function selectUpload(file) {
- return new Promise((resolve, reject) => {
- uni.showLoading({
- title: '上传中',
- mask: true
- })
- let that = this
- let list = []
- //读取文件大小
- var fileSize = file.size;
- if (fileSize > 1048576) {
- that.$message({
- type: 'error',
- showClose: true,
- duration: 3000,
- message: '文件大于1M!'
- });
- return
- }
- let eList = []
- file.tempFilePaths.forEach(item => {
- eList.push(uploaditem(item))
- })
- Promise.all(eList).then((res3) => {
- uni.hideLoading()
- resolve(res3)
- }).catch(error => {
- setTimeout(() => {
- uni.hideLoading()
- }, 1000)
- reject()
- })
- })
- }
- function getFilesList(ids, that = this) {
- return new Promise((resolve, reject) => {
- let lists = ids.split(",")
- let params = {}
- params.ids = lists
- that.$http.post("/ibps/platform/v3/file/attachment/transfer", params).then(res => {
- if (res.data.state == 200) {
- const data = res.data.data
- let list = []
- lists.forEach(item => {
- let obj = {
- id: item,
- name: data[item].fileName + '.' + data[item].ext,
- ext: data[item].ext,
- uuid: item
- }
- list.push(obj)
- })
- resolve(list)
- }
- })
- })
- }
- function download(id, item, that = this) {
- uni.showLoading({
- title: '下载中',
- mask: true
- })
- let requestData = {
- attachmentId: id
- }
- that.$http.get(`/ibps/platform/v3/file/download?attachmentId=${id}`, {
- responseType: 'arraybuffer'
- }).then(res => {
- const blob = new Blob([
- res.data
- ], {
- type: 'application/octet-stream'
- })
- const url = window.URL.createObjectURL(blob)
- const link = document.createElement('a')
- link.style.display = 'none'
- link.href = url
- link.setAttribute('download', item)
- document.body.appendChild(link)
- link.click()
- window.URL.revokeObjectURL(link.href)
- document.body.removeChild(link)
- uni.hideLoading()
- })
- }
- export default {
- download,
- getFilesList,
- selectUpload
- }
|