index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <Water></Water>
  3. <view class="nav" :style="{ paddingTop: statusBarHeight + 'px' }">
  4. <text class="nav-back" :style="{ top: statusBarHeight + 'px' }" @click="onBack"></text>
  5. <text class="nav-title">药品追溯码查询</text>
  6. </view>
  7. <view class="page" :style="{
  8. marginTop: statusBarHeight + 44 + 'px',
  9. height: pageHeight + 'px',
  10. }">
  11. <view class="scan-tip" @click="handleScanTipClick">
  12. <uni-icons type="help" size="18" color="#2c69ff"></uni-icons>
  13. <text :style="{ marginLeft: '5rpx', paddingBottom: '5rpx' }">可扫码生产企业及药品</text>
  14. </view>
  15. <view class="scan-card" @click="handleScan">
  16. <view class="scan-card-content">
  17. <image src="../../static/images/camera.png" mode="scaleToFill" class="scan-icon" />
  18. <text class="scan-text">扫码查询</text>
  19. </view>
  20. </view>
  21. <view class="hint">请扫描药品最小销售包装上的追溯码</view>
  22. <view class="input-wrap">
  23. <input class="input" v-model="traceCode" maxlength="23" @input="onTraceInput" placeholder="请输入 20 位药品追溯码" />
  24. </view>
  25. <view class="count">已输入 {{ traceCode.replace(/\s+/g, "").length }}/20 位</view>
  26. <button class="query-btn" :class="{ disabled: !canQuery }" @click="onQuery">
  27. 查询
  28. </button>
  29. <text class="tip">若扫码失败,可手动输入追溯码查询</text>
  30. <view class="section-title">扫码历史</view>
  31. <scroll-view class="history-scroll" v-if="history.length != 0 && !loading" scroll-y="true"
  32. @scrolltolower="onHistoryScrollToLower">
  33. <view class="history-list">
  34. <view class="history-item" v-for="item in history" :key="item.code" @click="onTapHistory(item)">
  35. <view class="history-left">
  36. <view class="history-title">{{ item.physicName }}</view>
  37. <view class="history-sub">追溯码:{{ item.tracCode }}</view>
  38. </view>
  39. <text class="arrow">›</text>
  40. </view>
  41. <view class="loading-row" v-if="history.length < totalCount">
  42. <view class="loading-wrapper">
  43. <image class="loading-icon" :src="loadingImg" />
  44. </view>
  45. </view>
  46. </view>
  47. </scroll-view>
  48. <view v-else-if="!loading" style="margin-top: 20rpx">
  49. <Empty text="暂无扫码历史" />
  50. </view>
  51. <view class="loading-view" v-else>
  52. <image class="loading-icon" src="../../static/images/loading.png" mode="scaleToFill"
  53. :style="{ width: '50rpx', height: '50rpx' }" />
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import loadingImg from "../../static/images/loading.png";
  59. import Empty from "../../wigets/empty.vue";
  60. import Water from "@/components/water/water.vue";
  61. import request from "../../request/index.js";
  62. export default {
  63. components: {
  64. Empty,
  65. Water,
  66. },
  67. data() {
  68. return {
  69. statusBarHeight: 20,
  70. pageHeight: 0,
  71. traceCode: "",
  72. history: [],
  73. totalCount: 0,
  74. pageNum: 1,
  75. pageSize: 7,
  76. loading: true,
  77. isLoading: false,
  78. };
  79. },
  80. onShow() {
  81. this.history = [];
  82. this.totalCount = 0;
  83. this.pageNum = 1;
  84. this.isLoading = false;
  85. // 从详情页返回时,强制清空输入框 traceCode
  86. this.traceCode = "";
  87. this.getHistoryData();
  88. },
  89. onLoad() {
  90. const info = uni.getSystemInfoSync();
  91. this.statusBarHeight = info.statusBarHeight || 20;
  92. const navH = 44 + this.statusBarHeight;
  93. const winH =
  94. info.windowHeight ||
  95. (info.screenHeight ? info.screenHeight - this.statusBarHeight : 0);
  96. if (winH) {
  97. this.pageHeight = Math.max(0, winH - navH);
  98. }
  99. },
  100. computed: {
  101. canQuery() {
  102. return this.traceCode.replace(/\s+/g, "").length === 20;
  103. },
  104. },
  105. methods: {
  106. onBack() {
  107. try {
  108. uni.navigateBack();
  109. } catch (e) { }
  110. },
  111. handleScanTipClick() {
  112. uni.navigateTo({
  113. url: "/traceCodePackages/traceabilityCodeQuery/pages/scanRange/index",
  114. });
  115. },
  116. formatTraceCode(code) {
  117. const raw = code.replace(/\s+/g, "").slice(0, 20);
  118. const parts = raw.match(/.{1,5}/g) || [];
  119. return parts.join(" ");
  120. },
  121. onTraceInput(e) {
  122. const val = (e && e.detail && e.detail.value) || "";
  123. this.traceCode = this.formatTraceCode(val);
  124. },
  125. getHistoryData() {
  126. request("/bills/getScanHistory", {
  127. pageNum: this.pageNum,
  128. pageSize: this.pageSize,
  129. path: "/traceabilityCodeQuery/pages/index.vue",
  130. }).then((res) => {
  131. if (res.code == 200) {
  132. const { total, list } = res.data || {};
  133. this.totalCount = total || 0;
  134. this.history = Array.isArray(list)
  135. ? [...this.history, ...list]
  136. : [...this.history];
  137. }
  138. this.isLoading = false;
  139. this.loading = false;
  140. });
  141. },
  142. handleScan() {
  143. uni.scanCode({
  144. onlyFromCamera: false,
  145. success: (res) => {
  146. if (!res.result) {
  147. console.log("scanCode fail", res);
  148. uni.showToast({
  149. title: "扫码失败,请重试",
  150. icon: "none",
  151. duration: 2000,
  152. });
  153. return;
  154. }
  155. this.traceCode = this.formatTraceCode(res.result || "");
  156. if (res.result.length !== 20) {
  157. uni.showToast({
  158. title: "追溯码通常为20位,请尝试重新扫描完整的条形码",
  159. icon: "none",
  160. duration: 2000,
  161. });
  162. } else {
  163. uni.showToast({
  164. title: "扫码成功",
  165. icon: "none",
  166. duration: 2000,
  167. });
  168. this.onTapHistory({ tracCode: res.result });
  169. }
  170. },
  171. fail: (err) => {
  172. if (err.errMsg.includes("scanCode:fail cancel")) {
  173. console.log("用户取消了扫码");
  174. console.log("scan fail", err);
  175. } else {
  176. uni.showToast({
  177. title: "扫码失败,请重试",
  178. icon: "none",
  179. duration: 2000,
  180. }); // 这里可以看到具体失败原因
  181. }
  182. },
  183. });
  184. },
  185. onQuery() {
  186. if (!this.canQuery) return;
  187. this.onTapHistory({ tracCode: this.traceCode.replace(/\s+/g, "") });
  188. },
  189. onHistoryScrollToLower() {
  190. if (this.isLoading) return;
  191. if (this.history.length >= this.totalCount) return;
  192. this.isLoading = true;
  193. this.pageNum++;
  194. this.getHistoryData();
  195. },
  196. onTapHistory(item) {
  197. uni.navigateTo({
  198. url:
  199. "/traceCodePackages/traceabilityCodeQuery/pages/detail/index" +
  200. (item ? "?traceCode=" + item.tracCode : ""),
  201. });
  202. },
  203. },
  204. };
  205. </script>
  206. <style>
  207. .scan-tip {
  208. margin-top: 30rpx;
  209. color: #2c69ff;
  210. font-size: 24rpx;
  211. width: 100%;
  212. text-align: right;
  213. padding-bottom: 40rpx;
  214. display: flex;
  215. justify-content: flex-end;
  216. align-items: center;
  217. }
  218. .nav {
  219. position: fixed;
  220. top: 0;
  221. left: 0;
  222. right: 0;
  223. height: 44px;
  224. background-color: #2c69ff;
  225. display: flex;
  226. align-items: center;
  227. justify-content: center;
  228. }
  229. .nav-back {
  230. position: absolute;
  231. left: 40rpx;
  232. top: 12rpx;
  233. width: 20rpx;
  234. height: 20rpx;
  235. color: #fff;
  236. border-left: 3rpx solid #fff;
  237. border-bottom: 3rpx solid #fff;
  238. transform: rotate(45deg) translateY(49rpx);
  239. margin-left: 34rpx;
  240. }
  241. .nav-title {
  242. font-size: 36rpx;
  243. color: #fff;
  244. font-weight: 700;
  245. }
  246. .page {
  247. box-sizing: border-box;
  248. display: flex;
  249. flex-direction: column;
  250. align-items: center;
  251. padding: 0 55rpx;
  252. padding-bottom: env(safe-area-inset-bottom);
  253. background-color: rgb(243, 246, 249);
  254. overflow: hidden;
  255. }
  256. .scan-card {
  257. flex-shrink: 0;
  258. width: 100%;
  259. display: flex;
  260. justify-content: center;
  261. }
  262. .scan-card-content {
  263. width: 320rpx;
  264. height: 320rpx;
  265. background-color: #fff;
  266. border-radius: 20rpx;
  267. box-shadow: 0 1rpx 16rpx rgba(0, 0, 0, 0.1);
  268. display: flex;
  269. flex-direction: column;
  270. align-items: center;
  271. justify-content: center;
  272. }
  273. .scan-icon {
  274. width: 100rpx;
  275. height: 100rpx;
  276. margin-bottom: 16rpx;
  277. }
  278. .scan-text {
  279. font-size: 32rpx;
  280. color: #2c69ff;
  281. font-weight: 1000;
  282. }
  283. .hint {
  284. margin-top: 35rpx;
  285. color: #727a86;
  286. font-size: 24rpx;
  287. width: 100%;
  288. text-align: center;
  289. border-bottom: 1rpx dashed #727a8663;
  290. padding-bottom: 40rpx;
  291. }
  292. .input-wrap {
  293. width: 640rpx;
  294. margin-top: 40rpx;
  295. }
  296. .input {
  297. box-sizing: border-box;
  298. width: 100%;
  299. height: 80rpx;
  300. border: 2rpx solid #2c69ff;
  301. border-radius: 12rpx;
  302. padding: 0 20rpx;
  303. font-size: 28rpx;
  304. }
  305. .count {
  306. width: 640rpx;
  307. text-align: right;
  308. color: #99a1ad;
  309. font-size: 22rpx;
  310. margin-top: 8rpx;
  311. }
  312. .query-btn {
  313. flex-shrink: 0;
  314. width: 640rpx;
  315. height: 80rpx;
  316. line-height: 80rpx;
  317. text-align: center;
  318. margin-top: 40rpx;
  319. background-color: #2c69ff;
  320. color: #fff;
  321. font-size: 30rpx;
  322. }
  323. .query-btn.disabled {
  324. background-color: rgb(192, 210, 224);
  325. color: #fff;
  326. }
  327. .tip {
  328. margin-top: 12rpx;
  329. color: #99a1ad;
  330. font-size: 22rpx;
  331. }
  332. .section-title {
  333. width: 640rpx;
  334. margin-top: 30rpx;
  335. font-size: 28rpx;
  336. color: #000;
  337. font-weight: 1000;
  338. }
  339. .history-list {
  340. width: 640rpx;
  341. }
  342. .history-scroll {
  343. flex: 1;
  344. height: 0;
  345. width: 640rpx;
  346. }
  347. .history-item {
  348. background-color: #fff;
  349. border-radius: 16rpx;
  350. padding: 24rpx;
  351. margin-top: 16rpx;
  352. display: flex;
  353. align-items: center;
  354. justify-content: space-between;
  355. }
  356. .history-title {
  357. font-size: 28rpx;
  358. color: #2c69ff;
  359. }
  360. .history-sub {
  361. margin-top: 6rpx;
  362. font-size: 24rpx;
  363. color: #727a86;
  364. }
  365. .arrow {
  366. font-size: 40rpx;
  367. color: #cbd3e6;
  368. }
  369. .loading-row {
  370. justify-content: center;
  371. }
  372. .loading-wrapper {
  373. width: 100%;
  374. height: 76rpx;
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. }
  379. .loading-icon {
  380. width: 40rpx;
  381. height: 40rpx;
  382. animation: spin 1s linear infinite;
  383. }
  384. @keyframes spin {
  385. from {
  386. transform: rotate(0deg);
  387. }
  388. to {
  389. transform: rotate(360deg);
  390. }
  391. }
  392. .loading-view {
  393. margin-top: 20rpx;
  394. display: flex;
  395. flex-direction: column;
  396. align-items: center;
  397. justify-content: space-around;
  398. }
  399. </style>