alarModel.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <template>
  2. <!-- 告警弹窗 -->
  3. <view>
  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 config from "../../data/config.js";
  45. import mqtt from "../../utils/mqtt";
  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. const wmpfVoip = requirePlugin("wmpf-voip").default;
  61. const envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
  62. const apiTypes = ["校园模式", "硬件模式", "呼叫安卓", "校园+硬件模式"];
  63. const apiTypesWMPF = [1, 0, 3];
  64. const apiTypesWechat = [2];
  65. const getEnvVersionForVoip = (function () {
  66. const _map = {
  67. develop: "developer",
  68. trial: "trial",
  69. release: "formal",
  70. };
  71. return () => {
  72. return _map[envVersion];
  73. };
  74. })();
  75. export default {
  76. comments: {
  77. name: "alarModel",
  78. },
  79. props: {
  80. clientIdProp: {
  81. default: null,
  82. },
  83. },
  84. data() {
  85. return {
  86. alarmModel: false,
  87. eventListId: "",
  88. nowTime: "",
  89. devName: "",
  90. authorizeFlag: true,
  91. clientId: "",
  92. mqttClientTwo: "",
  93. // voip相关
  94. isWmpf: isWmpf,
  95. name: "用户",
  96. voipDevices: [],
  97. sn: "",
  98. isShowPopUp: false,
  99. contactList: [],
  100. isWmpf,
  101. envVersion,
  102. apiTypesValid: (isWmpf ? apiTypesWMPF : apiTypesWechat).map(
  103. (id) => apiTypes[id]
  104. ),
  105. apiTypeIndex: 0,
  106. currentIndex: 0,
  107. voipFlag: "",
  108. mqttClientFlag: false,
  109. inactivityTimer: "",
  110. };
  111. },
  112. computed: {},
  113. methods: {
  114. authorizeIot() {
  115. this.hanledList("Voice");
  116. this.sn = this.clientId;
  117. this.alarmModel = false;
  118. // const { name, sn, voipDevices } = this;
  119. const name = this.name;
  120. const sn = this.clientId;
  121. const voipDevices = this.voipDevices;
  122. if (!name || name.length === 0 || !sn || sn.length === 0) {
  123. wx.showToast({
  124. title: "请输入授权人名称和设备clientId",
  125. icon: "none",
  126. });
  127. return;
  128. }
  129. const { modelId } = config;
  130. console.log(sn, modelId, 77777);
  131. const { snTicket } = getSnTicket({
  132. sn,
  133. modelId,
  134. });
  135. console.log(snTicket, sn, 99999999);
  136. var that = this;
  137. wx.requestDeviceVoIP({
  138. sn,
  139. snTicket,
  140. modelId,
  141. deviceName: sn,
  142. async success(res) {
  143. console.log(`requestDeviceVoIP`, res);
  144. await authorize({
  145. sn,
  146. name,
  147. });
  148. wx.showToast({
  149. title: "授权成功",
  150. icon: "none",
  151. });
  152. const key = `voipDevices[${voipDevices.length}]`;
  153. // that.setData({
  154. // [key]: {
  155. // sn,
  156. // modelId,
  157. // status: 1,
  158. // },
  159. // });
  160. that.voipDevices[voipDevices.length] = {
  161. sn,
  162. modelId,
  163. status: 1,
  164. };
  165. that.sn = sn;
  166. that.modelId = modelId;
  167. that.status = 1;
  168. that.gotoCallPage();
  169. },
  170. fail(err) {
  171. console.error(`requestDeviceVoIP failOne`, err);
  172. wx.showToast({
  173. title: "授权失败, 请前往设置页开启",
  174. icon: "none",
  175. });
  176. },
  177. });
  178. },
  179. handleAlarm() {
  180. this.hanledList("noVoice");
  181. this.alarmModel = false;
  182. },
  183. // voip相关方法
  184. async getVoipDevices() {
  185. const { list } = await getDeviceVoIPList();
  186. console.log("getDeviceVoIPList:", list);
  187. if (list && list.length > 0) {
  188. const authorizeFlag = list.some(
  189. (item) => item.sn === this.clientId
  190. );
  191. console.log("authorizeFlag:", list, authorizeFlag);
  192. this.authorizeFlag = authorizeFlag;
  193. }
  194. if (!list || list.length === 0) {
  195. this.authorizeFlag = false;
  196. }
  197. if (typeof list === "object") {
  198. this.voipDevices = list;
  199. }
  200. },
  201. async authorizeIot() {
  202. this.sn = this.clientId;
  203. // this.alarmModel = false;
  204. const name = this.name;
  205. const sn = this.sn;
  206. const voipDevices = this.voipDevices;
  207. console.log(name, sn, voipDevices, 9999999999999);
  208. // const { name, sn, voipDevices } = this;
  209. if (!name || name.length === 0 || !sn || sn.length === 0) {
  210. wx.showToast({
  211. title: "请输入授权人名称和设备dev_id",
  212. icon: "none",
  213. });
  214. return;
  215. }
  216. const { modelId } = config;
  217. console.log(sn, modelId, 77777);
  218. const { snTicket } = await getSnTicket({
  219. sn,
  220. modelId,
  221. });
  222. console.log(snTicket, sn, 99999999);
  223. var that = this;
  224. wx.requestDeviceVoIP({
  225. sn,
  226. snTicket,
  227. modelId,
  228. deviceName: sn,
  229. async success(res) {
  230. console.log(`requestDeviceVoIP`, res);
  231. await authorize({
  232. sn,
  233. name,
  234. });
  235. wx.showToast({
  236. title: "授权成功",
  237. icon: "none",
  238. });
  239. const key = `voipDevices[${voipDevices.length}]`;
  240. // that.key = {
  241. // sn,
  242. // modelId,
  243. // status: 1,
  244. // };
  245. that.voipDevices[voipDevices.length] = {
  246. sn,
  247. modelId,
  248. status: 1,
  249. };
  250. that.gotoCallPage();
  251. },
  252. fail(err) {
  253. console.error(`requestDeviceVoIP failTwo`, err);
  254. wx.showToast({
  255. title: "授权失败, 请前往设置页开启",
  256. icon: "none",
  257. });
  258. },
  259. });
  260. },
  261. async gotoCallPage() {
  262. this.hanledList("Voice");
  263. wx.showToast({
  264. title: "通话加载中...",
  265. icon: "loading",
  266. duration: 2000, //持续的时间
  267. });
  268. // this.alarmModel = false;
  269. this._caller = new AsyncValue();
  270. let options = {
  271. sn: this.sn,
  272. };
  273. await this.getSelf(options);
  274. await this.getContactList();
  275. let voiceCan = {
  276. sn: this.sn,
  277. idx: 1,
  278. type: "voice",
  279. };
  280. await this.call(voiceCan);
  281. },
  282. async getSelf(options = {}) {
  283. if (!this._caller.isPending()) {
  284. return;
  285. }
  286. if (isWmpf) {
  287. const { sn } = options;
  288. if (sn) {
  289. config.sn = sn;
  290. this.sn = sn;
  291. }
  292. this._caller.set({
  293. id: config.sn,
  294. name: config.sn,
  295. });
  296. } else {
  297. try {
  298. const resp = await getUser();
  299. console.log(`getUser`, resp);
  300. this._caller.set({
  301. id: resp.openid,
  302. name: resp.name,
  303. });
  304. } catch (error) {
  305. console.error(`getself error`, error);
  306. }
  307. }
  308. },
  309. async getContactList() {
  310. wx.showLoading();
  311. try {
  312. let contactList = [];
  313. const raw_contactList = await getContactList({
  314. sn: isWmpf ? config.sn : null,
  315. });
  316. console.log(`raw_contactList`, raw_contactList);
  317. if (!isWmpf) {
  318. // 微信呼叫设备
  319. const { list } = await getDeviceVoIPList();
  320. contactList = raw_contactList.map(({ sn, pushToken }) => {
  321. let canCall = true;
  322. if (typeof list === "object") {
  323. const canCallList = list
  324. .filter(
  325. (item) =>
  326. item.status === AuthorizeStatus.ACCEPT
  327. )
  328. .map((item) => item.sn);
  329. canCall = canCallList.includes(sn);
  330. }
  331. return {
  332. id: sn,
  333. name: sn,
  334. canCall,
  335. ticket: pushToken,
  336. };
  337. });
  338. } else {
  339. // 设备呼叫微信
  340. const raw_contactList = await getContactList({
  341. sn: config.sn,
  342. });
  343. console.log(`raw_contactList: 业务方存储`, raw_contactList);
  344. if (raw_contactList.length > 0) {
  345. const canCallList = (
  346. await wmpfVoip.getIotBindContactList({
  347. sn: config.sn,
  348. model_id: config.modelId,
  349. openid_list: raw_contactList.map(
  350. (item) => item.openid
  351. ),
  352. })
  353. ).contact_list
  354. .filter(
  355. (item) => item.status === AuthorizeStatus.ACCEPT
  356. )
  357. .map((item) => item.openid);
  358. console.log(`canCallList: 微信授权存储`, canCallList);
  359. contactList = raw_contactList.map((item) => {
  360. const canCall = canCallList.includes(item.openid);
  361. return {
  362. id: item.openid,
  363. name: item.name,
  364. canCall,
  365. ticket: "",
  366. };
  367. });
  368. }
  369. }
  370. console.log(`getContactList: `, contactList);
  371. this.contactList = contactList;
  372. } catch (error) {
  373. console.error(`getContactList error: `, error);
  374. }
  375. wx.hideLoading();
  376. },
  377. async call(e) {
  378. console.log(`call`, "走到了call方法");
  379. const { sn, idx, type: roomtype } = e;
  380. // console.log(e, this.contactList, 888870);
  381. const target = this.contactList.find((item) => item.id === sn);
  382. console.log("走到了这里了。。。。1", target);
  383. // const target = this.contactList[idx];
  384. const apiTypeName = this.apiTypesValid[this.apiTypeIndex];
  385. const businessType = apiTypes.indexOf(apiTypeName);
  386. try {
  387. if (!target.canCall) {
  388. wx.showToast({
  389. title: "用户未授权或设备不在线",
  390. icon: "none",
  391. });
  392. throw Error("target can not call");
  393. }
  394. console.log("走到了这里了。。。。2");
  395. const _caller = await this._caller.get();
  396. console.log(`caller: `, _caller);
  397. console.log(`listener: `, target);
  398. const { roomId } = await wmpfVoip.callDevice({
  399. roomType: "voice",
  400. sn: sn,
  401. modelId: "iFJ2BNIQcd9WlUIaT7_l4Q",
  402. nickName: "xx",
  403. isCloud: true,
  404. payload: "radar-power.cn",
  405. });
  406. wx.redirectTo({
  407. url: CallPagePlugin,
  408. });
  409. } catch (error) {
  410. console.log(`>>>>>>>>>>>>>>>>>>>>>>>>>>>`);
  411. console.error(`call error`, error);
  412. }
  413. },
  414. // mqtt相关方法
  415. connectMQTTwo() {
  416. if (this.mqttClientFlag) {
  417. console.log("主题已订阅");
  418. return;
  419. }
  420. const THRESHOLD = 2;
  421. const params = {
  422. keepalive: 6000,
  423. clean: true,
  424. connectTimeout: 30 * 1000,
  425. clientId:
  426. "xcx_mqtt_cmd1" +
  427. uni.getStorageSync("userId") +
  428. Math.random().toString(16).substring(2, 8),
  429. username: "lnradar",
  430. password: "lnradar",
  431. // 微信小程序特定配置
  432. wsOptions: {
  433. WebSocket: function (url) {
  434. return wx.connectSocket({
  435. url: url,
  436. header: {
  437. "content-type": "application/json",
  438. },
  439. protocols: ["mqtt"],
  440. });
  441. },
  442. },
  443. reconnectPeriod: 0,
  444. rejectUnauthorized: false, // 仅开发环境使用,生产环境应设为true或移除
  445. };
  446. let client = "";
  447. let selectedService = uni.getStorageSync("sercviceChoice");
  448. if (!selectedService || selectedService == "aloneServe") {
  449. if (__wxConfig.envVersion == "develop") {
  450. client = mqtt.connect(
  451. "wxs://cmd.radar-power.cn/mqtt/",
  452. params
  453. );
  454. }
  455. if (__wxConfig.envVersion == "trial") {
  456. client = mqtt.connect(
  457. "wxs://cmd.radar-power.cn/mqtt/",
  458. params
  459. );
  460. }
  461. }
  462. // 存储client引用以便后续操作
  463. this.mqttClientTwo = client;
  464. client.on("connect", () => {
  465. console.log("MQTT连接成功");
  466. this.mqttClientFlag = true;
  467. let userId = uni.getStorageSync("userId");
  468. client.subscribe(`/mps/wx_${userId}/notice`, (err) => {
  469. if (err) {
  470. console.error("订阅失败", err);
  471. } else {
  472. console.log(
  473. `成功订阅设备主题: /mps/wx_${userId}/notice`
  474. );
  475. }
  476. });
  477. });
  478. client.on("disconnect", () => {
  479. console.log("MQTT不在连接");
  480. });
  481. client.on("error", (err) => {
  482. this.mqttClientFlag = false;
  483. setTimeout(() => {
  484. this.connectMQTTwo();
  485. }, 1000);
  486. });
  487. client.on("reconnect", () => {});
  488. client.on("close", () => {});
  489. client.on("message", (topic, message) => {
  490. console.log(JSON.parse(message.toString()), "8870");
  491. // 处理点位消息
  492. let userId = uni.getStorageSync("userId");
  493. const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
  494. const match = topic.match(noticeMatch);
  495. if (!match) return;
  496. this.alarmModel = true;
  497. const data = JSON.parse(message.toString());
  498. this.clientId = data.clientId;
  499. this.devName = data.devName;
  500. this.sn = data.clientId;
  501. this.eventListId = data.eventListId;
  502. this.getVoipAuthor(this.clientId, userId);
  503. console.log(data, topic, "接收到消息222:");
  504. console.log(
  505. "接收到消息:",
  506. this.clientId,
  507. this.devName,
  508. this.sn,
  509. this.eventListId,
  510. data
  511. );
  512. const now = new Date();
  513. const year = now.getFullYear();
  514. const month = (now.getMonth() + 1).toString().padStart(2, "0");
  515. const day = now.getDate().toString().padStart(2, "0");
  516. const hours = now.getHours().toString().padStart(2, "0");
  517. const minutes = now.getMinutes().toString().padStart(2, "0");
  518. const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
  519. this.nowTime = formattedTime;
  520. console.log("isWmpf:", isWmpf);
  521. if (!isWmpf) {
  522. console.log("isWmpf:", isWmpf);
  523. this.getVoipDevices();
  524. }
  525. console.log("接收到消息:", topic, data);
  526. });
  527. },
  528. hanledList(type) {
  529. this.$http
  530. .get(`wap/event/handleEvent`, {
  531. eventListId: this.eventListId,
  532. })
  533. .then((res) => {
  534. if (res.data.code == 200) {
  535. if (type === "noVoice") {
  536. uni.showToast({
  537. title: "已处理",
  538. icon: "success",
  539. duration: 1500,
  540. });
  541. }
  542. } else {
  543. uni.showToast({
  544. title: "处理失败",
  545. icon: "none",
  546. duration: 1500,
  547. });
  548. }
  549. });
  550. },
  551. closemqtt() {
  552. this.mqttClientFlag = false;
  553. if (this.mqttClientTwo) {
  554. this.mqttClientTwo.end();
  555. this.mqttClientTwo = null;
  556. console.log("MQTT连接已断开");
  557. }
  558. },
  559. getVoipAuthor(clientId, userId) {
  560. console.log(clientId, userId);
  561. this.$http
  562. .post(
  563. "wap/share/queryUserVoipPermission",
  564. {
  565. userId: userId,
  566. clientId: clientId,
  567. },
  568. {
  569. header: {
  570. "Content-Type": "application/json;charset=UTF-8",
  571. },
  572. }
  573. )
  574. .then((res) => {
  575. if (res.data.data) {
  576. console.log(res.data.data, 9999);
  577. if (res.data.data.voipFlag === 0) {
  578. this.voipFlag = true;
  579. } else {
  580. this.voipFlag = false;
  581. }
  582. }
  583. });
  584. },
  585. sendChange() {
  586. this.$emit("sendChange", this.targetPoints);
  587. },
  588. },
  589. mounted() {
  590. this.connectMQTTwo();
  591. },
  592. beforeDestroy() {
  593. this.closemqtt();
  594. },
  595. };
  596. </script>
  597. <style lang="less" scoped>
  598. /* 弹窗部分样式 */
  599. .mask {
  600. position: fixed;
  601. left: 0;
  602. top: 0;
  603. right: 0;
  604. bottom: 0;
  605. background: rgba(0, 0, 0, 0.3);
  606. z-index: 100;
  607. }
  608. .alarm-modal {
  609. width: 675rpx;
  610. height: 1100rpx;
  611. position: fixed;
  612. top: 50%;
  613. left: 50%;
  614. transform: translate(-50%, -50%);
  615. background: linear-gradient(180deg, #ffdfdf 0%, #ffffff 30.3%);
  616. border-radius: 37rpx 37rpx;
  617. z-index: 103;
  618. padding: 50rpx 55rpx;
  619. box-sizing: border-box;
  620. .alarm-header image {
  621. width: 315rpx;
  622. height: 40rpx;
  623. }
  624. .alarm image {
  625. position: fixed;
  626. top: 0;
  627. right: 0;
  628. width: 180rpx;
  629. height: 175rpx;
  630. }
  631. .nowTime {
  632. width: 270rpx;
  633. margin-top: 20rpx;
  634. text-align: center;
  635. align-items: center;
  636. line-height: 100%;
  637. background: #fed5d5;
  638. color: #111111;
  639. font-size: 28rpx;
  640. border-radius: 10rpx;
  641. padding: 5rpx 5rpx;
  642. }
  643. .alarm-info {
  644. margin-top: 40rpx;
  645. color: #111111;
  646. font-size: 28rpx;
  647. letter-spacing: 4%;
  648. }
  649. .backImg {
  650. margin: 37rpx auto;
  651. width: 400rpx;
  652. height: 600rpx;
  653. background-color: #fff;
  654. border: 12rpx solid #333333;
  655. background-image: url(http://jkld.radar-power.com//uploadFiles/framework/file/20250620/toilet_bg.png);
  656. background-repeat: no-repeat;
  657. background-position: center;
  658. image {
  659. position: absolute;
  660. top: 50%;
  661. left: 50%;
  662. width: 60rpx;
  663. height: 60rpx;
  664. }
  665. }
  666. .alarm-bottom {
  667. display: flex;
  668. justify-content: flex-end;
  669. align-items: center;
  670. .alarm-voice {
  671. width: 185rpx;
  672. height: 85rpx;
  673. background: #f3e2dd;
  674. border-radius: 28rpx;
  675. color: #111111;
  676. font-size: 32rpx;
  677. text-align: center;
  678. line-height: 85rpx;
  679. }
  680. .alarm-handle {
  681. margin-left: 20rpx;
  682. width: 185rpx;
  683. height: 85rpx;
  684. background: linear-gradient(114.23deg, #a27867 0%, #74483d 100%);
  685. border-radius: 28rpx;
  686. box-shadow: 0rpx 4.69rpx 18.75rpx rgba(72, 41, 29, 0.15),
  687. 0rpx 9.38rpx 9.38rpx rgba(154, 132, 89, 0.2),
  688. 0rpx -4.69rpx 28.13rpx 4.69rpx #a16647 inset;
  689. color: #ffffff;
  690. font-size: 32rpx;
  691. text-align: center;
  692. line-height: 85rpx;
  693. }
  694. }
  695. }
  696. </style>