scan_code.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. const code = e.detail.result;
  98. console.log('扫描到的内容:', code);
  99. this.getCodeNumber(code)
  100. },
  101. // 相机初始化失败
  102. onCameraError(e) {
  103. console.log(e.detail);
  104. uni.showModal({
  105. title: '相机错误',
  106. content: '无法启动相机,请检查权限或重启应用',
  107. showCancel: false
  108. })
  109. },
  110. // 播放提示音
  111. playBeep() {
  112. this.audioContext.play();
  113. },
  114. // 格式化序号
  115. formatIndex(num) {
  116. return num < 10 ? '0' + num : num;
  117. },
  118. // 清空事件
  119. clearAll() {
  120. if (this.totalCount === 0) return;
  121. uni.showModal({
  122. title: '确认',
  123. content: '您确定要清空所有已扫描的二维码吗?',
  124. success: (res) => {
  125. if (res.confirm) {
  126. this.codeList = [];
  127. this.numberList = [];
  128. uni.showToast({ title: '已清空', icon: 'success' });
  129. }
  130. }
  131. });
  132. },
  133. // 提交事件
  134. submit() {
  135. if (this.totalCount === 0) {
  136. uni.showToast({ title: '没有可提交的内容', icon: 'none' });
  137. return;
  138. }
  139. console.log('提交的二维码列表:', this.codeList);
  140. uni.showLoading({ title: '提交中...' });
  141. let obj = {};
  142. obj.storeId = 1;
  143. obj.qrcodeIds = this.codeList;
  144. active(obj).then(res=>{
  145. uni.hideLoading();
  146. if(res.code == 0){
  147. uni.showToast({ title: '操作成功', icon: 'none' });
  148. this.numberList = []
  149. this.codeList = [];
  150. uni.navigateBack();
  151. }else{
  152. uni.showToast({ title: res.msg, icon: 'none' });
  153. }
  154. })
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. /* 1. 页面整体使用flex布局,高度占满屏幕 */
  161. .page-container {
  162. display: flex;
  163. flex-direction: column;
  164. height: 100vh;
  165. background-color: #f5f6fa;
  166. }
  167. .camera-wrapper {
  168. // 相机区域高度固定
  169. height: 450rpx;
  170. }
  171. /* 1. 卡片区域自动撑满剩余空间 */
  172. .scan-result-card {
  173. flex: 1; /* flex: 1 是关键,让此元素占据所有剩余空间 */
  174. display: flex; /* 内部也使用flex布局,方便内容区滚动 */
  175. flex-direction: column;
  176. background-color: #ffffff;
  177. margin: 0 20rpx 20rpx 20rpx;
  178. border-radius: 20rpx;
  179. padding: 30rpx;
  180. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  181. overflow: hidden; /* 防止内容溢出圆角 */
  182. }
  183. .card-header {
  184. display: flex;
  185. align-items: center;
  186. padding-bottom: 25rpx;
  187. border-bottom: 1rpx solid #f0f0f0;
  188. // 头部高度固定,不参与flex缩放
  189. flex-shrink: 0;
  190. }
  191. .header-title {
  192. font-size: 32rpx;
  193. font-weight: bold;
  194. color: #333;
  195. margin-left: 15rpx;
  196. }
  197. .count-badge {
  198. background-color: #e3efff;
  199. color: #409eff;
  200. font-size: 24rpx;
  201. padding: 6rpx 15rpx;
  202. border-radius: 30rpx;
  203. margin-left: 20rpx;
  204. font-weight: 500;
  205. }
  206. /* 2. 列表滚动区域 */
  207. .code-list-scroll {
  208. flex: 1; /* 关键:让滚动区域占据头部和底部之间的所有空间 */
  209. // height: 0; // flex布局中的一个常用技巧,确保flex:1正常工作
  210. padding: 20rpx 0;
  211. }
  212. .empty-tip {
  213. text-align: center;
  214. color: #999;
  215. padding-top: 100rpx;
  216. }
  217. .code-list-grid {
  218. display: grid;
  219. grid-template-columns: 1fr 1fr;
  220. gap: 20rpx 40rpx;
  221. }
  222. .code-item {
  223. font-size: 28rpx;
  224. color: #606266;
  225. word-break: break-all;
  226. }
  227. .footer-buttons {
  228. display: flex;
  229. justify-content: space-between;
  230. gap: 30rpx;
  231. padding-top: 20rpx;
  232. border-top: 1rpx solid #f0f0f0;
  233. // 底部高度固定,不参与flex缩放
  234. flex-shrink: 0;
  235. }
  236. .btn {
  237. flex: 1;
  238. margin: 0;
  239. font-size: 30rpx;
  240. border-radius: 40rpx;
  241. height: 80rpx;
  242. line-height: 80rpx;
  243. background-color: #409eff;
  244. color: #ffffff;
  245. &::after {
  246. border: none;
  247. }
  248. }
  249. </style>