putStorage.vue 8.4 KB

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