Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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