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.

bluetooth.vue 17KB

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