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

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