putStorage.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. if (storageIds.value.length < 1) {
  246. return;
  247. }
  248. manualStorageLoading.value = true;
  249. const {
  250. error,
  251. code,
  252. msg,
  253. response
  254. } = await mainApi.update({
  255. id: storageIds.value,
  256. sampleStatus: 2,
  257. });
  258. manualStorageLoading.value = false;
  259. if (!error && code === 200) {
  260. uni.showToast({
  261. title: '操作成功',
  262. duration: 2000,
  263. mask: true
  264. });
  265. getResult(storageIds.value,1).then();
  266. } else {
  267. uni.showToast({
  268. title: '操作失败',
  269. duration: 2000,
  270. mask: true
  271. });
  272. }
  273. }
  274. const clearItem = (item, index) => {
  275. listArr.value.splice(index, 1);
  276. storageIds.value = arrToId(listArr.value)
  277. }
  278. //一键检测
  279. const reportLoad = ref(false)
  280. const toReport = async () => {
  281. if (!isShowStartBtn.value) {
  282. uni.showToast({
  283. title: '请先停止扫描',
  284. icon: 'none',
  285. duration: 3000
  286. })
  287. return;
  288. }
  289. if (storageIds.value.length < 1) {
  290. return;
  291. }
  292. reportLoad.value = true;
  293. const {
  294. error,
  295. code,
  296. msg,
  297. response
  298. } = await mainApi.update({
  299. id: storageIds.value,
  300. sampleStatus: 4,
  301. expCount: 0
  302. });
  303. reportLoad.value = false;
  304. if (!error && code === 200) {
  305. uni.showToast({
  306. title: '操作成功',
  307. duration: 2000,
  308. mask: true
  309. });
  310. getResult(2,storageIds.value).then();
  311. } else {
  312. uni.showToast({
  313. title: '操作失败',
  314. duration: 2000,
  315. mask: true
  316. });
  317. }
  318. }
  319. </script>
  320. <style lang="scss" scoped>
  321. .name {
  322. font-weight: bold;
  323. }
  324. .container {
  325. display: flex;
  326. flex-direction: column;
  327. height: 100vh;
  328. /* 使容器高度为视口高度 */
  329. }
  330. .scroll-list {
  331. flex: 1;
  332. /* 占据剩余空间 */
  333. overflow-y: auto;
  334. /* 纵向滚动 */
  335. }
  336. .button-container {
  337. display: flex;
  338. justify-content: space-around;
  339. /* 两个按钮均匀分布 */
  340. padding: 10px;
  341. background-color: #fff;
  342. /* 背景色 */
  343. box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  344. /* 阴影效果 */
  345. position: fixed;
  346. /* 固定在底部 */
  347. bottom: 0;
  348. /* 靠近底部 */
  349. left: 0;
  350. /* 左对齐 */
  351. right: 0;
  352. /* 右对齐 */
  353. z-index: 100;
  354. }
  355. .btn {
  356. flex: 1;
  357. /* 每个按钮占据相同的空间 */
  358. margin: 0 5px;
  359. /* 按钮之间的间距 */
  360. border: none;
  361. border-radius: 5px;
  362. cursor: pointer;
  363. color: black;
  364. }
  365. </style>