| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <template>
- <view class="page flex-column">
- <cover-image
- class="page_back"
- :style="backStyle"
- src="/static/scan/scan-camera-back.png"
- @click="back"
- ></cover-image>
- <view class="page_camera flex-grow">
- <camera
- v-if="!isDevTool"
- device-position="back"
- mode="scanCode"
- :flash="cameraFlash"
- @scancode="handleScancode"
- @ready="handleReady"
- @error="handleError"
- :style="{
- width: '100%',
- height: `${cameraHeight}px`,
- }"
- ></camera>
- <view
- v-else
- class="mock-camera"
- :style="{
- width: '100%',
- height: `${cameraHeight}px`,
- }"
- ></view>
- </view>
- <view class="page_action flex-shrink" id="page_action">
- <cover-view
- @click="toggleFlash"
- style="
- text-align: center;
- position: absolute;
- width: 100%;
- left: 0;
- top: -96px;
- "
- >
- <cover-image
- style="width: 28px; height: 28px; margin: 0 auto"
- src="/static/scan/scan-code-light.png"
- ></cover-image>
- <cover-view
- style="
- color: rgba(255, 255, 255, 0.7);
- font-size: 13px;
- margin-top: 10px;
- "
- >{{ cameraFlash === "off" ? "轻触照亮" : "轻触关闭" }}</cover-view
- >
- </cover-view>
- <view class="flex">
- <view
- class="flex-center flex-column"
- @click="redirect('/pages-charge/codeing/codeing')"
- >
- <image
- class="width-96"
- mode="widthFix"
- src="/static/scan/scan-code-input.png"
- ></image>
- <!-- <view class="fs-24 color-fff mt-20">输入充电桩编码</view>-->
- </view>
- <view class="flex-center flex-column" @click="chooseImage">
- <image
- class="width-96"
- mode="widthFix"
- src="/static/scan/scan-code-album.png"
- ></image>
- <view class="fs-24 color-fff mt-20">打开相册</view>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script setup lang="ts">
- import { ref } from "vue";
- import { isDevTool } from "@/utils/device";
- import { back, redirect } from "@/utils/navigate";
- import { onLoad, onReady } from "@dcloudio/uni-app";
- import {debounce} from "@/utils/common";
- const cameraHeight = ref(0);
- const cameraFlash = ref("off");
- const backStyle = ref("");
- const scaning = ref(false)
- onLoad(() => {
- const menuButtonRect = uni.getMenuButtonBoundingClientRect();
- backStyle.value = `left:12px;top:${menuButtonRect.top + 6}px;`;
- // uni.showLoading({
- // title: "加载中",
- // });
- // fetchChargeStatus(true, true)
- // .then(() => {
- // uni.hideLoading();
- // })
- // .catch(() => {
- // uni.hideLoading();
- // });
- });
- onReady(() => {
- // #ifdef MP-WEIXIN
- const query = wx.createSelectorQuery();
- query.select("#page_action").boundingClientRect();
- query.exec(function (res: any) {
- if (res && res.length) {
- cameraHeight.value =
- getApp<any>().globalData.device.windowHeight - res[0].height;
- uni.hideLoading();
- }
- });
- // #endif
- });
- const handleReady = () => {
- uni.hideLoading();
- };
- const handleScancode = (e: any) => {
- // debounce(()=>{
- if(scaning.value){
- return;
- }else{
- scaning.value = true;
- }
- uni.vibrateShort({});
- if (e.detail && e.detail.result) {
- console.log(e)
- let {id} = e.detail.result;
- uni.navigateTo({
- url:'/pages-wash/device/index?id='+id
- })
- }
- setTimeout(()=>{
- scaning.value = false;
- },500)
- // },2000)
- };
- const debounceScan = debounce((e)=>{
- handleScancode(e)
- },2000)
- const handleError = (e: any) => {
- uni.hideLoading();
- if (e.detail && e.detail.errMsg && /auth/.test(e.detail.errMsg)) {
- uni.showModal({
- content: "请打开摄像头权限开关",
- success: () => {
- uni.openSetting();
- },
- });
- return;
- }
- uni.showModal({
- content: e.detail.errMsg,
- });
- };
- const toggleFlash = () => {
- if (cameraFlash.value === "off") {
- cameraFlash.value = "on";
- return;
- }
- cameraFlash.value = "off";
- };
- const chooseImage = () => {
- uni.chooseImage({
- count: 1,
- sizeType: ["compressed"],
- sourceType: ["album"],
- success: (res) => {
- uni.showLoading({
- title: "正在识别",
- });
- },
- fail: (err) => {
- console.log(err);
- if (/cancel/.test(err.errMsg)) {
- return;
- }
- uni.showModal({
- content: err.errMsg || "出现错误,请重试",
- });
- },
- });
- };
- </script>
- <style lang="scss">
- .page {
- position: relative;
- height: 100vh;
- background-color: #000;
- &_back {
- position: absolute;
- width: 24px;
- height: auto;
- }
- &_camera {
- height: 100%;
- .mock-camera {
- background-color: #999;
- }
- }
- &_action {
- position: relative;
- box-sizing: content-box;
- padding-bottom: constant(safe-area-inset-bottom);
- padding-bottom: env(safe-area-inset-bottom);
- & > view {
- height: 222rpx;
- & > view {
- width: 50%;
- height: 222rpx;
- }
- }
- }
- }
- </style>
|