scan_code.vue 8.9 KB

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