You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

device-active-step3.vue 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <!-- 设备激活3-设备选择 -->
  2. <template>
  3. <view class="devices">
  4. <view
  5. class="device"
  6. v-for="(item, index) in deviceList"
  7. :key="index"
  8. @click="connectDevice(item)"
  9. >
  10. <image :src="`${$imgUrl}bluetooth/card1.png`" class="head"></image>
  11. <view class="center">
  12. <view class="name">{{ item._name }}</view>
  13. <view class="desc">编号 {{ item.name }}</view>
  14. </view>
  15. <image
  16. :src="item.selected ? selectedUrl : unSelectedUrl"
  17. class="icon"
  18. ></image>
  19. </view>
  20. <view class="hint">
  21. <view class="orange-txt as-layout-horizontal as-gravity-center-start">
  22. <image :src="`${$imgUrl}common/icon-hint.png`"></image>
  23. <view>温馨提示:</view>
  24. </view>
  25. <view class="grey-txt"
  26. >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  27. 指令执行过程中,请勿关闭蓝牙,勿将手机远离电子标签,以免导致写卡、写签失败。</view
  28. >
  29. </view>
  30. <view class="btn">
  31. <submit-button title="点击重新搜索蓝牙" @submit="load"></submit-button>
  32. </view>
  33. </view>
  34. <!-- 激活成功popup -->
  35. <u-popup v-model="state.showPopup" mode="center">
  36. <active-success @cancel="state.showPopup = false"></active-success>
  37. </u-popup>
  38. </template>
  39. <script setup lang="ts">
  40. import { setItem } from "../../utils/storage";
  41. import { ref, reactive } from "vue";
  42. import { onLoad, onReady } from "@dcloudio/uni-app";
  43. import { fileURL } from "@/datas/fileURL.js";
  44. import activeSuccess from "./components/popup-device-active-success";
  45. const imgURL = `${fileURL}image/`;
  46. const selectedUrl = "common/selected.png`";
  47. const unSelectedUrl = "common/unselected.png`";
  48. const wjApi = require("../../static/etc/WJAPI/wjBleAPI");
  49. // import * as wjApi from '../../lib/etc/WJAPI/wjBleAPI'
  50. const jlApi = require("../../static/etc/JLAPI/JLObuSDK.js");
  51. // import * as jlApi from '../../lib/etc/JLAPI/JLObuSDK.js'
  52. const jyApi = require("../../static/etc/JYAPI/GenvictBleUtil.js");
  53. // import * as jyApi from '../../lib/etc/JYAPI/GenvictBleUtil.js'
  54. const jlQZApi = require("../../static/etc/JLQZAPI/JLObuSDK.js");
  55. // import * as jlQZApi from '../../lib/etc/JLQZAPI/JLObuSDK.js'
  56. const atApi = require("../../static/etc/ATAPI/ArtcBleUtil.js");
  57. // import * as atApi from '../../lib/etc/ATAPI/ArtcBleUtil.js'
  58. const jtApi = require("../../static/etc/JTAPI/BleUtil.js");
  59. // import * as jtApi from '../../lib/etc/JTAPI/BleUtil.js'
  60. const wqApi = require("../../static/etc/WQAPI/WCDObuSdk.js");
  61. // import * as wqApi from '../../lib/etc/WQAPI/WCDObuSdk.js'
  62. const cgApi = require("../../static/etc/CGAPI/cguBle.js");
  63. // import * as cgApi from '../../lib/etc/CGAPI/cguBle.js'
  64. const tdApi = require("../../static/etc/TDAPI/TDRObuSDK.js");
  65. // import * as tdApi from '../../lib/etc/TDAPI/TDRObuSDK.js'
  66. const zzApi = require("../../static/etc/WJAPI/wjBleAPI.js");
  67. // import * as zzApi from '../../lib/etc/WJAPI/wjBleAPI'
  68. const tools = require("../../static/etcUtil/tools.js");
  69. const datas = require("../../static/etcUtil/datas.js");
  70. const bluetoothUtil = require("../../static/etcUtil/index.js");
  71. const state = reactive({
  72. cardId:'',
  73. showPopup: false, //显示激活成功提示
  74. curDeviceId: "", //当前选中的设备ID
  75. deviceList: [
  76. //设备列表
  77. {
  78. id: "1",
  79. image: imgURL + "bluetooth/card1.png`",
  80. name: "聚力",
  81. number: "235564444558855",
  82. },
  83. ],
  84. });
  85. const deviceList = ref([]);
  86. const connectPrefixName = ref(null);
  87. const routeType = ref(null); //来源
  88. interface DeviceType {
  89. name: string; //设备名称
  90. deviceId: string; //uuid
  91. prefixName: string; //前缀名称
  92. selected: boolean; //判断点击次数
  93. _name: string; //中文名称
  94. }
  95. let device: DeviceType = reactive({
  96. name: "", //设备名称
  97. deviceId: "", //uuid
  98. prefixName: "", //前缀名称
  99. selected: false, //判断点击次数
  100. _name: "", //中文名称
  101. });
  102. onReady(() => {
  103. load();
  104. });
  105. onLoad((option) => {
  106. routeType.value = option.routeType ? option.routeType : "1";
  107. state.cardId = option.cardId
  108. });
  109. /*
  110. * 蓝牙初始化
  111. */
  112. const load = () => {
  113. deviceList.value = [];
  114. console.log("****************蓝牙getsetting******************");
  115. uni.getSetting({
  116. success(res) {
  117. console.log("****************成功******************");
  118. console.log("scopebluetooth:" + res.authSetting["scope.bluetooth"]);
  119. console.log("成功结果:" + JSON.stringify(res));
  120. if (res.authSetting["scope.bluetooth"] == undefined) {
  121. //不存在
  122. uni.authorize({
  123. scope: "scope.bluetooth",
  124. success() {
  125. console.log("扫描蓝牙中");
  126. tools.showLoadingAlert("扫描蓝牙中");
  127. openBluetooth();
  128. },
  129. fail() {
  130. uni.showToast({
  131. title: "蓝牙授权失败",
  132. icon: "error",
  133. duration: 1500,
  134. });
  135. },
  136. });
  137. } else if (!res.authSetting["scope.bluetooth"]) {
  138. //false
  139. uni.showModal({
  140. title: "是否授权读取蓝牙",
  141. content: "需要获取你的蓝牙,请确认授权,否则无法获取蓝牙",
  142. success: function (mres) {
  143. if (mres.confirm) {
  144. uni.openSetting({
  145. success(authData) {
  146. if (authData.authSetting["scope.bluetooth"] == true) {
  147. console.log("扫描蓝牙中");
  148. openBluetooth();
  149. } else {
  150. uni.closeBluetoothAdapter();
  151. uni.showToast({
  152. title: "蓝牙授权失败",
  153. icon: "error",
  154. duration: 1500,
  155. });
  156. }
  157. },
  158. });
  159. } else if (mres.cancel) {
  160. uni.showToast({
  161. title: "蓝牙授权失败",
  162. icon: "error",
  163. duration: 1000,
  164. });
  165. }
  166. },
  167. });
  168. } else {
  169. console.log("扫描蓝牙中");
  170. openBluetooth();
  171. }
  172. },
  173. fail(res) {
  174. console.log("****************失败******************");
  175. console.log("失败结果:" + JSON.stringify(res));
  176. },
  177. });
  178. };
  179. /*
  180. * 打开蓝牙
  181. */
  182. const openBluetooth = () => {
  183. let foundDevices = []; //扫描到的蓝牙列表
  184. uni.closeBluetoothAdapter(); //先关闭蓝牙
  185. //打开蓝牙
  186. uni.openBluetoothAdapter({
  187. success: function (res) {
  188. uni.startBluetoothDevicesDiscovery({
  189. success: function (res) {
  190. console.log(res);
  191. //扫描结果的监听
  192. uni.onBluetoothDeviceFound(function (res) {
  193. // console.log(res.devices);
  194. for (let i = 0; i < res.devices.length; i++) {
  195. let name = res.devices[i]["name"];
  196. let prefixName = "";
  197. let deviceId = res.devices[i]["deviceId"];
  198. console.log(res.devices[i]["name"]);
  199. if (name != "" && name != undefined && name != "undefined") {
  200. if (
  201. name.indexOf("G-WJ") != -1 ||
  202. name.indexOf("ETC") != -1 ||
  203. name.indexOf("G-JL") != -1
  204. ) {
  205. //前装设备
  206. prefixName = "ETC";
  207. } else {
  208. //聚力临时设备
  209. if (name.indexOf("5201121") != -1) {
  210. prefixName = "JL";
  211. } else {
  212. prefixName = name.substring(0, 2);
  213. }
  214. }
  215. prefixName = prefixName.toUpperCase();
  216. if (
  217. prefixName == "WJ" ||
  218. prefixName == "JL" ||
  219. prefixName == "JY" ||
  220. prefixName == "AT" ||
  221. prefixName == "JT" ||
  222. prefixName == "WQ" ||
  223. prefixName == "CG" ||
  224. prefixName == "TD" ||
  225. prefixName == "ZZ" ||
  226. prefixName == "ETC"
  227. ) {
  228. // //隐藏加载框
  229. tools.hideLoadingAlert();
  230. device.name = name;
  231. device.deviceId = deviceId;
  232. device.prefixName = prefixName;
  233. device.selected = false; //防止重复点击
  234. let _name = "";
  235. switch (prefixName) {
  236. case "WJ":
  237. _name = "万集";
  238. break;
  239. case "JL":
  240. _name = "聚利";
  241. break;
  242. case "JY":
  243. _name = "金溢";
  244. break;
  245. case "AT":
  246. _name = "埃特斯";
  247. break;
  248. case "JT":
  249. _name = "建投";
  250. break;
  251. case "WQ":
  252. _name = "握奇";
  253. break;
  254. case "CG":
  255. _name = "成谷";
  256. break;
  257. case "TD":
  258. _name = "天地融";
  259. break;
  260. case "ZZ":
  261. _name = "智载";
  262. break;
  263. case "ETC":
  264. _name = "前装";
  265. break;
  266. default:
  267. _name = "未知";
  268. break;
  269. }
  270. device._name = _name;
  271. if (deviceList.value.length == 0) {
  272. foundDevices.push(device);
  273. } else {
  274. let isHave = false;
  275. for (let j = 0; j < foundDevices.length; j++) {
  276. if (name == foundDevices[j].deviceId) {
  277. isHave = true;
  278. break;
  279. }
  280. }
  281. if (!isHave) {
  282. foundDevices.push(device);
  283. }
  284. }
  285. deviceList.value = foundDevices;
  286. foundDevices = [];
  287. // console.log(foundDevices);
  288. }
  289. }
  290. }
  291. });
  292. },
  293. fail: function (res) {
  294. console.log(res);
  295. },
  296. });
  297. },
  298. fail: function (res) {
  299. console.log(res);
  300. alertF("手机蓝牙未打开或不支持蓝牙");
  301. },
  302. });
  303. };
  304. /**
  305. * 连接蓝牙
  306. */
  307. const connectDevice = (e) => {
  308. console.log(e);
  309. let item = e;
  310. //停止扫描蓝牙
  311. console.info("停止扫描蓝牙");
  312. wx.stopBluetoothDevicesDiscovery({
  313. success: function (res) {
  314. // var id = e.currentTarget.id;
  315. // console.log(id)
  316. // device = deviceList[id];
  317. // if (device == undefined || device == 'undefined' || device == "" || device.selected == undefined) {
  318. // console.info('device is undefined');
  319. // // that.linkFail(); //未找到设备, 请重新搜索
  320. // return;
  321. // }
  322. console.log(device);
  323. if (item.selected == false) {
  324. console.log("第一次点击了");
  325. item.selected = true;
  326. setTimeout(function () {
  327. if (device.selected == undefined) {
  328. console.info("selected is undefined");
  329. linkFail(); //未找到设备, 请重新搜索
  330. return;
  331. }
  332. item.selected = false;
  333. }, 4000);
  334. } else {
  335. console.log("第二次点击了");
  336. return;
  337. }
  338. if (
  339. item.prefixName == undefined ||
  340. item.prefixName == "undefined" ||
  341. item.prefixName == ""
  342. ) {
  343. console.info("device.prefixName is undefined");
  344. linkFail(); //未找到设备, 请重新搜索
  345. return;
  346. }
  347. let prefixName = item.prefixName;
  348. connectPrefixName.value = item.prefixName;
  349. // delete device.prefixName
  350. // delete device._name
  351. console.log(item);
  352. tools.showLoadingAlert("蓝牙连接中");
  353. console.info("连接的是" + prefixName);
  354. switch (prefixName) {
  355. case "WJ":
  356. wjApi.connectDevice(
  357. device,
  358. function (res) {
  359. connectSuccess(res);
  360. },
  361. function (res) {
  362. listenStatus(res);
  363. }
  364. );
  365. break;
  366. case "JL":
  367. jlApi.connectDevice(
  368. device,
  369. function (res) {
  370. connectSuccess(res);
  371. },
  372. function (res) {
  373. listenStatus(res);
  374. }
  375. );
  376. break;
  377. case "ETC":
  378. jlQZApi.connectDevice(
  379. device,
  380. function (res) {
  381. preDevice(res);
  382. },
  383. function (res) {
  384. listenStatus(res);
  385. }
  386. );
  387. break;
  388. case "JY":
  389. jyApi.connectDevice(
  390. device,
  391. function (res) {
  392. connectSuccess(res);
  393. },
  394. function (res) {
  395. listenStatus(res);
  396. }
  397. );
  398. break;
  399. case "AT":
  400. atApi.connectDevice(
  401. device,
  402. function (res) {
  403. connectSuccess(res);
  404. },
  405. function (res) {
  406. listenStatus(res);
  407. }
  408. );
  409. break;
  410. case "JT":
  411. jtApi.connectDevice(
  412. device,
  413. function (res) {
  414. connectSuccess(res);
  415. },
  416. function (res) {
  417. listenStatus(res);
  418. }
  419. );
  420. break;
  421. case "WQ":
  422. wqApi.connectDevice(
  423. device,
  424. function (res) {
  425. connectSuccess(res);
  426. },
  427. function (res) {
  428. listenStatus(res);
  429. }
  430. );
  431. break;
  432. case "CG":
  433. cgApi.connectDevice(
  434. device,
  435. function (res) {
  436. connectSuccess(res);
  437. },
  438. function (res) {
  439. listenStatus(res);
  440. }
  441. );
  442. break;
  443. case "TD":
  444. tdApi.connectDevice(
  445. device,
  446. function (res) {
  447. connectSuccess(res);
  448. },
  449. function (res) {
  450. listenStatus(res);
  451. }
  452. );
  453. break;
  454. case "ZZ":
  455. zzApi.connectDevice(
  456. device,
  457. function (res) {
  458. connectSuccess(res);
  459. },
  460. function (res) {
  461. listenStatus(res);
  462. }
  463. );
  464. break;
  465. default: //未找到设备, 请重新搜索
  466. linkFail();
  467. break;
  468. }
  469. },
  470. fail: function (res) {
  471. console.log(res);
  472. linkFail(); //未找到设备, 请重新搜索
  473. },
  474. });
  475. };
  476. /**
  477. * 未找到设备, 请重新搜索
  478. */
  479. const linkFail = () => {
  480. datas.setData("bluLinkStatus", false);
  481. datas.setData("connectPrefixName", "");
  482. tools.showModalAlert("未找到设备, 请重新搜索", function successFunc() {
  483. load();
  484. });
  485. };
  486. /**
  487. * 连接成功
  488. */
  489. const connectSuccess = (res) => {
  490. console.log("连接回调函数func1");
  491. console.log(res);
  492. if (res.code == 0) {
  493. console.log("连接成功");
  494. datas.setData("bluLinkStatus", true);
  495. datas.setData("connectPrefixName", connectPrefixName.value);
  496. //routeType 1.激活(订单来) 2.圈存 (/pages/recharge/recharge来)
  497. if (routeType.value == "1") {
  498. uni.navigateTo({
  499. url: "/pages/bluetooth/result",
  500. });
  501. } else if (routeType.value == "2") {
  502. uni.navigateTo({
  503. url: `/pages/recharge/recharge?connectSuccess=1&&cardId=${state.cardId}`,
  504. });
  505. } else {
  506. return;
  507. }
  508. } else {
  509. alertF(res.msg);
  510. }
  511. };
  512. /**
  513. * 前装设备
  514. */
  515. const preDevice = (res) => {
  516. if (res.code == 0) {
  517. console.log("连接成功");
  518. datas.setData("bluLinkStatus", true);
  519. datas.setData("connectPrefixName", connectPrefixName.value);
  520. // tools.toUrl(route.preActivateInfo);//跳转 前装设备
  521. } else {
  522. alertF(res.msg);
  523. console.log(res.msg);
  524. }
  525. };
  526. /**
  527. * 监听蓝牙状态
  528. */
  529. const listenStatus = (res) => {
  530. console.log("时时监听蓝牙状态func2");
  531. console.log(res);
  532. if (res.code == 0) {
  533. datas.setData("bluLinkStatus", true);
  534. datas.setData("connectPrefixName", connectPrefixName.value);
  535. } else {
  536. datas.setData("bluLinkStatus", false);
  537. datas.setData("connectPrefixName", "");
  538. tools.showToastAlert("蓝牙已断开");
  539. }
  540. };
  541. /**
  542. * 提示加关蓝牙
  543. */
  544. const alertF = (msg: string) => {
  545. //隐藏加载框
  546. tools.hideLoadingAlert();
  547. //断开蓝牙
  548. bluetoothUtil.disconnectDevice();
  549. //提示对话框
  550. tools.showModalAlert(msg);
  551. };
  552. </script>
  553. <style>
  554. page {
  555. background-color: #f3f3f3;
  556. }
  557. :deep(.u-mode-center-box) {
  558. border-radius: 20rpx;
  559. }
  560. </style>
  561. <style lang="scss" scoped>
  562. .devices {
  563. .device {
  564. width: calc(100% - 30rpx);
  565. margin-left: 30rpx;
  566. display: flex;
  567. flex-direction: row;
  568. align-items: center;
  569. margin-top: 30rpx;
  570. background: white;
  571. padding: 25rpx 20rpx;
  572. border-top-left-radius: 20rpx;
  573. border-bottom-left-radius: 20rpx;
  574. box-shadow: 0px 10px 10rpx 10rpx rgba(223, 223, 223, 0.3);
  575. .head {
  576. width: 120rpx;
  577. height: 120rpx;
  578. border-radius: 10rpx;
  579. background-color: #f3f3f3;
  580. }
  581. .center {
  582. flex: 1;
  583. margin-left: 30rpx;
  584. margin-right: 30rpx;
  585. .name {
  586. font-size: 30rpx;
  587. color: #333333;
  588. }
  589. .desc {
  590. font-size: 26rpx;
  591. color: #666666;
  592. margin-top: 25rpx;
  593. }
  594. }
  595. .icon {
  596. width: 43rpx;
  597. height: 43rpx;
  598. margin-right: 10rpx;
  599. }
  600. }
  601. .hint {
  602. margin: 60rpx 30rpx 0px;
  603. .orange-txt {
  604. font-size: 26rpx;
  605. color: #ff8000;
  606. image {
  607. width: 30rpx;
  608. height: 30rpx;
  609. margin-right: 10rpx;
  610. }
  611. }
  612. .grey-txt {
  613. font-size: 26rpx;
  614. color: #666666;
  615. line-height: 40rpx;
  616. margin-top: 16rpx;
  617. }
  618. }
  619. .btn {
  620. margin: 70rpx 40rpx;
  621. }
  622. }
  623. </style>