meLaboratory.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <!-- 我的实验室 -->
  2. <template>
  3. <view id="meLaboratory">
  4. <view class="top-picker-max-box">
  5. <view class="top-picker-box">
  6. <picker @change="facultyChange" :value="facultyIndex" :range="facultyArray">
  7. <view class="picker-view">
  8. <view>{{!facultyIndex?'学院':facultyArray[facultyIndex]}}</view>
  9. <img class="picker-img" src="@/images/icon_06.png" alt="">
  10. </view>
  11. </picker>
  12. </view>
  13. <view class="top-picker-box">
  14. <picker @change="subjectChange" :value="subjectIndex" :range="subjectArray">
  15. <view class="picker-view">
  16. <view>{{!subjectIndex?'分类':subjectArray[subjectIndex]}}</view>
  17. <img class="picker-img" src="@/images/icon_06.png" alt="">
  18. </view>
  19. </picker>
  20. </view>
  21. <view class="top-picker-box">
  22. <picker @change="levelChange" :value="levelIndex" :range="levelArray">
  23. <view class="picker-view">
  24. <view>{{!levelIndex?'分级':levelArray[levelIndex]}}</view>
  25. <img class="picker-img" src="@/images/icon_06.png" alt="">
  26. </view>
  27. </picker>
  28. </view>
  29. </view>
  30. <view class="for-max-box">
  31. <img class="null-img" v-if="!dataList[0]" src="@/images/null-data-1.png">
  32. <view class="for-box" v-for="(item,index) in dataList" :key="index" @click="laboratoryInfo(item)">
  33. <view class="title-box">
  34. <view :style="'border:1rpx solid '+item.fiedColor+';background:#fff;color:'+item.fiedColor+';'">{{item.levelName}}</view>
  35. <view>{{item.name}}</view>
  36. <img src="@/images/icon_04.png">
  37. </view>
  38. <view class="address-box"><span>{{item.typeName}}</span>{{item.deptName}}</view>
  39. <view class="address-box-two">
  40. <img src="@/images/icon_14.png">
  41. <view>{{item.subAddrrStr}}</view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import $mqtt from '@/utils/mqtt.min.js';
  49. import { config } from '@/api/request/config.js'
  50. import { laboratoryList,subject_class,listDepartments,mangerControl,laboratoryInfo } from '@/api/index.js'
  51. export default {
  52. data() {
  53. return {
  54. dataList:[],
  55. subject_classData:[],
  56. //MQTT请求参数-传感器
  57. mtopic:"lab/function/data",
  58. msg:"",
  59. client:{},
  60. //院系选择器数据
  61. facultyList:[],
  62. facultyArray:[],
  63. facultyIndex:"",
  64. //学科选择器数据
  65. subjectList:[],
  66. subjectArray:[],
  67. subjectIndex:"",
  68. //级别选择器数据
  69. levelList:[
  70. {dictLabel:"全部级别", dictValue:""},
  71. {dictLabel:"一级", dictValue:"1"},
  72. {dictLabel:"二级", dictValue:"2"},
  73. {dictLabel:"三级", dictValue:"3"},
  74. {dictLabel:"四级", dictValue:"4"},
  75. ],
  76. levelArray:['全部级别','一级','二级','三级','四级'],
  77. levelIndex:"",
  78. }
  79. },
  80. onLoad() {
  81. },
  82. onShow(){
  83. this.listDepartments();
  84. this.subject_class();
  85. //监听传感器信息
  86. getApp().watch(this.getMqttSensorData,'mqttSensorData')
  87. },
  88. methods: {
  89. //监听传感器信息
  90. getMqttSensorData(val){
  91. let self = this;
  92. // console.log('页面获取的-传感器信息',val)
  93. if(val.subId){
  94. for(let i=0;i<self.dataList.length;i++){
  95. if(self.dataList[i].id == val.subId){
  96. self.$set(self.dataList[i],'sensorFunctionList',val.functionStatuses);
  97. }
  98. }
  99. }
  100. },
  101. async laboratoryInfo(item){
  102. const {data} = await laboratoryInfo(item.id);
  103. if(data.code == 200){
  104. let obj = data.data[0];
  105. obj.subClassVO = item.subClassVO;
  106. obj.name = item.name;
  107. obj.subTypeLable = item.subTypeLable;
  108. obj.deptName = item.deptName;
  109. obj.subAddrrStr = item.subAddrrStr;
  110. obj.sensorFunctionList = item.sensorFunctionList;
  111. uni.navigateTo({
  112. url: '/pages_manage/workbench/laboratory/laboratoryInfo?item='+encodeURIComponent(JSON.stringify(obj))+'&deptId='+item.deptId
  113. });
  114. }
  115. },
  116. //滚动选择器
  117. facultyChange: function(e) {
  118. if(this.facultyArray[0]){
  119. this.facultyIndex = parseInt(e.target.value);
  120. this.laboratoryList();
  121. }
  122. },
  123. subjectChange: function(e) {
  124. if(this.subjectArray[0]){
  125. this.subjectIndex = parseInt(e.target.value);
  126. this.laboratoryList();
  127. }
  128. },
  129. levelChange: function(e) {
  130. if(this.levelArray[0]){
  131. this.levelIndex = parseInt(e.target.value);
  132. this.laboratoryList();
  133. }
  134. },
  135. //MQTT订阅-传感器
  136. subscriptionMQTT(){
  137. let self = this;
  138. const mqttOptions = {
  139. keepalive: 30,
  140. clean: false,//保留会话
  141. connectTimeout: 5000, // 超时时间
  142. reconnectPeriod:5000, // 重连间隔
  143. clientId: 'lab/function/data',
  144. username: config.username,
  145. password: config.password,
  146. }
  147. const connectUrl = config.mqtt_url;
  148. this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  149. this.client.on('connect', () => {
  150. // 这是为了给自己发条消息,其它无作用
  151. this.client.subscribe(this.mtopic, (err) => {
  152. if (!err) {
  153. console.log("订阅传感器成功",this.mtopic)
  154. }
  155. })
  156. });
  157. // 自动重连
  158. this.client.on('reconnect', (msg) => {
  159. console.log('reconnect', msg)
  160. });
  161. // 错误
  162. this.client.on('error', () => {
  163. console.log('error')
  164. });
  165. // 断开
  166. this.client.on('end', () => {
  167. console.log('end')
  168. });
  169. // 掉线
  170. this.client.on('offline', (msg) => {
  171. console.log('offline',msg)
  172. });
  173. // 收到消息
  174. this.client.on('message', (topic, message) => {
  175. let data = JSON.parse(message)
  176. console.log('topic',topic)
  177. console.log('data',data)
  178. if(topic == this.mtopic){
  179. if(data.data.subId){
  180. for(let i=0;i<self.dataList.length;i++){
  181. if(self.dataList[i].id == data.data.subId){
  182. self.$set(self.dataList[i],'sensorFunctionList',data.data.functionStatuses);
  183. }
  184. }
  185. }
  186. }
  187. })
  188. // 全局监听是否有关闭mqtt的消息的事件
  189. uni.$on('closeMqtt',() => {
  190. this.client.end(true); // 主动断开连接
  191. })
  192. },
  193. //取消订阅关闭MQTT连接
  194. offMQTT(){
  195. this.client.unsubscribe(this.mtopic, error => {
  196. if (error) {
  197. console.log('Unsubscribe error', error)
  198. }
  199. })
  200. this.client.end();
  201. this.client = {};
  202. },
  203. //设备开关按钮
  204. hardwareButton(minItem,command){
  205. let self = this;
  206. let text = '';
  207. if(command == 'close'){
  208. text = '关闭';
  209. }else if(command == 'open'){
  210. text = '开启';
  211. }
  212. uni.showModal({
  213. content: '确认要'+text+'吗?',
  214. cancelColor:"#999",
  215. confirmColor:"#0183FA",
  216. success: function (res) {
  217. if (res.confirm) {
  218. self.mangerControl(minItem,command);
  219. console.log('用户点击确定');
  220. } else if (res.cancel) {
  221. console.log('用户点击取消');
  222. }
  223. }
  224. });
  225. },
  226. //设备开关
  227. async mangerControl(minItem,command){
  228. let obj = {
  229. id:minItem.id,
  230. command:command,
  231. };
  232. const {data} = await mangerControl(obj);
  233. if(data.code == 200){
  234. if(command == 'open'){
  235. minItem.type = 3;
  236. }else if(command == 'close'){
  237. minItem.type = 4;
  238. }
  239. uni.showToast({
  240. title: '操作成功',
  241. icon:"none",
  242. mask:true,
  243. duration: 2000
  244. });
  245. }
  246. },
  247. //获取院系
  248. async listDepartments(){
  249. const {data} = await listDepartments();
  250. if(data.code == 200){
  251. let list = [];
  252. for(let i=0;i<data.data.length;i++){
  253. list.push(data.data[i].deptName)
  254. }
  255. this.facultyArray = list;
  256. list.unshift('全部院系');
  257. this.facultyList = data.data;
  258. this.facultyList.unshift({deptName:"全部院系", deptId:""})
  259. }
  260. },
  261. //获取学科字典
  262. async subject_class(){
  263. const {data} = await subject_class();
  264. if(data.code == 200){
  265. this.subject_classData = data.data;
  266. let list = [];
  267. for(let i=0;i<data.data.length;i++){
  268. list.push(data.data[i].dictLabel)
  269. }
  270. this.subjectArray = list;
  271. list.unshift('全部分类');
  272. this.subjectList = data.data;
  273. this.subjectList.unshift({dictLabel:"全部分类", dictValue:""})
  274. this.laboratoryList().then(res => {
  275. // this.subscriptionMQTT()
  276. });
  277. }
  278. },
  279. //获取实验室列表
  280. async laboratoryList(){
  281. let self = this;
  282. let obj = {
  283. deptId:this.facultyIndex?this.facultyList[this.facultyIndex].deptId:'',
  284. subDept:this.subjectIndex?this.subjectList[this.subjectIndex].dictValue:'',
  285. level:this.levelIndex?this.levelList[this.levelIndex].dictValue:'',
  286. };
  287. const {data} = await laboratoryList(obj);
  288. if(data.code == 200){
  289. for(let o=0;o<data.data.length;o++){
  290. if(data.data[o].labHardwareVOList){
  291. data.data[o].hardwareList = [];
  292. for(let i=0;i<data.data[o].labHardwareVOList.length;i++){
  293. if(data.data[o].labHardwareVOList[i].hardwareTypeEnum.code == 4 &&data.data[o].labHardwareVOList[i].hardwareNUM){
  294. if(data.data[o].hardwareNUM){
  295. data.data[o].hardwareNUM = data.data[o].hardwareNUM + ',' + data.data[o].labHardwareVOList[i].hardwareNUM
  296. }else{
  297. data.data[o].hardwareNUM = data.data[o].labHardwareVOList[i].hardwareNUM
  298. }
  299. }else{
  300. let obj = {
  301. name:"",
  302. type:"",
  303. };
  304. obj.name = data.data[o].labHardwareVOList[i].hardwareTypeEnum.hardwareTypeName
  305. obj.type = data.data[o].labHardwareVOList[i].state.code;
  306. obj.id = data.data[o].labHardwareVOList[i].id;
  307. data.data[o].hardwareList.push(obj);
  308. }
  309. }
  310. }
  311. }
  312. this.dataList = data.data;
  313. return data.data;
  314. }
  315. },
  316. },
  317. onHide(){
  318. // this.offMQTT();
  319. // //删除传感器监听
  320. getApp().deleteSensorWatch();
  321. console.log("onHide",this.mtopic);
  322. },
  323. beforeDestroy(){
  324. // this.offMQTT();
  325. // //删除传感器监听
  326. getApp().deleteSensorWatch();
  327. console.log("beforeDestroy",this.mtopic);
  328. }
  329. }
  330. </script>
  331. <style lang="stylus" scoped>
  332. #meLaboratory{
  333. height:100%;
  334. display flex
  335. flex-direction column
  336. .top-picker-max-box{
  337. display:flex;
  338. padding:0 20rpx;
  339. background #fff
  340. border-bottom:1rpx solid #E0E0E0;
  341. .top-picker-box{
  342. line-height:80rpx;
  343. height:80rpx;
  344. .picker-view{
  345. display flex
  346. view{
  347. display:block;
  348. overflow:hidden;
  349. text-overflow:ellipsis;
  350. white-space:nowrap;
  351. font-size:28rpx;
  352. }
  353. .picker-img{
  354. width:24rpx;
  355. height:13rpx;
  356. margin-top:36rpx;
  357. margin-left:13rpx;
  358. }
  359. }
  360. }
  361. .top-picker-box:nth-child(1){
  362. text-align left
  363. width:320rpx;
  364. .picker-view{
  365. view{
  366. max-width:260rpx;
  367. }
  368. }
  369. }
  370. .top-picker-box:nth-child(2){
  371. text-align left
  372. width:215rpx;
  373. .picker-view{
  374. view{
  375. max-width:156rpx;
  376. }
  377. }
  378. }
  379. .top-picker-box:nth-child(3){
  380. text-align right
  381. width:195rpx;
  382. .picker-view{
  383. view{
  384. flex:1;
  385. }
  386. }
  387. }
  388. }
  389. .for-max-box{
  390. flex:1;
  391. overflow-y scroll
  392. .null-img{
  393. display block
  394. width:276rpx;
  395. height:321rpx;
  396. position absolute
  397. top:200rpx;
  398. left:274rpx;
  399. }
  400. .for-box{
  401. overflow hidden
  402. background #ffffff
  403. margin-bottom:20rpx;
  404. .title-box{
  405. margin:20rpx 20rpx 0;
  406. display flex
  407. view:nth-child(1){
  408. height:40rpx;
  409. line-height:40rpx;
  410. font-size:20rpx;
  411. border-radius:10rpx;
  412. padding:0 20rpx;
  413. color:#fff;
  414. margin-right:20rpx;
  415. }
  416. view:nth-child(2){
  417. line-height:42rpx;
  418. font-size:30rpx;
  419. color:#333;
  420. max-width:600rpx;
  421. flex:1;
  422. }
  423. img{
  424. width:12rpx;
  425. height:20rpx;
  426. margin-top:11rpx;
  427. }
  428. .colorA{
  429. background:#aa2315;
  430. }
  431. .colorB{
  432. background:#ff9b09;
  433. }
  434. .colorC{
  435. background:#3ea3e9;
  436. }
  437. .colorD{
  438. background:#3ea34c;
  439. }
  440. }
  441. .address-box{
  442. margin:20rpx 20rpx;
  443. font-size:26rpx;
  444. color:#999999;
  445. span{
  446. margin-right:60rpx;
  447. }
  448. }
  449. .address-box-two{
  450. display flex
  451. margin:0 20rpx 20rpx 20rpx;
  452. img{
  453. width:28rpx;
  454. height:30rpx;
  455. margin-right:18rpx;
  456. }
  457. view{
  458. line-height:30rpx;
  459. font-size:24rpx;
  460. color:#999999;
  461. }
  462. }
  463. .text-box{
  464. /*border-top:1rpx dashed #e0e0e0;*/
  465. border-top:1rpx solid #E0E0E0;
  466. padding:0 20rpx;
  467. /*
  468. overflow-x:scroll;
  469. overflow-y:hidden;
  470. white-space: nowrap;
  471. */
  472. .min-box{
  473. display inline-block
  474. /*padding:0 30rpx 0 10rpx;*/
  475. width:355rpx;
  476. .min-min-box{
  477. display: flex
  478. img{
  479. width:30rpx;
  480. height:27rpx;
  481. margin-top:24rpx;
  482. margin-right:10rpx;
  483. }
  484. view{
  485. line-height:74rpx;
  486. text-align center
  487. font-size:28rpx;
  488. color:#333;
  489. }
  490. .colorA{
  491. color:#EE3A3A;
  492. }
  493. }
  494. }
  495. }
  496. .sensor-max-box{
  497. margin:0 20rpx;
  498. border-top:1rpx solid #e0e0e0;
  499. border-bottom:1rpx solid #e0e0e0;
  500. display flex
  501. .for-sensor-box{
  502. flex:1;
  503. display flex
  504. font-size:28rpx;
  505. line-height:94rpx;
  506. view{
  507. margin-left:20rpx;
  508. font-size:25rpx;
  509. line-height:94rpx;
  510. color:#0183FA;
  511. }
  512. }
  513. }
  514. .button-max-box{
  515. .button-box{
  516. display flex
  517. view:nth-child(1){
  518. flex:1;
  519. line-height:90rpx;
  520. color:#333333;
  521. margin-left:30rpx;
  522. font-size:28rpx;
  523. }
  524. view:nth-child(2){
  525. width:142rpx;
  526. line-height:42rpx;
  527. border:4rpx solid #0183FA;
  528. border-radius:25rpx;
  529. color: #0183FA;
  530. font-size: 30rpx;
  531. text-align center;
  532. margin:20rpx 20rpx 20rpx 0;
  533. }
  534. }
  535. .for-button-box{
  536. display flex
  537. view{
  538. flex:1;
  539. line-height:90rpx;
  540. color:#333333;
  541. margin-left:30rpx;
  542. font-size:28rpx;
  543. }
  544. img{
  545. height:50rpx;
  546. width:100rpx;
  547. margin:20rpx 20rpx 20rpx 0;
  548. }
  549. view:nth-child(2){
  550. text-align right
  551. color:#999;
  552. margin-right:30rpx;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }
  559. </style>