Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

bluetooth.vue 17KB

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