scan_code.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <view class="page-container">
  3. <view class="camera-wrapper">
  4. <camera mode= "scanCode" device-position="back" flash="off" @scancode="onScanCodeSuccess" @error="onCameraError" style="width: 100%; height: 450rpx;"></camera>
  5. </view>
  6. <view class="scan-result-card">
  7. <view class="card-header">
  8. <uni-icons type="scan" size="20" color="#333"></uni-icons>
  9. <text class="header-title">已扫二维码</text>
  10. <view class="count-badge">
  11. <text>总数:{{ totalCount }}个</text>
  12. </view>
  13. </view>
  14. <scroll-view scroll-y="true" class="code-list-scroll">
  15. <view class="code-list-grid">
  16. <view v-if="numberList.length === 0" class="empty-tip">等待扫描...</view>
  17. <view class="code-item" v-for="(code, index) in numberList" :key="index">
  18. {{ formatIndex(index + 1) }}.{{ code }}
  19. </view>
  20. </view>
  21. </scroll-view>
  22. <view class="footer-buttons">
  23. <button class="btn clear-btn" @click="clearAll">全部清空</button>
  24. <button class="btn submit-btn" @click="submit">提交</button>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import {getQrcodeNum } from '@/api/hexiao.js';
  31. import {active} from "../../../../api/hexiao";
  32. export default {
  33. data() {
  34. return {
  35. // 列表初始为空
  36. codeList: [],
  37. storeId:0,
  38. numberList: [],
  39. // 用于播放提示音
  40. audioContext: null
  41. };
  42. },
  43. onReady() {
  44. // 提前创建好音频上下文,提高性能
  45. // 您需要在 static 目录下放置一个提示音文件,例如 beep.mp3
  46. this.audioContext = uni.createInnerAudioContext();
  47. this.audioContext.src = '/static/beep.mp3'; // 请替换为您的提示音文件路径
  48. },
  49. onLoad(opt) {
  50. this.storeId = opt.storeId
  51. // 获取二维码列表
  52. // this.codeList = this.$store.state.qrCodeList;
  53. },
  54. computed: {
  55. totalCount() {
  56. return this.codeList.length;
  57. }
  58. },
  59. methods: {
  60. getCodeNumber(code){
  61. if (this.codeList.includes(code)) {
  62. uni.showToast({
  63. title: '该码已存在',
  64. icon: 'none'
  65. });
  66. return;
  67. }
  68. uni.showLoading();
  69. getQrcodeNum(code).then(res=>{
  70. uni.hideLoading();
  71. if(res.code === 0){
  72. let data = res.data;
  73. //检查是否已存在,防止重复添加
  74. if (this.codeList.includes(code)) {
  75. uni.showToast({
  76. title: '该码已存在',
  77. icon: 'none'
  78. });
  79. return;
  80. }
  81. // 添加到列表顶部
  82. this.codeList.unshift(code);
  83. this.numberList.unshift(data);
  84. // 给予用户反馈
  85. this.playBeep();
  86. uni.vibrateShort();
  87. }else{
  88. uni.showToast({
  89. title: "该码有误",
  90. icon: 'none'
  91. });
  92. }
  93. });
  94. },
  95. // 3. 处理相机扫码成功的事件
  96. onScanCodeSuccess(e) {
  97. let code = e.detail.result;
  98. console.log('扫描到的内容:', code);
  99. if(code.length != "https://e.dnzc.vip/abce563011c5347f".length){
  100. uni.showToast({
  101. title: '二维码有误',
  102. icon: 'none'
  103. });
  104. return;
  105. }
  106. let index = code.lastIndexOf("/");
  107. code = code.substr(index+1);
  108. this.getCodeNumber(code)
  109. },
  110. // 相机初始化失败
  111. onCameraError(e) {
  112. console.log(e.detail);
  113. uni.showModal({
  114. title: '相机错误',
  115. content: '无法启动相机,请检查权限或重启应用',
  116. showCancel: false
  117. })
  118. },
  119. // 播放提示音
  120. playBeep() {
  121. this.audioContext.play();
  122. },
  123. // 格式化序号
  124. formatIndex(num) {
  125. return num < 10 ? '0' + num : num;
  126. },
  127. // 清空事件
  128. clearAll() {
  129. if (this.totalCount === 0) return;
  130. uni.showModal({
  131. title: '确认',
  132. content: '您确定要清空所有已扫描的二维码吗?',
  133. success: (res) => {
  134. if (res.confirm) {
  135. this.codeList = [];
  136. this.numberList = [];
  137. uni.showToast({ title: '已清空', icon: 'success' });
  138. }
  139. }
  140. });
  141. },
  142. // 提交事件
  143. submit() {
  144. if (this.totalCount === 0) {
  145. uni.showToast({ title: '没有可提交的内容', icon: 'none' });
  146. return;
  147. }
  148. console.log('提交的二维码列表:', this.codeList);
  149. uni.showLoading({ title: '提交中...' });
  150. let obj = {};
  151. obj.storeId = 1;
  152. obj.qrcodeIds = this.codeList;
  153. active(obj).then(res=>{
  154. uni.hideLoading();
  155. if(res.code == 0){
  156. uni.showToast({ title: '操作成功', icon: 'none' });
  157. this.numberList = []
  158. this.codeList = [];
  159. uni.navigateBack();
  160. }else{
  161. uni.showToast({ title: res.msg, icon: 'none' });
  162. }
  163. })
  164. }
  165. }
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. /* 1. 页面整体使用flex布局,高度占满屏幕 */
  170. .page-container {
  171. display: flex;
  172. flex-direction: column;
  173. height: 100vh;
  174. background-color: #f5f6fa;
  175. }
  176. .camera-wrapper {
  177. // 相机区域高度固定
  178. height: 450rpx;
  179. }
  180. /* 1. 卡片区域自动撑满剩余空间 */
  181. .scan-result-card {
  182. flex: 1; /* flex: 1 是关键,让此元素占据所有剩余空间 */
  183. display: flex; /* 内部也使用flex布局,方便内容区滚动 */
  184. flex-direction: column;
  185. background-color: #ffffff;
  186. margin: 0 20rpx 20rpx 20rpx;
  187. border-radius: 20rpx;
  188. padding: 30rpx;
  189. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  190. overflow: hidden; /* 防止内容溢出圆角 */
  191. }
  192. .card-header {
  193. display: flex;
  194. align-items: center;
  195. padding-bottom: 25rpx;
  196. border-bottom: 1rpx solid #f0f0f0;
  197. // 头部高度固定,不参与flex缩放
  198. flex-shrink: 0;
  199. }
  200. .header-title {
  201. font-size: 32rpx;
  202. font-weight: bold;
  203. color: #333;
  204. margin-left: 15rpx;
  205. }
  206. .count-badge {
  207. background-color: #e3efff;
  208. color: #409eff;
  209. font-size: 24rpx;
  210. padding: 6rpx 15rpx;
  211. border-radius: 30rpx;
  212. margin-left: 20rpx;
  213. font-weight: 500;
  214. }
  215. /* 2. 列表滚动区域 */
  216. .code-list-scroll {
  217. flex: 1; /* 关键:让滚动区域占据头部和底部之间的所有空间 */
  218. // height: 0; // flex布局中的一个常用技巧,确保flex:1正常工作
  219. padding: 20rpx 0;
  220. }
  221. .empty-tip {
  222. text-align: center;
  223. color: #999;
  224. padding-top: 100rpx;
  225. }
  226. .code-list-grid {
  227. display: grid;
  228. grid-template-columns: 1fr 1fr;
  229. gap: 20rpx 40rpx;
  230. }
  231. .code-item {
  232. font-size: 28rpx;
  233. color: #606266;
  234. word-break: break-all;
  235. }
  236. .footer-buttons {
  237. display: flex;
  238. justify-content: space-between;
  239. gap: 30rpx;
  240. padding-top: 20rpx;
  241. border-top: 1rpx solid #f0f0f0;
  242. // 底部高度固定,不参与flex缩放
  243. flex-shrink: 0;
  244. }
  245. .btn {
  246. flex: 1;
  247. margin: 0;
  248. font-size: 30rpx;
  249. border-radius: 40rpx;
  250. height: 80rpx;
  251. line-height: 80rpx;
  252. background-color: #409eff;
  253. color: #ffffff;
  254. &::after {
  255. border: none;
  256. }
  257. }
  258. </style>