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.

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