scan_code.vue 7.5 KB

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