|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <h1 class="accordion-header" id="histAccordion-headingOne"> |
| 4 | + Печать по QR коду |
| 5 | + </h1> |
| 6 | + <small |
| 7 | + ><a href="#" @click.prevent="this.$router.go(-1)">← Вернуться</a></small |
| 8 | + > |
| 9 | + <p> |
| 10 | + Для быстрой печати по QR подойдите к принтеру и отсканируйте код под |
| 11 | + кнопкой "Печать". |
| 12 | + </p> |
| 13 | + <div v-if="qrInitSuccess === undefined"> |
| 14 | + <p class="alert alert-success" role="alert"> |
| 15 | + Попытка подключиться к камере |
| 16 | + </p> |
| 17 | + <p> |
| 18 | + Пожалуйста, предоставьте сайту доступ к камере вашего устройства. Вся |
| 19 | + обработка происходит на вашем устройстве, разработчики получают доступ |
| 20 | + только к отсканированному коду. |
| 21 | + </p> |
| 22 | + </div> |
| 23 | + <div v-else-if="qrInitSuccess === false"> |
| 24 | + <p class="alert alert-secondary" role="alert"> |
| 25 | + Для печати необходимо предоставить сайту доступ к камере! |
| 26 | + </p> |
| 27 | + <p> |
| 28 | + Пожалуйста, предоставьте сайту доступ к камере вашего устройства. Вся |
| 29 | + обработка происходит на вашем устройстве, разработчики получают доступ |
| 30 | + только к отсканированному коду. |
| 31 | + </p> |
| 32 | + </div> |
| 33 | + <qrcode-stream |
| 34 | + @decode="onDecode" |
| 35 | + @init="onInit" |
| 36 | + v-if="(qrInitSuccess !== false) & !qrPrintStatus" |
| 37 | + > |
| 38 | + </qrcode-stream> |
| 39 | + <div class="form-actions"> |
| 40 | + <div v-if="qrPrintStatus === 'pending'"> |
| 41 | + <p>Обработка...</p> |
| 42 | + </div> |
| 43 | + <div v-if="qrPrintStatus === 'success'"> |
| 44 | + <p>Готово!</p> |
| 45 | + <router-link to="/" class="btn btn-lg btn-primary"> |
| 46 | + Вернуться на главную |
| 47 | + </router-link> |
| 48 | + <router-link to="/history" class="btn btn-lg btn-primary"> |
| 49 | + Перейти к истории |
| 50 | + </router-link> |
| 51 | + </div> |
| 52 | + <div v-if="qrPrintStatus === 'error'"> |
| 53 | + <p>Не вышло!</p> |
| 54 | + <p>{{ qrPrintErrorMsg }}</p> |
| 55 | + <router-link to="/" class="btn btn-lg btn-primary"> |
| 56 | + Вернуться на главную |
| 57 | + </router-link> |
| 58 | + <router-link to="/history" class="btn btn-lg btn-primary"> |
| 59 | + Перейти к истории |
| 60 | + </router-link> |
| 61 | + <div class="btn btn-lg btn-success" @click="reset"> |
| 62 | + Попробовать еще раз |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | +</template> |
| 68 | + |
| 69 | +<script> |
| 70 | +import { QrcodeStream } from "vue3-qrcode-reader"; |
| 71 | +import { log_open_qr, log_print_qr, log_error_qr } from "@/utils/marketing"; |
| 72 | +
|
| 73 | +export default { |
| 74 | + data: () => ({ |
| 75 | + pins: [], |
| 76 | + qrInitSuccess: undefined, |
| 77 | + qrPrintStatus: undefined, |
| 78 | + qrPrintErrorMsg: "", |
| 79 | + }), |
| 80 | + components: { |
| 81 | + QrcodeStream, |
| 82 | + }, |
| 83 | + methods: { |
| 84 | + reset() { |
| 85 | + this.qrInitSuccess = undefined; |
| 86 | + this.qrPrintStatus = undefined; |
| 87 | + this.qrPrintErrorMsg = ""; |
| 88 | + }, |
| 89 | + onDecode(decodedString) { |
| 90 | + console.log(decodedString); |
| 91 | + this.qrPrintStatus = "pending"; |
| 92 | +
|
| 93 | + fetch(`${process.env.VUE_APP_API_PRINTER}/qr`, { |
| 94 | + method: "POST", |
| 95 | + cache: "no-cache", |
| 96 | + redirect: "follow", |
| 97 | + headers: { "Content-Type": "application/json" }, |
| 98 | + body: JSON.stringify({ |
| 99 | + qr_token: decodedString, |
| 100 | + files: this.pins, |
| 101 | + }), |
| 102 | + }) |
| 103 | + .then((response) => response.json()) |
| 104 | + .then((response) => { |
| 105 | + console.log(response); |
| 106 | + if (response.status === "ok") { |
| 107 | + this.qrPrintStatus = "success"; |
| 108 | + log_print_qr(this.pins); |
| 109 | + } else if (response.detail === "Terminal not found by qr") { |
| 110 | + this.qrPrintStatus = "error"; |
| 111 | + this.qrPrintErrorMsg = "QR код недействительный"; |
| 112 | + log_error_qr(this.pins, response); |
| 113 | + } else if (response.detail === "Terminal not found by qr") { |
| 114 | + this.qrPrintStatus = "error"; |
| 115 | + this.qrPrintErrorMsg = "QR код недействительный"; |
| 116 | + log_error_qr(this.pins, response); |
| 117 | + } else { |
| 118 | + this.qrPrintStatus = "error"; |
| 119 | + this.qrPrintErrorMsg = "Неизвестная ошибка"; |
| 120 | + log_error_qr(this.pins, response); |
| 121 | + } |
| 122 | + }) |
| 123 | + .catch((error) => { |
| 124 | + console.log(error); |
| 125 | + this.qrPrintStatus = "error"; |
| 126 | + this.qrPrintErrorMsg = "Ошибка сети"; |
| 127 | + log_error_qr(this.pins, error); |
| 128 | + }); |
| 129 | + }, |
| 130 | + async onInit(promise) { |
| 131 | + // show loading indicator |
| 132 | +
|
| 133 | + try { |
| 134 | + await promise; |
| 135 | + this.qrInitSuccess = true; |
| 136 | + } catch (error) { |
| 137 | + this.qrInitSuccess = false; |
| 138 | + if (error.name === "NotAllowedError") { |
| 139 | + // user denied camera access permisson |
| 140 | + } else if (error.name === "NotFoundError") { |
| 141 | + // no suitable camera device installed |
| 142 | + } else if (error.name === "NotSupportedError") { |
| 143 | + // page is not served over HTTPS (or localhost) |
| 144 | + } else if (error.name === "NotReadableError") { |
| 145 | + // maybe camera is already in use |
| 146 | + } else if (error.name === "OverconstrainedError") { |
| 147 | + // did you requested the front camera although there is none? |
| 148 | + } else if (error.name === "StreamApiNotSupportedError") { |
| 149 | + // browser seems to be lacking features |
| 150 | + } |
| 151 | + } finally { |
| 152 | + // hide loading indicator |
| 153 | + } |
| 154 | + }, |
| 155 | + }, |
| 156 | + mounted() { |
| 157 | + const location = document.location.hash.replace(/^#?/, ""); |
| 158 | + this.pins = location.split(","); |
| 159 | + log_open_qr(); |
| 160 | + }, |
| 161 | +}; |
| 162 | +</script> |
0 commit comments