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 18KB

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