145 lines
3.5 KiB
Vue
145 lines
3.5 KiB
Vue
<template>
|
||
<view class="container">
|
||
<button type="default" @click="scanCode">扫码</button>
|
||
<!-- <view>
|
||
扫码结果:{{qrCodeRes}}
|
||
</view>
|
||
|
||
<image :src="qc"></image> -->
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
let Qrcode = require('../../../../utils/reqrcode.js')
|
||
import {
|
||
getInspectionManage
|
||
} from '@/api/inspection/inspectionManage.js'
|
||
export default {
|
||
data() {
|
||
return {
|
||
qrCodeRes: '',
|
||
qc: ''
|
||
}
|
||
},
|
||
methods: {
|
||
// 扫码
|
||
scanCode() {
|
||
// #ifdef APP-PLUS
|
||
this.scanCodeAPP()
|
||
// #endif
|
||
|
||
// #ifdef H5
|
||
this.scanCodeH5()
|
||
// #endif
|
||
},
|
||
// APP直接调用 uni.scanCode 接口
|
||
scanCodeAPP() {
|
||
uni.scanCode({
|
||
scanType: ['qrCode'],
|
||
success: (res) => {
|
||
this.qrCodeRes = res.result
|
||
}
|
||
})
|
||
},
|
||
// H5通过拉起相机拍照来识别二维码
|
||
scanCodeH5() {
|
||
let that = this;
|
||
uni.chooseImage({
|
||
count: 1,
|
||
sourceType: ['camera'],
|
||
success: imgRes => {
|
||
that.qc = imgRes.tempFiles[0].path; // 预览图片
|
||
const tempFile = imgRes.tempFiles[0];
|
||
const reader = new FileReader();
|
||
|
||
reader.onload = function(event) {
|
||
const base64 = event.target.result;
|
||
try {
|
||
Qrcode.qrcode.decode(base64);
|
||
Qrcode.qrcode.callback = (codeRes) => {
|
||
if (codeRes.indexOf('error') >= 0) {
|
||
that.qrCodeRes = '不合法二维码:' + codeRes;
|
||
uni.showToast({
|
||
title: '二维码识别失败!',
|
||
icon: 'none'
|
||
});
|
||
} else {
|
||
let r = that.decodeStr(codeRes);
|
||
that.qrCodeRes = r;
|
||
getInspectionManage(that.qrCodeRes)
|
||
.then(res => {
|
||
if (res.data.inspectionStatus === "1") {
|
||
console.log(res)
|
||
let inspectionPoint = res.data.inspectionPoint;
|
||
let inspectionRequirements = res.data
|
||
.inspectionRequirements;
|
||
let inspectionPointId = res.data.id
|
||
uni.reLaunch({
|
||
url: `/pages/work/inspection/scanSign/index?inspectionPoint=${inspectionPoint}&inspectionRequirements=${inspectionRequirements}&inspectionPointId=${inspectionPointId}`
|
||
});
|
||
} else {
|
||
uni.showToast({
|
||
title: '验证码已失效!',
|
||
icon: 'none'
|
||
});
|
||
uni.reLaunch({
|
||
url: '/pages/work/index'
|
||
});
|
||
}
|
||
})
|
||
.catch(err => {
|
||
console.log("请求错误", err);
|
||
uni.showToast({
|
||
title: '服务器错误',
|
||
icon: 'none'
|
||
});
|
||
});
|
||
}
|
||
};
|
||
} catch (error) {
|
||
console.log("解析失败", error);
|
||
uni.showToast({
|
||
title: '二维码解析失败',
|
||
icon: 'none'
|
||
});
|
||
}
|
||
};
|
||
|
||
reader.readAsDataURL(tempFile);
|
||
},
|
||
fail: (err) => {
|
||
console.log("图片选择失败", err);
|
||
uni.showToast({
|
||
title: '拍照失败,请重试',
|
||
icon: 'none'
|
||
});
|
||
}
|
||
});
|
||
},
|
||
// 获取文件地址函数
|
||
getObjectURL(file) {
|
||
if (window.URL && window.URL.createObjectURL) {
|
||
return window.URL.createObjectURL(file);
|
||
} else {
|
||
console.warn("当前浏览器不支持 createObjectURL 方法");
|
||
return null;
|
||
}
|
||
},
|
||
// 解码,输出:中文
|
||
decodeStr(str) {
|
||
try {
|
||
return decodeURIComponent(escape(str));
|
||
} catch (e) {
|
||
console.warn("解码失败,返回原字符串:", str);
|
||
return str;
|
||
}
|
||
},
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
.container {
|
||
padding: 10px;
|
||
}
|
||
</style> |