alarModel.vue 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <template>
  2. <!-- 告警弹窗 -->
  3. <view style="z-index: 999">
  4. <view class="mask" v-if="alarmModel"></view>
  5. <view class="alarm-modal" v-if="alarmModel">
  6. <view class="alarm">
  7. <!-- <image src="../../images/alram.png" mode="" /> -->
  8. <image src="../../static/alarm.png"></image>
  9. </view>
  10. <view class="alarm-header">
  11. <image src="../../static/alarm_event.png" mode="" />
  12. </view>
  13. <view class="nowTime">
  14. {{ nowTime }}
  15. </view>
  16. <view class="alarm-info">
  17. <text style="color: #ff6d6d">{{ devName }}</text>
  18. 检测到可能发生跌倒事件,请立即处理!
  19. </view>
  20. <view class="backImg">
  21. <image src="../../static/actionRed.png" mode="" />
  22. </view>
  23. <view class="alarm-bottom">
  24. <template v-if="voipFlag">
  25. <view
  26. class="alarm-voice"
  27. v-if="authorizeFlag"
  28. @click="gotoCallPage"
  29. >
  30. 语音确认
  31. </view>
  32. <view class="alarm-voice" v-else @click="authorizeIot">
  33. 语音确认
  34. </view>
  35. </template>
  36. <view class="alarm-handle" @click="handleAlarm()">
  37. 已知晓
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import MqttService from "../../utils/globalMqtt.js";
  45. import config from "../../data/config.js";
  46. import {
  47. getSnTicket,
  48. authorize,
  49. getDeviceVoIPList,
  50. getContactList,
  51. getUser,
  52. } from "../../api/index.js";
  53. import {
  54. CameraStatus,
  55. AuthorizeStatus,
  56. CallPagePlugin,
  57. isWmpf,
  58. } from "../../const.js";
  59. import { AsyncValue } from "../../utils";
  60. import { onShow } from "@dcloudio/uni-app";
  61. const wmpfVoip = requirePlugin("wmpf-voip").default;
  62. const envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
  63. const apiTypes = ["校园模式", "硬件模式", "呼叫安卓", "校园+硬件模式"];
  64. const apiTypesWMPF = [1, 0, 3];
  65. const apiTypesWechat = [2];
  66. const getEnvVersionForVoip = (function () {
  67. const _map = {
  68. develop: "developer",
  69. trial: "trial",
  70. release: "formal",
  71. };
  72. return () => {
  73. return _map[envVersion];
  74. };
  75. })();
  76. export default {
  77. comments: {
  78. name: "alarModel",
  79. },
  80. props: {
  81. clientIdProp: {
  82. default: null,
  83. },
  84. },
  85. data() {
  86. return {
  87. alarmModel: false,
  88. eventListId: "",
  89. nowTime: "",
  90. devName: "",
  91. authorizeFlag: true,
  92. clientId: "",
  93. mqttClientTwo: "",
  94. // voip相关
  95. isWmpf: isWmpf,
  96. name: "用户",
  97. voipDevices: [],
  98. sn: "",
  99. isShowPopUp: false,
  100. contactList: [],
  101. isWmpf,
  102. envVersion,
  103. apiTypesValid: (isWmpf ? apiTypesWMPF : apiTypesWechat).map(
  104. (id) => apiTypes[id]
  105. ),
  106. apiTypeIndex: 0,
  107. currentIndex: 0,
  108. voipFlag: "",
  109. mqttClientFlag: false,
  110. inactivityTimer: "",
  111. };
  112. },
  113. computed: {},
  114. methods: {
  115. authorizeIot() {
  116. this.hanledList("Voice");
  117. this.sn = this.clientId;
  118. this.alarmModel = false;
  119. // const { name, sn, voipDevices } = this;
  120. const name = this.name;
  121. const sn = this.clientId;
  122. const voipDevices = this.voipDevices;
  123. if (!name || name.length === 0 || !sn || sn.length === 0) {
  124. wx.showToast({
  125. title: "请输入授权人名称和设备clientId",
  126. icon: "none",
  127. });
  128. return;
  129. }
  130. const { modelId } = config;
  131. console.log(sn, modelId, 77777);
  132. const { snTicket } = getSnTicket({
  133. sn,
  134. modelId,
  135. });
  136. console.log(snTicket, sn, 7777555);
  137. var that = this;
  138. wx.requestDeviceVoIP({
  139. sn,
  140. snTicket,
  141. modelId,
  142. deviceName: sn,
  143. async success(res) {
  144. console.log(`requestDeviceVoIP`, res);
  145. await authorize({
  146. sn,
  147. name,
  148. });
  149. wx.showToast({
  150. title: "授权成功",
  151. icon: "none",
  152. });
  153. const key = `voipDevices[${voipDevices.length}]`;
  154. // that.setData({
  155. // [key]: {
  156. // sn,
  157. // modelId,
  158. // status: 1,
  159. // },
  160. // });
  161. that.voipDevices[voipDevices.length] = {
  162. sn,
  163. modelId,
  164. status: 1,
  165. };
  166. that.sn = sn;
  167. that.modelId = modelId;
  168. that.status = 1;
  169. that.gotoCallPage();
  170. },
  171. fail(err) {
  172. console.error(`requestDeviceVoIP failOne`, err);
  173. wx.showToast({
  174. title: "授权失败, 请前往设置页开启",
  175. icon: "none",
  176. });
  177. },
  178. });
  179. },
  180. handleAlarm() {
  181. this.hanledList("noVoice");
  182. this.alarmModel = false;
  183. },
  184. // voip相关方法
  185. async getVoipDevices() {
  186. const { list } = await getDeviceVoIPList();
  187. console.log("getDeviceVoIPList:", list);
  188. if (list && list.length > 0) {
  189. const authorizeFlag = list.some(
  190. (item) => item.sn === this.clientId
  191. );
  192. console.log("authorizeFlag:", list, authorizeFlag);
  193. this.authorizeFlag = authorizeFlag;
  194. }
  195. if (!list || list.length === 0) {
  196. this.authorizeFlag = false;
  197. }
  198. if (typeof list === "object") {
  199. this.voipDevices = list;
  200. }
  201. },
  202. async authorizeIot() {
  203. this.sn = this.clientId;
  204. // this.alarmModel = false;
  205. const name = this.name;
  206. const sn = this.sn;
  207. const voipDevices = this.voipDevices;
  208. console.log(name, sn, voipDevices, 9999999999999);
  209. // const { name, sn, voipDevices } = this;
  210. if (!name || name.length === 0 || !sn || sn.length === 0) {
  211. wx.showToast({
  212. title: "请输入授权人名称和设备dev_id",
  213. icon: "none",
  214. });
  215. return;
  216. }
  217. const { modelId } = config;
  218. console.log(sn, modelId, 77777);
  219. const { snTicket } = await getSnTicket({
  220. sn,
  221. modelId,
  222. });
  223. console.log(snTicket, sn, 997777);
  224. var that = this;
  225. wx.requestDeviceVoIP({
  226. sn,
  227. snTicket,
  228. modelId,
  229. deviceName: sn,
  230. async success(res) {
  231. console.log(`requestDeviceVoIP`, res);
  232. await authorize({
  233. sn,
  234. name,
  235. });
  236. wx.showToast({
  237. title: "授权成功",
  238. icon: "none",
  239. });
  240. const key = `voipDevices[${voipDevices.length}]`;
  241. // that.key = {
  242. // sn,
  243. // modelId,
  244. // status: 1,
  245. // };
  246. that.voipDevices[voipDevices.length] = {
  247. sn,
  248. modelId,
  249. status: 1,
  250. };
  251. that.gotoCallPage();
  252. },
  253. fail(err) {
  254. console.error(`requestDeviceVoIP failTwo`, err);
  255. wx.showToast({
  256. title: "授权失败, 请前往设置页开启",
  257. icon: "none",
  258. });
  259. },
  260. });
  261. },
  262. async gotoCallPage() {
  263. this.hanledList("Voice");
  264. wx.showToast({
  265. title: "通话加载中...",
  266. icon: "loading",
  267. duration: 2000, //持续的时间
  268. });
  269. // this.alarmModel = false;
  270. this._caller = new AsyncValue();
  271. let options = {
  272. sn: this.sn,
  273. };
  274. await this.getSelf(options);
  275. await this.getContactList();
  276. let voiceCan = {
  277. sn: this.sn,
  278. idx: 1,
  279. type: "voice",
  280. };
  281. await this.call(voiceCan);
  282. },
  283. async getSelf(options = {}) {
  284. if (!this._caller.isPending()) {
  285. return;
  286. }
  287. if (isWmpf) {
  288. const { sn } = options;
  289. if (sn) {
  290. config.sn = sn;
  291. this.sn = sn;
  292. }
  293. this._caller.set({
  294. id: config.sn,
  295. name: config.sn,
  296. });
  297. } else {
  298. try {
  299. const resp = await getUser();
  300. console.log(`getUser`, resp);
  301. this._caller.set({
  302. id: resp.openid,
  303. name: resp.name,
  304. });
  305. } catch (error) {
  306. console.error(`getself error`, error);
  307. }
  308. }
  309. },
  310. async getContactList() {
  311. wx.showLoading();
  312. try {
  313. let contactList = [];
  314. const raw_contactList = await getContactList({
  315. sn: isWmpf ? config.sn : null,
  316. });
  317. console.log(`raw_contactList`, raw_contactList);
  318. if (!isWmpf) {
  319. // 微信呼叫设备
  320. const { list } = await getDeviceVoIPList();
  321. contactList = raw_contactList.map(({ sn, pushToken }) => {
  322. let canCall = true;
  323. if (typeof list === "object") {
  324. const canCallList = list
  325. .filter(
  326. (item) =>
  327. item.status === AuthorizeStatus.ACCEPT
  328. )
  329. .map((item) => item.sn);
  330. canCall = canCallList.includes(sn);
  331. }
  332. return {
  333. id: sn,
  334. name: sn,
  335. canCall,
  336. ticket: pushToken,
  337. };
  338. });
  339. } else {
  340. // 设备呼叫微信
  341. const raw_contactList = await getContactList({
  342. sn: config.sn,
  343. });
  344. console.log(`raw_contactList: 业务方存储`, raw_contactList);
  345. if (raw_contactList.length > 0) {
  346. const canCallList = (
  347. await wmpfVoip.getIotBindContactList({
  348. sn: config.sn,
  349. model_id: config.modelId,
  350. openid_list: raw_contactList.map(
  351. (item) => item.openid
  352. ),
  353. })
  354. ).contact_list
  355. .filter(
  356. (item) => item.status === AuthorizeStatus.ACCEPT
  357. )
  358. .map((item) => item.openid);
  359. console.log(`canCallList: 微信授权存储`, canCallList);
  360. contactList = raw_contactList.map((item) => {
  361. const canCall = canCallList.includes(item.openid);
  362. return {
  363. id: item.openid,
  364. name: item.name,
  365. canCall,
  366. ticket: "",
  367. };
  368. });
  369. }
  370. }
  371. console.log(`getContactList: `, contactList);
  372. this.contactList = contactList;
  373. } catch (error) {
  374. console.error(`getContactList error: `, error);
  375. }
  376. wx.hideLoading();
  377. },
  378. async call(e) {
  379. console.log(`call`, "走到了call方法");
  380. const { sn, idx, type: roomtype } = e;
  381. // console.log(e, this.contactList, 888870);
  382. const target = this.contactList.find((item) => item.id === sn);
  383. console.log("走到了这里了。。。。1", target);
  384. // const target = this.contactList[idx];
  385. const apiTypeName = this.apiTypesValid[this.apiTypeIndex];
  386. const businessType = apiTypes.indexOf(apiTypeName);
  387. try {
  388. if (!target.canCall) {
  389. wx.showToast({
  390. title: "用户未授权或设备不在线",
  391. icon: "none",
  392. });
  393. throw Error("target can not call");
  394. }
  395. console.log("走到了这里了。。。。2");
  396. const _caller = await this._caller.get();
  397. console.log(`caller: `, _caller);
  398. console.log(`listener: `, target);
  399. const { roomId } = await wmpfVoip.callDevice({
  400. roomType: "voice",
  401. sn: sn,
  402. modelId: "iFJ2BNIQcd9WlUIaT7_l4Q",
  403. nickName: "xx",
  404. isCloud: true,
  405. payload: "radar-power.cn",
  406. });
  407. wx.redirectTo({
  408. url: CallPagePlugin,
  409. });
  410. } catch (error) {
  411. console.log(`>>>>>>>>>>>>>>>>>>>>>>>>>>>`);
  412. console.error(`call error`, error);
  413. }
  414. },
  415. // mqtt相关方法
  416. // connectMQTTwo() {
  417. // if (this.mqttClientFlag) {
  418. // console.log("主题已订阅");
  419. // return;
  420. // }
  421. // const THRESHOLD = 2;
  422. // const params = {
  423. // keepalive: 6000,
  424. // clean: true,
  425. // connectTimeout: 30 * 1000,
  426. // clientId:
  427. // "xcx_mqtt_cmd1" +
  428. // uni.getStorageSync("userId") +
  429. // Math.random().toString(16).substring(2, 8),
  430. // username: "lnradar",
  431. // password: "lnradar",
  432. // // 微信小程序特定配置
  433. // wsOptions: {
  434. // WebSocket: function (url) {
  435. // return wx.connectSocket({
  436. // url: url,
  437. // header: {
  438. // "content-type": "application/json",
  439. // },
  440. // protocols: ["mqtt"],
  441. // });
  442. // },
  443. // },
  444. // reconnectPeriod: 0,
  445. // rejectUnauthorized: false, // 仅开发环境使用,生产环境应设为true或移除
  446. // };
  447. // let client = "";
  448. // let selectedService = uni.getStorageSync("sercviceChoice");
  449. // if (!selectedService || selectedService == "aloneServe") {
  450. // if (__wxConfig.envVersion == "develop") {
  451. // client = mqtt.connect(
  452. // "wxs://cmd.radar-power.cn/mqtt/",
  453. // params
  454. // );
  455. // }
  456. // if (__wxConfig.envVersion == "trial") {
  457. // client = mqtt.connect(
  458. // "wxs://cmd.radar-power.cn/mqtt/",
  459. // params
  460. // );
  461. // }
  462. // }
  463. // // 存储client引用以便后续操作
  464. // this.mqttClientTwo = client;
  465. // client.on("connect", () => {
  466. // console.log("MQTT连接成功");
  467. // this.mqttClientFlag = true;
  468. // let userId = uni.getStorageSync("userId");
  469. // client.subscribe(`/mps/wx_${userId}/notice`, (err) => {
  470. // if (err) {
  471. // console.error("订阅失败", err);
  472. // } else {
  473. // console.log(
  474. // `成功订阅设备主题: /mps/wx_${userId}/notice`
  475. // );
  476. // }
  477. // });
  478. // });
  479. // client.on("disconnect", () => {
  480. // console.log("MQTT不在连接");
  481. // });
  482. // client.on("error", (err) => {
  483. // this.mqttClientFlag = false;
  484. // setTimeout(() => {
  485. // this.connectMQTTwo();
  486. // }, 1000);
  487. // });
  488. // client.on("reconnect", () => {});
  489. // client.on("close", () => {});
  490. // client.on("message", (topic, message) => {
  491. // console.log(JSON.parse(message.toString()), "8870");
  492. // // 处理点位消息
  493. // let userId = uni.getStorageSync("userId");
  494. // console.log("topic777777", userId);
  495. // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  496. // const match = topic.match(noticeMatch);
  497. // if (!match) return;
  498. // this.alarmModel = true;
  499. // const data = JSON.parse(message.toString());
  500. // this.clientId = data.clientId;
  501. // this.devName = data.devName;
  502. // this.sn = data.clientId;
  503. // this.eventListId = data.eventListId;
  504. // this.getVoipAuthor(this.clientId, userId);
  505. // console.log(data, topic, "接收到消息222:");
  506. // console.log(
  507. // "接收到消息:",
  508. // this.clientId,
  509. // this.devName,
  510. // this.sn,
  511. // this.eventListId,
  512. // data
  513. // );
  514. // const now = new Date();
  515. // const year = now.getFullYear();
  516. // const month = (now.getMonth() + 1).toString().padStart(2, "0");
  517. // const day = now.getDate().toString().padStart(2, "0");
  518. // const hours = now.getHours().toString().padStart(2, "0");
  519. // const minutes = now.getMinutes().toString().padStart(2, "0");
  520. // const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
  521. // this.nowTime = formattedTime;
  522. // console.log("isWmpf:", isWmpf);
  523. // if (!isWmpf) {
  524. // console.log("isWmpf:", isWmpf);
  525. // this.getVoipDevices();
  526. // }
  527. // console.log("接收到消息:", topic, data);
  528. // });
  529. // },
  530. hanledList(type) {
  531. this.$http
  532. .get(`wap/event/handleEvent`, {
  533. eventListId: this.eventListId,
  534. })
  535. .then((res) => {
  536. if (res.data.code == 200) {
  537. if (type === "noVoice") {
  538. uni.showToast({
  539. title: "已处理",
  540. icon: "success",
  541. duration: 1500,
  542. });
  543. }
  544. } else {
  545. uni.showToast({
  546. title: "处理失败",
  547. icon: "none",
  548. duration: 1500,
  549. });
  550. }
  551. });
  552. },
  553. closemqtt() {
  554. this.mqttClientFlag = false;
  555. if (this.mqttClientTwo) {
  556. this.mqttClientTwo.end();
  557. this.mqttClientTwo = null;
  558. console.log("MQTT连接已断开");
  559. }
  560. },
  561. getVoipAuthor(clientId, userId) {
  562. console.log(clientId, userId);
  563. this.$http
  564. .post(
  565. "wap/share/queryUserVoipPermission",
  566. {
  567. userId: userId,
  568. clientId: clientId,
  569. },
  570. {
  571. header: {
  572. "Content-Type": "application/json;charset=UTF-8",
  573. },
  574. }
  575. )
  576. .then((res) => {
  577. if (res.data.data) {
  578. console.log(res.data.data, 9999);
  579. if (res.data.data.voipFlag === 0) {
  580. this.voipFlag = true;
  581. } else {
  582. this.voipFlag = false;
  583. }
  584. }
  585. });
  586. },
  587. sendChange() {
  588. this.$emit("sendChange", this.targetPoints);
  589. },
  590. hanOtherMessage(topic, message) {
  591. console.log("接收到组件的消息:", JSON.parse(message.toString()));
  592. console.log(JSON.parse(message.toString()), "8870");
  593. // 处理点位消息
  594. let userId = uni.getStorageSync("userId");
  595. const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  596. const match = topic.match(noticeMatch);
  597. if (!match) return;
  598. this.alarmModel = true;
  599. const data = JSON.parse(message.toString());
  600. this.clientId = data.clientId;
  601. this.devName = data.devName;
  602. this.sn = data.clientId;
  603. this.eventListId = data.eventListId;
  604. this.getVoipAuthor(this.clientId, userId);
  605. console.log(data, topic, "接收到消息222:");
  606. console.log(
  607. "接收到消息:",
  608. this.clientId,
  609. this.devName,
  610. this.sn,
  611. this.eventListId,
  612. data
  613. );
  614. const now = new Date();
  615. const year = now.getFullYear();
  616. const month = (now.getMonth() + 1).toString().padStart(2, "0");
  617. const day = now.getDate().toString().padStart(2, "0");
  618. const hours = now.getHours().toString().padStart(2, "0");
  619. const minutes = now.getMinutes().toString().padStart(2, "0");
  620. const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
  621. this.nowTime = formattedTime;
  622. console.log("isWmpf:", isWmpf);
  623. if (!isWmpf) {
  624. console.log("isWmpf:", isWmpf);
  625. this.getVoipDevices();
  626. }
  627. console.log("接收到消息:", topic, data);
  628. },
  629. },
  630. mounted() {
  631. // if (this.$mqttClient) {
  632. // console.log("开始接受mqtt消息111");
  633. // this.$mqttClient.on("message", (topic, message) => {
  634. // console.log(
  635. // "接收到消息CMD11111:",
  636. // JSON.parse(message.toString())
  637. // );
  638. // console.log(JSON.parse(message.toString()), "8870");
  639. // // 处理点位消息
  640. // let userId = uni.getStorageSync("userId");
  641. // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  642. // const match = topic.match(noticeMatch);
  643. // if (!match) return;
  644. // this.alarmModel = true;
  645. // const data = JSON.parse(message.toString());
  646. // this.clientId = data.clientId;
  647. // this.devName = data.devName;
  648. // this.sn = data.clientId;
  649. // this.eventListId = data.eventListId;
  650. // this.getVoipAuthor(this.clientId, userId);
  651. // console.log(data, topic, "接收到消息222:");
  652. // console.log(
  653. // "接收到消息:",
  654. // this.clientId,
  655. // this.devName,
  656. // this.sn,
  657. // this.eventListId,
  658. // data
  659. // );
  660. // const now = new Date();
  661. // const year = now.getFullYear();
  662. // const month = (now.getMonth() + 1).toString().padStart(2, "0");
  663. // const day = now.getDate().toString().padStart(2, "0");
  664. // const hours = now.getHours().toString().padStart(2, "0");
  665. // const minutes = now.getMinutes().toString().padStart(2, "0");
  666. // const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
  667. // this.nowTime = formattedTime;
  668. // console.log("isWmpf:", isWmpf);
  669. // if (!isWmpf) {
  670. // console.log("isWmpf:", isWmpf);
  671. // this.getVoipDevices();
  672. // }
  673. // console.log("接收到消息:", topic, data);
  674. // });
  675. // } else {
  676. // uni.$once("mqtt-ready", () => {
  677. // console.log("开始接受mqtt消息222");
  678. // this.$mqttClient.on("message", (topic, message) => {
  679. // console.log(
  680. // "接收到消息CMD222:",
  681. // JSON.parse(message.toString())
  682. // );
  683. // console.log(JSON.parse(message.toString()), "8870");
  684. // // 处理点位消息
  685. // let userId = uni.getStorageSync("userId");
  686. // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  687. // const match = topic.match(noticeMatch);
  688. // if (!match) return;
  689. // this.alarmModel = true;
  690. // const data = JSON.parse(message.toString());
  691. // this.clientId = data.clientId;
  692. // this.devName = data.devName;
  693. // this.sn = data.clientId;
  694. // this.eventListId = data.eventListId;
  695. // this.getVoipAuthor(this.clientId, userId);
  696. // console.log(data, topic, "接收到消息222:");
  697. // console.log(
  698. // "接收到消息:",
  699. // this.clientId,
  700. // this.devName,
  701. // this.sn,
  702. // this.eventListId,
  703. // data
  704. // );
  705. // const now = new Date();
  706. // const year = now.getFullYear();
  707. // const month = (now.getMonth() + 1)
  708. // .toString()
  709. // .padStart(2, "0");
  710. // const day = now.getDate().toString().padStart(2, "0");
  711. // const hours = now.getHours().toString().padStart(2, "0");
  712. // const minutes = now
  713. // .getMinutes()
  714. // .toString()
  715. // .padStart(2, "0");
  716. // const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
  717. // this.nowTime = formattedTime;
  718. // console.log("isWmpf:", isWmpf);
  719. // if (!isWmpf) {
  720. // console.log("isWmpf:", isWmpf);
  721. // this.getVoipDevices();
  722. // }
  723. // console.log("接收到消息:", topic, data);
  724. // });
  725. // });
  726. // }
  727. // 确保已经连接好再订阅
  728. const userId = uni.getStorageSync("userId");
  729. const topic = `/mps/wx_${userId}/notice`;
  730. if (MqttService.cmdConnected) {
  731. // 已连接 → 直接订阅
  732. this.unsubscribeOther = MqttService.subscribe(
  733. "CMD",
  734. topic,
  735. (message) => {
  736. const data = JSON.parse(message);
  737. let userId = uni.getStorageSync("userId");
  738. const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  739. const match = topic.match(noticeMatch);
  740. if (!match) return;
  741. // 更新页面数据
  742. this.alarmModel = true;
  743. this.clientId = data.clientId;
  744. this.devName = data.devName;
  745. this.sn = data.clientId;
  746. this.eventListId = data.eventListId;
  747. // VOIP 相关逻辑
  748. this.getVoipAuthor(this.clientId, userId);
  749. // 格式化时间
  750. const now = new Date();
  751. this.nowTime = `${now.getFullYear()}-${String(
  752. now.getMonth() + 1
  753. ).padStart(2, "0")}-${String(now.getDate()).padStart(
  754. 2,
  755. "0"
  756. )} ${String(now.getHours()).padStart(2, "0")}:${String(
  757. now.getMinutes()
  758. ).padStart(2, "0")}`;
  759. if (!isWmpf) {
  760. this.getVoipDevices();
  761. }
  762. console.log("处理后的 CMD 数据:", data);
  763. },
  764. (err) => {
  765. if (!err) console.log(`✅ 页面B 已订阅 ${topic}`);
  766. }
  767. );
  768. } else {
  769. // 还没连上 → 等待 mqtt-ready
  770. uni.$once("mqtt-ready", () => {
  771. this.unsubscribeOther = MqttService.subscribe(
  772. "CMD",
  773. topic,
  774. (message) => {
  775. const data = JSON.parse(message);
  776. let userId = uni.getStorageSync("userId");
  777. const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  778. const match = topic.match(noticeMatch);
  779. if (!match) return;
  780. // 更新页面数据
  781. this.alarmModel = true;
  782. this.clientId = data.clientId;
  783. this.devName = data.devName;
  784. this.sn = data.clientId;
  785. this.eventListId = data.eventListId;
  786. // VOIP 相关逻辑
  787. this.getVoipAuthor(this.clientId, userId);
  788. // 格式化时间
  789. const now = new Date();
  790. this.nowTime = `${now.getFullYear()}-${String(
  791. now.getMonth() + 1
  792. ).padStart(2, "0")}-${String(now.getDate()).padStart(
  793. 2,
  794. "0"
  795. )} ${String(now.getHours()).padStart(2, "0")}:${String(
  796. now.getMinutes()
  797. ).padStart(2, "0")}`;
  798. if (!isWmpf) {
  799. this.getVoipDevices();
  800. }
  801. console.log("处理后的 CMD 数据:", data);
  802. },
  803. (err) => {
  804. if (!err) console.log(`✅ 页面B 已订阅 ${topic}`);
  805. }
  806. );
  807. });
  808. }
  809. // if (MqttService.cmdConnected) {
  810. // this.unsubscribeFn = MqttService.subscribe(
  811. // "CMD",
  812. // topic,
  813. // (topic, message) => {
  814. // const data = JSON.parse(message);
  815. // let userId = uni.getStorageSync("userId");
  816. // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  817. // const match = topic.match(noticeMatch);
  818. // if (!match) return;
  819. // // 更新页面数据
  820. // this.alarmModel = true;
  821. // this.clientId = data.clientId;
  822. // this.devName = data.devName;
  823. // this.sn = data.clientId;
  824. // this.eventListId = data.eventListId;
  825. // // VOIP 相关逻辑
  826. // this.getVoipAuthor(this.clientId, userId);
  827. // // 格式化时间
  828. // const now = new Date();
  829. // this.nowTime = `${now.getFullYear()}-${String(
  830. // now.getMonth() + 1
  831. // ).padStart(2, "0")}-${String(now.getDate()).padStart(
  832. // 2,
  833. // "0"
  834. // )} ${String(now.getHours()).padStart(2, "0")}:${String(
  835. // now.getMinutes()
  836. // ).padStart(2, "0")}`;
  837. // if (!isWmpf) {
  838. // this.getVoipDevices();
  839. // }
  840. // console.log("处理后的 CMD 数据:", data);
  841. // }
  842. // );
  843. // } else {
  844. // uni.$once("mqtt-ready", () => {
  845. // this.unsubscribeFn = MqttService.subscribe(
  846. // "CMD",
  847. // topic,
  848. // (topic, message) => {
  849. // const data = JSON.parse(message);
  850. // let userId = uni.getStorageSync("userId");
  851. // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  852. // const match = topic.match(noticeMatch);
  853. // if (!match) return;
  854. // // 更新页面数据
  855. // this.alarmModel = true;
  856. // this.clientId = data.clientId;
  857. // this.devName = data.devName;
  858. // this.sn = data.clientId;
  859. // this.eventListId = data.eventListId;
  860. // // VOIP 相关逻辑
  861. // this.getVoipAuthor(this.clientId, userId);
  862. // // 格式化时间
  863. // const now = new Date();
  864. // this.nowTime = `${now.getFullYear()}-${String(
  865. // now.getMonth() + 1
  866. // ).padStart(2, "0")}-${String(now.getDate()).padStart(
  867. // 2,
  868. // "0"
  869. // )} ${String(now.getHours()).padStart(2, "0")}:${String(
  870. // now.getMinutes()
  871. // ).padStart(2, "0")}`;
  872. // if (!isWmpf) {
  873. // this.getVoipDevices();
  874. // }
  875. // console.log("处理后的 CMD 数据:", data);
  876. // }
  877. // );
  878. // });
  879. // }
  880. },
  881. beforeDestroy() {
  882. if (this.unsubscribeCmd) {
  883. this.unsubscribeCmd();
  884. this.unsubscribeCmd = null;
  885. console.log("CMD 订阅已取消");
  886. }
  887. },
  888. };
  889. </script>
  890. <style lang="less" scoped>
  891. /* 弹窗部分样式 */
  892. .mask {
  893. position: fixed;
  894. left: 0;
  895. top: 0;
  896. right: 0;
  897. bottom: 0;
  898. background: rgba(0, 0, 0, 0.3);
  899. z-index: 100;
  900. }
  901. .alarm-modal {
  902. width: 675rpx;
  903. height: 1100rpx;
  904. position: fixed;
  905. top: 50%;
  906. left: 50%;
  907. transform: translate(-50%, -50%);
  908. background: linear-gradient(180deg, #ffdfdf 0%, #ffffff 30.3%);
  909. border-radius: 37rpx 37rpx;
  910. z-index: 103;
  911. padding: 50rpx 55rpx;
  912. box-sizing: border-box;
  913. .alarm-header image {
  914. width: 315rpx;
  915. height: 40rpx;
  916. }
  917. .alarm image {
  918. position: fixed;
  919. top: 0;
  920. right: 0;
  921. width: 180rpx;
  922. height: 175rpx;
  923. }
  924. .nowTime {
  925. width: 270rpx;
  926. margin-top: 20rpx;
  927. text-align: center;
  928. align-items: center;
  929. line-height: 100%;
  930. background: #fed5d5;
  931. color: #111111;
  932. font-size: 28rpx;
  933. border-radius: 10rpx;
  934. padding: 5rpx 5rpx;
  935. }
  936. .alarm-info {
  937. margin-top: 40rpx;
  938. color: #111111;
  939. font-size: 28rpx;
  940. letter-spacing: 4%;
  941. }
  942. .backImg {
  943. margin: 37rpx auto;
  944. width: 400rpx;
  945. height: 600rpx;
  946. background-color: #fff;
  947. border: 12rpx solid #333333;
  948. background-image: url(https://hflnxx.oss-cn-shanghai.aliyuncs.com/IMAGE/20250919/toilet_bg.png);
  949. background-repeat: no-repeat;
  950. background-position: center;
  951. image {
  952. position: absolute;
  953. top: 50%;
  954. left: 50%;
  955. width: 60rpx;
  956. height: 60rpx;
  957. }
  958. }
  959. .alarm-bottom {
  960. display: flex;
  961. justify-content: flex-end;
  962. align-items: center;
  963. .alarm-voice {
  964. width: 185rpx;
  965. height: 85rpx;
  966. background: #f3e2dd;
  967. border-radius: 28rpx;
  968. color: #111111;
  969. font-size: 32rpx;
  970. text-align: center;
  971. line-height: 85rpx;
  972. }
  973. .alarm-handle {
  974. margin-left: 20rpx;
  975. width: 185rpx;
  976. height: 85rpx;
  977. background: linear-gradient(114.23deg, #a27867 0%, #74483d 100%);
  978. border-radius: 28rpx;
  979. box-shadow: 0rpx 4.69rpx 18.75rpx rgba(72, 41, 29, 0.15),
  980. 0rpx 9.38rpx 9.38rpx rgba(154, 132, 89, 0.2),
  981. 0rpx -4.69rpx 28.13rpx 4.69rpx #a16647 inset;
  982. color: #ffffff;
  983. font-size: 32rpx;
  984. text-align: center;
  985. line-height: 85rpx;
  986. }
  987. }
  988. }
  989. </style>