您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

bluetooth.vue 16KB

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