scan_code.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 = this.storeId;
  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. setTimeout(()=>{
  172. uni.navigateBack();
  173. },1000)
  174. }else{
  175. uni.showToast({ title: res.msg, icon: 'none' });
  176. }
  177. })
  178. }else if(this.type == 2){
  179. writeOff(obj).then(res=>{
  180. uni.hideLoading();
  181. if(res.code == 0){
  182. uni.showToast({ title: '操作成功', icon: 'none' });
  183. this.numberList = []
  184. this.codeList = [];
  185. uni.navigateBack();
  186. }else{
  187. uni.showToast({ title: res.msg, icon: 'none' });
  188. }
  189. })
  190. }
  191. }
  192. }
  193. }
  194. </script>
  195. <style lang="scss" scoped>
  196. /* 1. 页面整体使用flex布局,高度占满屏幕 */
  197. .page-container {
  198. display: flex;
  199. flex-direction: column;
  200. height: 100vh;
  201. background-color: #f5f6fa;
  202. }
  203. .camera-wrapper {
  204. // 相机区域高度固定
  205. height: 450rpx;
  206. }
  207. /* 1. 卡片区域自动撑满剩余空间 */
  208. .scan-result-card {
  209. flex: 1; /* flex: 1 是关键,让此元素占据所有剩余空间 */
  210. display: flex; /* 内部也使用flex布局,方便内容区滚动 */
  211. flex-direction: column;
  212. background-color: #ffffff;
  213. margin: 0 20rpx 20rpx 20rpx;
  214. border-radius: 20rpx;
  215. padding: 30rpx;
  216. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  217. overflow: hidden; /* 防止内容溢出圆角 */
  218. }
  219. .card-header {
  220. display: flex;
  221. align-items: center;
  222. padding-bottom: 25rpx;
  223. border-bottom: 1rpx solid #f0f0f0;
  224. // 头部高度固定,不参与flex缩放
  225. flex-shrink: 0;
  226. }
  227. .header-title {
  228. font-size: 32rpx;
  229. font-weight: bold;
  230. color: #333;
  231. margin-left: 15rpx;
  232. }
  233. .count-badge {
  234. background-color: #e3efff;
  235. color: #409eff;
  236. font-size: 24rpx;
  237. padding: 6rpx 15rpx;
  238. border-radius: 30rpx;
  239. margin-left: 20rpx;
  240. font-weight: 500;
  241. }
  242. /* 2. 列表滚动区域 */
  243. .code-list-scroll {
  244. flex: 1; /* 关键:让滚动区域占据头部和底部之间的所有空间 */
  245. // height: 0; // flex布局中的一个常用技巧,确保flex:1正常工作
  246. padding: 20rpx 0;
  247. max-height: 300px;
  248. }
  249. .empty-tip {
  250. text-align: center;
  251. color: #999;
  252. padding-top: 100rpx;
  253. }
  254. .code-list-grid {
  255. display: grid;
  256. grid-template-columns: 1fr 1fr;
  257. gap: 20rpx 40rpx;
  258. }
  259. .code-item {
  260. font-size: 28rpx;
  261. color: #606266;
  262. word-break: break-all;
  263. }
  264. .footer-buttons {
  265. display: flex;
  266. justify-content: space-between;
  267. gap: 30rpx;
  268. padding-top: 20rpx;
  269. border-top: 1rpx solid #f0f0f0;
  270. // 底部高度固定,不参与flex缩放
  271. flex-shrink: 0;
  272. }
  273. .btn {
  274. flex: 1;
  275. margin: 0;
  276. font-size: 30rpx;
  277. border-radius: 40rpx;
  278. height: 80rpx;
  279. line-height: 80rpx;
  280. background-color: #409eff;
  281. color: #ffffff;
  282. &::after {
  283. border: none;
  284. }
  285. }
  286. </style>