scan_code.vue 7.6 KB

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