index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view class="page flex-column">
  3. <cover-image
  4. class="page_back"
  5. :style="backStyle"
  6. src="/static/scan/scan-camera-back.png"
  7. @click="back"
  8. ></cover-image>
  9. <view class="page_camera flex-grow">
  10. <camera
  11. v-if="!isDevTool"
  12. device-position="back"
  13. mode="scanCode"
  14. :flash="cameraFlash"
  15. @scancode="handleScancode"
  16. @ready="handleReady"
  17. @error="handleError"
  18. :style="{
  19. width: '100%',
  20. height: `${cameraHeight}px`,
  21. }"
  22. ></camera>
  23. <view
  24. v-else
  25. class="mock-camera"
  26. :style="{
  27. width: '100%',
  28. height: `${cameraHeight}px`,
  29. }"
  30. ></view>
  31. </view>
  32. <view class="page_action flex-shrink" id="page_action">
  33. <cover-view
  34. @click="toggleFlash"
  35. style="
  36. text-align: center;
  37. position: absolute;
  38. width: 100%;
  39. left: 0;
  40. top: -96px;
  41. "
  42. >
  43. <cover-image
  44. style="width: 28px; height: 28px; margin: 0 auto"
  45. src="/static/scan/scan-code-light.png"
  46. ></cover-image>
  47. <cover-view
  48. style="
  49. color: rgba(255, 255, 255, 0.7);
  50. font-size: 13px;
  51. margin-top: 10px;
  52. "
  53. >{{ cameraFlash === "off" ? "轻触照亮" : "轻触关闭" }}</cover-view
  54. >
  55. </cover-view>
  56. <view class="flex">
  57. <view
  58. class="flex-center flex-column"
  59. @click="redirect('/pages-charge/codeing/codeing')"
  60. >
  61. <image
  62. class="width-96"
  63. mode="widthFix"
  64. src="/static/scan/scan-code-input.png"
  65. ></image>
  66. <!-- <view class="fs-24 color-fff mt-20">输入充电桩编码</view>-->
  67. </view>
  68. <view class="flex-center flex-column" @click="chooseImage">
  69. <image
  70. class="width-96"
  71. mode="widthFix"
  72. src="/static/scan/scan-code-album.png"
  73. ></image>
  74. <view class="fs-24 color-fff mt-20">打开相册</view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. </template>
  80. <script setup lang="ts">
  81. import { ref } from "vue";
  82. import { isDevTool } from "@/utils/device";
  83. import { back, redirect } from "@/utils/navigate";
  84. import { onLoad, onReady } from "@dcloudio/uni-app";
  85. import {debounce} from "@/utils/common";
  86. const cameraHeight = ref(0);
  87. const cameraFlash = ref("off");
  88. const backStyle = ref("");
  89. const scaning = ref(false)
  90. onLoad(() => {
  91. const menuButtonRect = uni.getMenuButtonBoundingClientRect();
  92. backStyle.value = `left:12px;top:${menuButtonRect.top + 6}px;`;
  93. // uni.showLoading({
  94. // title: "加载中",
  95. // });
  96. // fetchChargeStatus(true, true)
  97. // .then(() => {
  98. // uni.hideLoading();
  99. // })
  100. // .catch(() => {
  101. // uni.hideLoading();
  102. // });
  103. });
  104. onReady(() => {
  105. // #ifdef MP-WEIXIN
  106. const query = wx.createSelectorQuery();
  107. query.select("#page_action").boundingClientRect();
  108. query.exec(function (res: any) {
  109. if (res && res.length) {
  110. cameraHeight.value =
  111. getApp<any>().globalData.device.windowHeight - res[0].height;
  112. uni.hideLoading();
  113. }
  114. });
  115. // #endif
  116. });
  117. const handleReady = () => {
  118. uni.hideLoading();
  119. };
  120. const handleScancode = (e: any) => {
  121. // debounce(()=>{
  122. if(scaning.value){
  123. return;
  124. }else{
  125. scaning.value = true;
  126. }
  127. uni.vibrateShort({});
  128. if (e.detail && e.detail.result) {
  129. console.log(e)
  130. let {id} = e.detail.result;
  131. uni.navigateTo({
  132. url:'/pages-wash/device/index?id='+id
  133. })
  134. }
  135. setTimeout(()=>{
  136. scaning.value = false;
  137. },500)
  138. // },2000)
  139. };
  140. const debounceScan = debounce((e)=>{
  141. handleScancode(e)
  142. },2000)
  143. const handleError = (e: any) => {
  144. uni.hideLoading();
  145. if (e.detail && e.detail.errMsg && /auth/.test(e.detail.errMsg)) {
  146. uni.showModal({
  147. content: "请打开摄像头权限开关",
  148. success: () => {
  149. uni.openSetting();
  150. },
  151. });
  152. return;
  153. }
  154. uni.showModal({
  155. content: e.detail.errMsg,
  156. });
  157. };
  158. const toggleFlash = () => {
  159. if (cameraFlash.value === "off") {
  160. cameraFlash.value = "on";
  161. return;
  162. }
  163. cameraFlash.value = "off";
  164. };
  165. const chooseImage = () => {
  166. uni.chooseImage({
  167. count: 1,
  168. sizeType: ["compressed"],
  169. sourceType: ["album"],
  170. success: (res) => {
  171. uni.showLoading({
  172. title: "正在识别",
  173. });
  174. },
  175. fail: (err) => {
  176. console.log(err);
  177. if (/cancel/.test(err.errMsg)) {
  178. return;
  179. }
  180. uni.showModal({
  181. content: err.errMsg || "出现错误,请重试",
  182. });
  183. },
  184. });
  185. };
  186. </script>
  187. <style lang="scss">
  188. .page {
  189. position: relative;
  190. height: 100vh;
  191. background-color: #000;
  192. &_back {
  193. position: absolute;
  194. width: 24px;
  195. height: auto;
  196. }
  197. &_camera {
  198. height: 100%;
  199. .mock-camera {
  200. background-color: #999;
  201. }
  202. }
  203. &_action {
  204. position: relative;
  205. box-sizing: content-box;
  206. padding-bottom: constant(safe-area-inset-bottom);
  207. padding-bottom: env(safe-area-inset-bottom);
  208. & > view {
  209. height: 222rpx;
  210. & > view {
  211. width: 50%;
  212. height: 222rpx;
  213. }
  214. }
  215. }
  216. }
  217. </style>