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. console.log(codeIds.value, 'codeIds.value');
  178. } else {
  179. codeIds.value = epcs.join(',')
  180. }
  181. getResult(type,codeIds.value)
  182. }
  183. const storageIds = ref('')
  184. //获取扫描结果
  185. const getResult = async (type,ids) => {
  186. console.log('发请求', type);
  187. console.log(ids, 'ids');
  188. const {
  189. response,
  190. error,
  191. code,
  192. data
  193. } = await mainApi.getSelectRfidListById({
  194. rids:ids,
  195. state: type
  196. });
  197. console.log(response, 'response22222222222221');
  198. //处理数据
  199. if (!error && code === 200) {
  200. console.log(listArr.value, ' listArr.value');
  201. listArr.value = getArrValue(data);
  202. storageIds.value = arrToId(listArr.value)
  203. } else {
  204. listArr.value = [];
  205. storageIds.value = ''
  206. }
  207. }
  208. //停止扫描
  209. const stopScan = () => {
  210. const {
  211. code
  212. } = rfidModule.stopScan()
  213. if (code === 0) {
  214. isScan.value = false
  215. uni.showToast({
  216. icon: "success",
  217. title: '关闭扫描成功'
  218. })
  219. isShowStartBtn.value = true
  220. } else {
  221. uni.showToast({
  222. icon: "error",
  223. title: res.message
  224. })
  225. }
  226. }
  227. //页面卸载
  228. onUnload(() => {
  229. // #ifdef APP-PLUS
  230. plus.key.removeEventListener('keydown', keyListener)
  231. // 使用完毕必须释放RDIF模块
  232. rfidModule.free();
  233. // #endif
  234. })
  235. const listArr = ref([])
  236. //一键入库
  237. const manualStorageLoading = ref(false)
  238. const toStorageClick = async () => {
  239. if (!isShowStartBtn.value) {
  240. uni.showToast({
  241. title: '请先停止扫描',
  242. icon: 'none',
  243. duration: 3000
  244. })
  245. return;
  246. }
  247. manualStorageLoading.value = true;
  248. console.log(storageIds.value, 'storageIds.value');
  249. if (storageIds.value.length < 1) {
  250. return;
  251. }
  252. const {
  253. error,
  254. code,
  255. msg,
  256. response
  257. } = await mainApi.update({
  258. id: storageIds.value,
  259. sampleStatus: 2,
  260. });
  261. console.log(response, 'response11111111111111');
  262. manualStorageLoading.value = false;
  263. if (!error && code === 200) {
  264. uni.showToast({
  265. title: '操作成功',
  266. duration: 2000,
  267. mask: true
  268. });
  269. getResult(storageIds.value,1).then();
  270. } else {
  271. uni.showToast({
  272. title: '操作失败',
  273. duration: 2000,
  274. mask: true
  275. });
  276. }
  277. }
  278. const clearItem = (item, index) => {
  279. listArr.value.splice(index, 1)
  280. }
  281. //一键检测
  282. const reportLoad = ref(false)
  283. const toReport = async () => {
  284. if (!isShowStartBtn.value) {
  285. uni.showToast({
  286. title: '请先停止扫描',
  287. icon: 'none',
  288. duration: 3000
  289. })
  290. return;
  291. }
  292. reportLoad.value = true;
  293. if (storageIds.value.length < 1) {
  294. return;
  295. }
  296. const {
  297. error,
  298. code,
  299. msg,
  300. response
  301. } = await mainApi.update({
  302. id: storageIds.value,
  303. sampleStatus: 4,
  304. expCount: 0
  305. });
  306. console.log(response, 'response');
  307. reportLoad.value = false;
  308. if (!error && code === 200) {
  309. uni.showToast({
  310. title: '操作成功',
  311. duration: 2000,
  312. mask: true
  313. });
  314. getResult(2,storageIds.value).then();
  315. } else {
  316. uni.showToast({
  317. title: '操作失败',
  318. duration: 2000,
  319. mask: true
  320. });
  321. }
  322. }
  323. </script>
  324. <style lang="scss" scoped>
  325. .name {
  326. font-weight: bold;
  327. }
  328. .container {
  329. display: flex;
  330. flex-direction: column;
  331. height: 100vh;
  332. /* 使容器高度为视口高度 */
  333. }
  334. .scroll-list {
  335. flex: 1;
  336. /* 占据剩余空间 */
  337. overflow-y: auto;
  338. /* 纵向滚动 */
  339. }
  340. .button-container {
  341. display: flex;
  342. justify-content: space-around;
  343. /* 两个按钮均匀分布 */
  344. padding: 10px;
  345. background-color: #fff;
  346. /* 背景色 */
  347. box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  348. /* 阴影效果 */
  349. position: fixed;
  350. /* 固定在底部 */
  351. bottom: 0;
  352. /* 靠近底部 */
  353. left: 0;
  354. /* 左对齐 */
  355. right: 0;
  356. /* 右对齐 */
  357. z-index: 100;
  358. }
  359. .btn {
  360. flex: 1;
  361. /* 每个按钮占据相同的空间 */
  362. margin: 0 5px;
  363. /* 按钮之间的间距 */
  364. border: none;
  365. border-radius: 5px;
  366. cursor: pointer;
  367. color: black;
  368. }
  369. </style>