putStorage.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <view class="container">
  3. <scroll-view scroll-y="true" class="scroll-list">
  4. <uni-list>
  5. <uni-list-item border v-for="(item, index) in listArr">
  6. <template v-slot:header>
  7. <view class="slot-box">
  8. <image class="slot-image" src="/static/girder.png" mode="widthFix"
  9. style="width: 50px; height: 50px;"></image>
  10. </view>
  11. </template>
  12. <template v-slot:body>
  13. <view class="ml-6" style="font-size: 14px;">
  14. <view class="name">
  15. {{item.materialName}}
  16. </view>
  17. <view>
  18. {{item.specificationNumber}}
  19. </view>
  20. <view>
  21. {{item.proposedPosition}}
  22. </view>
  23. </view>
  24. </template>
  25. <template v-slot:footer>
  26. <uni-icons type="close" color="#999" size="20" style="position: absolute; right: 20px;"
  27. @click="clearItem(item,index)"></uni-icons>
  28. </template>
  29. </uni-list-item>
  30. </uni-list>
  31. </scroll-view>
  32. <view class="button-container" v-if="type==1">
  33. <button class="btn" type="default" @click='startScan(1)' v-if="isShowStartBtn" :loading="scanLoad">
  34. <uni-icons type="scan"></uni-icons>
  35. 开始扫描
  36. </button>
  37. <button class="btn" type="default" @click='stopScan' v-if="!isShowStartBtn">
  38. <uni-icons type="circle-filled"></uni-icons>
  39. 停止扫描</button>
  40. <button class="btn" type="default" @click="toStorageClick" :loading="manualStorageLoading">
  41. <uni-icons type="upload-filled"></uni-icons>
  42. 一键入库</button>
  43. </view>
  44. <view class="button-container" v-if="type==2">
  45. <button class="btn" type="default" @click='startScan(2)' v-if="isShowStartBtn" :loading="scanLoad">
  46. <uni-icons type="scan"></uni-icons>
  47. 开始扫描</button>
  48. <button class="btn" type="default" @click='stopScan' v-if="!isShowStartBtn">
  49. <uni-icons type="circle-filled"></uni-icons>
  50. 停止扫描</button>
  51. <button class="btn" type="default" @click="toReport" :loading="reportLoad">
  52. <uni-icons type="upload"></uni-icons>
  53. 一键检测</button>
  54. </view>
  55. </view>
  56. </template>
  57. <script setup>
  58. import {
  59. onMounted,
  60. ref,
  61. watch
  62. } from "vue";
  63. import {
  64. onLoad,
  65. onReady,
  66. onUnload
  67. } from '@dcloudio/uni-app'
  68. import {
  69. getArrValue,
  70. arrToId,
  71. arrToKey
  72. } from "js-fast-way";
  73. import mainApi from '~api/storage.js';
  74. // 获取 module
  75. const rfidModule = uni.requireNativePlugin("DeviceModule_RFID");
  76. //渲染完成
  77. onReady(() => {
  78. // #ifdef APP-PLUS
  79. rfidModuleInit()
  80. // #endif
  81. })
  82. onLoad((options) => {
  83. type.value = options.type
  84. codeIds.value = ''
  85. storageIds.value = ''
  86. if (options.type == 1) {
  87. title.value = '样品入库'
  88. } else {
  89. title.value = '样品检测'
  90. }
  91. uni.setNavigationBarTitle({
  92. title: title.value
  93. })
  94. })
  95. const title = ref('')
  96. const type = ref('1')
  97. // //按键操作
  98. const isRfidInit = ref(true)
  99. const rfidModuleInit = () => {
  100. isRfidInit.value = true
  101. uni.showLoading({
  102. title: 'RFID模块加载中...',
  103. mask: true,
  104. });
  105. /**
  106. 手机按键监听事件,可在此编写一些逻辑,如使用按键触发扫描,更多详细信息请查阅uni官方文档
  107. 需要注意:退出界面必须移除监听,否则再进入页面重复注册监听会出现多次触发、回调失效的问题
  108. */
  109. plus.key.addEventListener('keydown', keyListener);
  110. //初始化
  111. setTimeout(() => {
  112. // 使用模块前必须先初始化RDIF模块
  113. let {
  114. code
  115. } = rfidModule.init();
  116. if (code === 0) {
  117. uni.hideLoading();
  118. } else if (code === -1) {
  119. uni.hideLoading();
  120. uni.showToast({
  121. title: 'RFID模块加载失败',
  122. icon: 'error',
  123. duration: 3000
  124. })
  125. } else {
  126. uni.hideLoading();
  127. }
  128. isRfidInit.value = false
  129. }, 400);
  130. }
  131. //按键操作
  132. const keyListener = ({
  133. keyCode
  134. }) => {
  135. if (keyCode === 293 || keyCode === 312) {
  136. if (isScan.value) {
  137. stopScan()
  138. } else {
  139. startScan(type.value)
  140. }
  141. }
  142. }
  143. //开始扫描
  144. const isShowStartBtn = ref(true)
  145. const scanLoad = ref(false)
  146. const isScan = ref(false)
  147. const startScan = (type) => {
  148. isShowStartBtn.value = false
  149. if (isRfidInit.value) return
  150. isScan.value = true
  151. rfidModule.startScan((res) => {
  152. if (res.code === 0) {
  153. uni.showToast({
  154. icon: "success",
  155. title: '开启扫描成功'
  156. })
  157. } else if (res.code === 1) {
  158. startScanData(res.data, type)
  159. }
  160. })
  161. }
  162. //扫描结果处理
  163. const codeIds = ref('')
  164. const scanDatas = ref([])
  165. const startScanData = async (data, type) => {
  166. const arr = getArrValue(data)
  167. let epcs = scanDatas.value
  168. for (let i = 0; i < arr.length; i++) {
  169. const epc = arr[i].epc
  170. if (epcs.indexOf(epc) === -1) {
  171. epcs.push(epc)
  172. }
  173. }
  174. scanDatas.value = epcs
  175. if (epcs.length === 1) {
  176. codeIds.value = epcs[0]
  177. } else {
  178. codeIds.value = epcs.join(',')
  179. }
  180. getResult(type,codeIds.value)
  181. }
  182. const storageIds = ref('')
  183. //获取扫描结果
  184. const getResult = async (type,ids) => {
  185. console.log('发请求', type);
  186. console.log(ids, 'ids');
  187. const {
  188. response,
  189. error,
  190. code,
  191. data
  192. } = await mainApi.getSelectRfidListById({
  193. rids:ids,
  194. state: type
  195. });
  196. //处理数据
  197. if (!error && code === 200) {
  198. console.log(listArr.value, ' listArr.value');
  199. listArr.value = getArrValue(data);
  200. storageIds.value = arrToId(listArr.value)
  201. } else {
  202. listArr.value = [];
  203. storageIds.value = ''
  204. }
  205. }
  206. //停止扫描
  207. const stopScan = () => {
  208. const {
  209. code
  210. } = rfidModule.stopScan()
  211. if (code === 0) {
  212. isScan.value = false
  213. uni.showToast({
  214. icon: "success",
  215. title: '关闭扫描成功'
  216. })
  217. isShowStartBtn.value = true
  218. } else {
  219. uni.showToast({
  220. icon: "error",
  221. title: res.message
  222. })
  223. }
  224. }
  225. //页面卸载
  226. onUnload(() => {
  227. // #ifdef APP-PLUS
  228. plus.key.removeEventListener('keydown', keyListener)
  229. // 使用完毕必须释放RDIF模块
  230. rfidModule.free();
  231. // #endif
  232. })
  233. const listArr = ref([])
  234. //一键入库
  235. const manualStorageLoading = ref(false)
  236. const toStorageClick = async () => {
  237. if (!isShowStartBtn.value) {
  238. uni.showToast({
  239. title: '请先停止扫描',
  240. icon: 'none',
  241. duration: 3000
  242. })
  243. return;
  244. }
  245. manualStorageLoading.value = true;
  246. console.log(storageIds.value, 'storageIds.value');
  247. if (storageIds.value.length < 1) {
  248. return;
  249. }
  250. const {
  251. error,
  252. code,
  253. msg,
  254. response
  255. } = await mainApi.update({
  256. id: storageIds.value,
  257. sampleStatus: 2,
  258. });
  259. manualStorageLoading.value = false;
  260. if (!error && code === 200) {
  261. uni.showToast({
  262. title: '操作成功',
  263. duration: 2000,
  264. mask: true
  265. });
  266. getResult(storageIds.value,1).then();
  267. } else {
  268. uni.showToast({
  269. title: '操作失败',
  270. duration: 2000,
  271. mask: true
  272. });
  273. }
  274. }
  275. const clearItem = (item, index) => {
  276. listArr.value.splice(index, 1);
  277. storageIds.value = arrToId(listArr.value)
  278. }
  279. //一键检测
  280. const reportLoad = ref(false)
  281. const toReport = async () => {
  282. if (!isShowStartBtn.value) {
  283. uni.showToast({
  284. title: '请先停止扫描',
  285. icon: 'none',
  286. duration: 3000
  287. })
  288. return;
  289. }
  290. reportLoad.value = true;
  291. if (storageIds.value.length < 1) {
  292. return;
  293. }
  294. const {
  295. error,
  296. code,
  297. msg,
  298. response
  299. } = await mainApi.update({
  300. id: storageIds.value,
  301. sampleStatus: 4,
  302. expCount: 0
  303. });
  304. reportLoad.value = false;
  305. if (!error && code === 200) {
  306. uni.showToast({
  307. title: '操作成功',
  308. duration: 2000,
  309. mask: true
  310. });
  311. getResult(2,storageIds.value).then();
  312. } else {
  313. uni.showToast({
  314. title: '操作失败',
  315. duration: 2000,
  316. mask: true
  317. });
  318. }
  319. }
  320. </script>
  321. <style lang="scss" scoped>
  322. .name {
  323. font-weight: bold;
  324. }
  325. .container {
  326. display: flex;
  327. flex-direction: column;
  328. height: 100vh;
  329. /* 使容器高度为视口高度 */
  330. }
  331. .scroll-list {
  332. flex: 1;
  333. /* 占据剩余空间 */
  334. overflow-y: auto;
  335. /* 纵向滚动 */
  336. }
  337. .button-container {
  338. display: flex;
  339. justify-content: space-around;
  340. /* 两个按钮均匀分布 */
  341. padding: 10px;
  342. background-color: #fff;
  343. /* 背景色 */
  344. box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  345. /* 阴影效果 */
  346. position: fixed;
  347. /* 固定在底部 */
  348. bottom: 0;
  349. /* 靠近底部 */
  350. left: 0;
  351. /* 左对齐 */
  352. right: 0;
  353. /* 右对齐 */
  354. z-index: 100;
  355. }
  356. .btn {
  357. flex: 1;
  358. /* 每个按钮占据相同的空间 */
  359. margin: 0 5px;
  360. /* 按钮之间的间距 */
  361. border: none;
  362. border-radius: 5px;
  363. cursor: pointer;
  364. color: black;
  365. }
  366. </style>