選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

device-active-step3.vue 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <!-- 设备激活3-设备选择 -->
  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. <!-- 激活成功popup -->
  35. <u-popup v-model="state.showPopup" mode="center">
  36. <active-success @cancel="state.showPopup = false"></active-success>
  37. </u-popup>
  38. </template>
  39. <script setup lang="ts">
  40. import { setItem } from "../../utils/storage";
  41. import { ref, reactive } from "vue";
  42. import { onLoad, onReady } from "@dcloudio/uni-app";
  43. import { fileURL } from "@/datas/fileURL.js";
  44. import activeSuccess from "./components/popup-device-active-success";
  45. const imgURL = `${fileURL}image/`;
  46. const selectedUrl = "common/selected.png`";
  47. const unSelectedUrl = "common/unselected.png`";
  48. const wjApi = require("../../static/etc/WJAPI/wjBleAPI");
  49. // import * as wjApi from '../../lib/etc/WJAPI/wjBleAPI'
  50. const jlApi = require("../../static/etc/JLAPI/JLObuSDK.js");
  51. // import * as jlApi from '../../lib/etc/JLAPI/JLObuSDK.js'
  52. const jyApi = require("../../static/etc/JYAPI/GenvictBleUtil.js");
  53. // import * as jyApi from '../../lib/etc/JYAPI/GenvictBleUtil.js'
  54. const jlQZApi = require("../../static/etc/JLQZAPI/JLObuSDK.js");
  55. // import * as jlQZApi from '../../lib/etc/JLQZAPI/JLObuSDK.js'
  56. const atApi = require("../../static/etc/ATAPI/ArtcBleUtil.js");
  57. // import * as atApi from '../../lib/etc/ATAPI/ArtcBleUtil.js'
  58. const jtApi = require("../../static/etc/JTAPI/BleUtil.js");
  59. // import * as jtApi from '../../lib/etc/JTAPI/BleUtil.js'
  60. const wqApi = require("../../static/etc/WQAPI/WCDObuSdk.js");
  61. // import * as wqApi from '../../lib/etc/WQAPI/WCDObuSdk.js'
  62. const cgApi = require("../../static/etc/CGAPI/cguBle.js");
  63. // import * as cgApi from '../../lib/etc/CGAPI/cguBle.js'
  64. const tdApi = require("../../static/etc/TDAPI/TDRObuSDK.js");
  65. // import * as tdApi from '../../lib/etc/TDAPI/TDRObuSDK.js'
  66. const zzApi = require("../../static/etc/WJAPI/wjBleAPI.js");
  67. // import * as zzApi from '../../lib/etc/WJAPI/wjBleAPI'
  68. const tools = require("../../static/etcUtil/tools.js");
  69. const datas = require("../../static/etcUtil/datas.js");
  70. const bluetoothUtil = require("../../static/etcUtil/index.js");
  71. const state = reactive({
  72. showPopup: false, //显示激活成功提示
  73. curDeviceId: "", //当前选中的设备ID
  74. deviceList: [
  75. //设备列表
  76. {
  77. id: "1",
  78. image: imgURL + "bluetooth/card1.png`",
  79. name: "聚力",
  80. number: "235564444558855",
  81. },
  82. ],
  83. });
  84. const deviceList = ref([]);
  85. const connectPrefixName = ref(null);
  86. const routeType = ref(null); //来源
  87. interface DeviceType {
  88. name: string; //设备名称
  89. deviceId: string; //uuid
  90. prefixName: string; //前缀名称
  91. selected: boolean; //判断点击次数
  92. _name: string; //中文名称
  93. }
  94. let device: DeviceType = reactive({
  95. name: "", //设备名称
  96. deviceId: "", //uuid
  97. prefixName: "", //前缀名称
  98. selected: false, //判断点击次数
  99. _name: "", //中文名称
  100. });
  101. onReady(() => {
  102. load();
  103. });
  104. onLoad((option) => {
  105. routeType.value = option.routeType ? option.routeType : "1";
  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. console.log("扫描蓝牙中");
  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. console.log("扫描蓝牙中");
  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. // var id = e.currentTarget.id;
  313. // console.log(id)
  314. // device = deviceList[id];
  315. // if (device == undefined || device == 'undefined' || device == "" || device.selected == undefined) {
  316. // console.info('device is undefined');
  317. // // that.linkFail(); //未找到设备, 请重新搜索
  318. // return;
  319. // }
  320. console.log(device);
  321. if (item.selected == false) {
  322. console.log("第一次点击了");
  323. item.selected = true;
  324. setTimeout(function () {
  325. if (device.selected == undefined) {
  326. console.info("selected is undefined");
  327. linkFail(); //未找到设备, 请重新搜索
  328. return;
  329. }
  330. item.selected = false;
  331. }, 4000);
  332. } else {
  333. console.log("第二次点击了");
  334. return;
  335. }
  336. if (
  337. item.prefixName == undefined ||
  338. item.prefixName == "undefined" ||
  339. item.prefixName == ""
  340. ) {
  341. console.info("device.prefixName is undefined");
  342. linkFail(); //未找到设备, 请重新搜索
  343. return;
  344. }
  345. let prefixName = item.prefixName;
  346. connectPrefixName.value = item.prefixName;
  347. // delete device.prefixName
  348. // delete device._name
  349. console.log(item);
  350. tools.showLoadingAlert("蓝牙连接中");
  351. console.info("连接的是" + prefixName);
  352. switch (prefixName) {
  353. case "WJ":
  354. wjApi.connectDevice(
  355. device,
  356. function (res) {
  357. connectSuccess(res);
  358. },
  359. function (res) {
  360. listenStatus(res);
  361. }
  362. );
  363. break;
  364. case "JL":
  365. jlApi.connectDevice(
  366. device,
  367. function (res) {
  368. connectSuccess(res);
  369. },
  370. function (res) {
  371. listenStatus(res);
  372. }
  373. );
  374. break;
  375. case "ETC":
  376. jlQZApi.connectDevice(
  377. device,
  378. function (res) {
  379. preDevice(res);
  380. },
  381. function (res) {
  382. listenStatus(res);
  383. }
  384. );
  385. break;
  386. case "JY":
  387. jyApi.connectDevice(
  388. device,
  389. function (res) {
  390. connectSuccess(res);
  391. },
  392. function (res) {
  393. listenStatus(res);
  394. }
  395. );
  396. break;
  397. case "AT":
  398. atApi.connectDevice(
  399. device,
  400. function (res) {
  401. connectSuccess(res);
  402. },
  403. function (res) {
  404. listenStatus(res);
  405. }
  406. );
  407. break;
  408. case "JT":
  409. jtApi.connectDevice(
  410. device,
  411. function (res) {
  412. connectSuccess(res);
  413. },
  414. function (res) {
  415. listenStatus(res);
  416. }
  417. );
  418. break;
  419. case "WQ":
  420. wqApi.connectDevice(
  421. device,
  422. function (res) {
  423. connectSuccess(res);
  424. },
  425. function (res) {
  426. listenStatus(res);
  427. }
  428. );
  429. break;
  430. case "CG":
  431. cgApi.connectDevice(
  432. device,
  433. function (res) {
  434. connectSuccess(res);
  435. },
  436. function (res) {
  437. listenStatus(res);
  438. }
  439. );
  440. break;
  441. case "TD":
  442. tdApi.connectDevice(
  443. device,
  444. function (res) {
  445. connectSuccess(res);
  446. },
  447. function (res) {
  448. listenStatus(res);
  449. }
  450. );
  451. break;
  452. case "ZZ":
  453. zzApi.connectDevice(
  454. device,
  455. function (res) {
  456. connectSuccess(res);
  457. },
  458. function (res) {
  459. listenStatus(res);
  460. }
  461. );
  462. break;
  463. default: //未找到设备, 请重新搜索
  464. linkFail();
  465. break;
  466. }
  467. },
  468. fail: function (res) {
  469. console.log(res);
  470. linkFail(); //未找到设备, 请重新搜索
  471. },
  472. });
  473. };
  474. /**
  475. * 未找到设备, 请重新搜索
  476. */
  477. const linkFail = () => {
  478. datas.setData("bluLinkStatus", false);
  479. datas.setData("connectPrefixName", "");
  480. tools.showModalAlert("未找到设备, 请重新搜索", function successFunc() {
  481. load();
  482. });
  483. };
  484. /**
  485. * 连接成功
  486. */
  487. const connectSuccess = (res) => {
  488. console.log("连接回调函数func1");
  489. console.log(res);
  490. if (res.code == 0) {
  491. console.log("连接成功");
  492. datas.setData("bluLinkStatus", true);
  493. datas.setData("connectPrefixName", connectPrefixName.value);
  494. //routeType 1.激活(订单来) 2.圈存 (/pages/recharge/recharge来)
  495. if (routeType.value == "1") {
  496. uni.navigateTo({
  497. url: "/pages/bluetooth/result",
  498. });
  499. } else if (routeType.value == "2") {
  500. uni.navigateTo({
  501. url: "/pages/recharge/recharge?connectSuccess=1",
  502. });
  503. } else {
  504. return;
  505. }
  506. } else {
  507. alertF(res.msg);
  508. }
  509. };
  510. /**
  511. * 前装设备
  512. */
  513. const preDevice = (res) => {
  514. if (res.code == 0) {
  515. console.log("连接成功");
  516. datas.setData("bluLinkStatus", true);
  517. datas.setData("connectPrefixName", connectPrefixName.value);
  518. // tools.toUrl(route.preActivateInfo);//跳转 前装设备
  519. } else {
  520. alertF(res.msg);
  521. console.log(res.msg);
  522. }
  523. };
  524. /**
  525. * 监听蓝牙状态
  526. */
  527. const listenStatus = (res) => {
  528. console.log("时时监听蓝牙状态func2");
  529. console.log(res);
  530. if (res.code == 0) {
  531. datas.setData("bluLinkStatus", true);
  532. datas.setData("connectPrefixName", connectPrefixName.value);
  533. } else {
  534. datas.setData("bluLinkStatus", false);
  535. datas.setData("connectPrefixName", "");
  536. tools.showToastAlert("蓝牙已断开");
  537. }
  538. };
  539. /**
  540. * 提示加关蓝牙
  541. */
  542. const alertF = (msg: string) => {
  543. //隐藏加载框
  544. tools.hideLoadingAlert();
  545. //断开蓝牙
  546. bluetoothUtil.disconnectDevice();
  547. //提示对话框
  548. tools.showModalAlert(msg);
  549. };
  550. </script>
  551. <style>
  552. page {
  553. background-color: #f3f3f3;
  554. }
  555. :deep(.u-mode-center-box) {
  556. border-radius: 20rpx;
  557. }
  558. </style>
  559. <style lang="scss" scoped>
  560. .devices {
  561. .device {
  562. width: calc(100% - 30rpx);
  563. margin-left: 30rpx;
  564. display: flex;
  565. flex-direction: row;
  566. align-items: center;
  567. margin-top: 30rpx;
  568. background: white;
  569. padding: 25rpx 20rpx;
  570. border-top-left-radius: 20rpx;
  571. border-bottom-left-radius: 20rpx;
  572. box-shadow: 0px 10px 10rpx 10rpx rgba(223, 223, 223, 0.3);
  573. .head {
  574. width: 120rpx;
  575. height: 120rpx;
  576. border-radius: 10rpx;
  577. background-color: #f3f3f3;
  578. }
  579. .center {
  580. flex: 1;
  581. margin-left: 30rpx;
  582. margin-right: 30rpx;
  583. .name {
  584. font-size: 30rpx;
  585. color: #333333;
  586. }
  587. .desc {
  588. font-size: 26rpx;
  589. color: #666666;
  590. margin-top: 25rpx;
  591. }
  592. }
  593. .icon {
  594. width: 43rpx;
  595. height: 43rpx;
  596. margin-right: 10rpx;
  597. }
  598. }
  599. .hint {
  600. margin: 60rpx 30rpx 0px;
  601. .orange-txt {
  602. font-size: 26rpx;
  603. color: #ff8000;
  604. image {
  605. width: 30rpx;
  606. height: 30rpx;
  607. margin-right: 10rpx;
  608. }
  609. }
  610. .grey-txt {
  611. font-size: 26rpx;
  612. color: #666666;
  613. line-height: 40rpx;
  614. margin-top: 16rpx;
  615. }
  616. }
  617. .btn {
  618. margin: 70rpx 40rpx;
  619. }
  620. }
  621. </style>