<template>
<div class="upload">
<div class="uploadHead">
<div class="uploadHeadTop">出仓单号</div>
<div class="uploadHeadBottom">LCGT20210822026</div>
</div>
<div class="uploadTips">
<div class="uploadTipsTop">
<div class="uploadTipsTit">目的地</div>
<div class="uploadTipsMsg">高明美宗</div>
</div>
<div class="uploadTipsBottom">
<div class="uploadTipsTit">车牌号</div>
<div class="uploadTipsMsg">粤X32659</div>
</div>
</div>
<div>
<div class="uploadBtnBox">
<van-uploader :after-read="afterRead">
<div class="uploadBtn">
<div class="uploadBtnL">点此上传磅单</div>
<div class="uploadBtnR">
<img src="@/assets/uploadimg.png" alt="">
</div>
</div>
</van-uploader>
</div>
<div class="uploadBtnTips">
*该单据已上传过磅单 如有要您可继续上传
</div>
</div>
<!-- <div class="uploadOK">出仓单已送达,无法上传磅单</div> -->
</div>
</template>
<script>
import { ref ,reactive,getCurrentInstance,onMounted} from 'vue';
export default{
setup() {
},
data() {
return {
};
},
mounted() {
},
computed: {
},
methods:{
uploadImg(blob, base64) {
const formData = new FormData();
formData.append("attach", blob, ".jpg");
this.$http.postImg(this.$api.UPLOAD, formData).then((res) => {
// if (res.code == 0) {
// let obj = {};
// obj["src"] = res.data;
// this.picList.push(obj);
// console.log(this.picList);
// }
console.log(res)
});
},
blobToBase64(blob) {
const self = this; // 绑定this
const reader = new FileReader();
reader.readAsDataURL(blob); // 添加二进制文件
reader.onload = function (event) {
const base64 = event.target.result; // 获取到它的base64文件
const scale = 0.5; // 设置缩放比例 (0-1)
self.compressImg(base64, scale, self.uploadImg);
};
},
compressImg(base64, scale, callback) {
console.log(`执行缩放程序,scale=${scale}`);
const img = new Image();
img.src = base64;
img.onload = function () {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.setAttribute("width", this.width);
canvas.setAttribute("height", this.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
let base64 = canvas.toDataURL("image/jpeg");
while (base64.length > 1024 * 1024 * 3) {
scale -= 0.01;
base64 = canvas.toDataURL("image/jpeg", scale);
}
const arr = base64.split(",");
const mime = arr[0].match(/:(.*?);/)[1];
const bytes = atob(arr[1]);
const bytesLength = bytes.length;
const u8arr = new Uint8Array(bytesLength);
for (let i = 0; i < bytes.length; i++) {
u8arr[i] = bytes.charCodeAt(i);
}
const blob = new Blob([u8arr], { type: mime });
callback(blob, base64);
};
},
afterRead(files) {
console.log(files.file)
// let files = e.target.files;
this.blobToBase64(files.file);
},
}
}
</script>
<style scoped>
.uploadHead{
width: 622px;
height: 164px;
background: #f3f4f5;
margin: 0 auto;
margin-top: 40px;
text-align: center;
}
.uploadHeadTop{
font-size: 28px;
color: #999999;
padding-bottom: 8px;
padding-top: 32px;
}
.uploadHeadBottom{
color: #333333;
font-size: 40px;
}
.uploadTips{
display: flex;
padding: 0 64px;
margin-top: 64px;
margin-bottom: 120px;
}
.uploadTipsTop{
width: 50%;
text-align: center;
}
.uploadTipsBottom{
width: 50%;
text-align: center;
}
.uploadTipsTit{
font-size: 28px;
margin-bottom: 8px;
color: #999999;
}
.uploadTipsMsg{
font-size: 34px;
color: #333333;
}
.uploadBtnBox{
display: flex;
margin: 0 64px;
}
.uploadBtn{
background: #1F7DE9;
display: flex;
align-items: center;
justify-content: space-between;
border-radius: 16px;
width: 622px;
padding: 64px 64px;
box-sizing: border-box;
}
.uploadBtnR img{
width: 174px;
}
.uploadBtnL{
font-size: 34px;
color: #fff;
}
.uploadOK{
color: #d80f09;
font-size: 40px;
text-align: center;
padding-top: 60px;
}
.uploadBtnTips{
width: 324px;
height: 96px;
font-size: 34px;
color: #d80f09;
text-align: center;
margin: 0 auto;
margin-top: 40px;
}
</style>
import axios from "axios";
import QS from 'qs';
import config from "../../config";
axios.defaults.baseURL = config.API_URI;
axios.defaults.timeout = 10000;
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
config => {
//请求之前(可以设置token)
// if (localStorage.getItem('token')){ // 判断是否存在token,如果存在的话,则每个http header都加上token
// config.headers.common['Token'] = localStorage.getItem('token') || ''
// }
return config;
},
error => {
alert(error);
return Promise.reject(error);
}
);
axios.interceptors.response.use(
response => {
//数据拿到之后
if (response.status === 200) {
return Promise.resolve(response);
} else {
return Promise.reject(response);
}
},
error => {
alert("Http请求失败,请联系管理员");
return Promise.reject(error.response);
}
);
//成功方法
function successfun(res) {
//处理后台返回的非200错误
if (res.code === 0) {
return res;
} else {
alert(res.msg);
return res;
}
}
//错误方法
function errorfun(res) {
if (res.code != 1) {
alert(res.msg);
return res;
}
}
export default {
postImg(url, data) {
//post请求
return axios({
method: "post",
url,
data: data,
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(
res => {
return successfun(res.data);
},
err => {
return errorfun(err);
}
);
},
post(url, data) {
//post请求
return axios({
method: "post",
url,
data: QS.stringify(data),
}).then(
res => {
return successfun(res.data);
},
err => {
return errorfun(err);
}
);
},
get(url, params) {
//get请求
return axios({
method: "get",
url,
params,
}).then(
res => {
return successfun(res.data);
},
err => {
return errorfun(err);
}
);
}
};
第一个文件,afterRead方法是vant框架的上传获取原生文件流
第二个文件,设置多一个方法postImg,设置headers
网友评论