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.

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