AI销管
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.
 
 
 
 

504 lines
11 KiB

  1. <template>
  2. <view class="bt">
  3. <view class="bt-t1">工牌开机后,须开启手机蓝牙和位置定位搜索</view>
  4. <view class="bt-c1">
  5. <text>蓝牙</text>
  6. <view @click="openBlue()" :class="['bt-sw',connectState?'bt-sw-ac':'']"></view>
  7. </view>
  8. <view class="bt-t2">
  9. 智能工牌开机后2分钟内会发送蓝牙信号,请先开机后并2分钟内搜索设备;如超时请重新开机搜索!或者请重新进入小程序并开启定位功能!
  10. </view>
  11. <!-- <view class="bt-t3">
  12. 输入或搜索需要查询的SN
  13. </view>
  14. <view class="bt-input1">
  15. <view class="bt-input1-c">
  16. <image src="/static/images/bluetooth-search.png" />
  17. <input v-model="keyword" @confirm="getBluetoothDevices" placeholder="请输入设备SN" />
  18. </view>
  19. <image class="scan" @click="ermScan" src="/static/images/bluetooth-scan.png" />
  20. </view> -->
  21. <view class="bt-c2">
  22. <view class="bt-c2-l">我的工牌</view>
  23. <image @click="startBluetoothDevicesDiscovery" src="/static/images/bluetooth-sx.png"
  24. :class="['bt-c2-r',isSearch?'bt-c2-r-ani':'']" />
  25. </view>
  26. <view v-for="(item,index) in blist" :key="index" @click="goDetail(item)" class="bt-c3">
  27. <view class="bt-c3-l">{{item.name}}</view>
  28. <image src="/static/images/jiantop.png" />
  29. </view>
  30. <view class="bt-c2">
  31. <view class="bt-c2-l">已绑定的工牌</view>
  32. </view>
  33. <view v-for="(item,index) in savelist" :key="index" @click="goDetail(item)" @longpress="delOld(index)" class="bt-c3">
  34. <view class="bt-c3-l">
  35. {{item.name}}
  36. <image mode="heightFix" v-if="item.isConnect && item.isConnect == 1" src="/static/images/bluetooth-con.png" />
  37. </view>
  38. <image src="/static/images/jiantop.png" />
  39. </view>
  40. <view v-if="savelist.length > 0" class="bt-c2" style="margin-top: 20rpx;">
  41. <view class="bt-c2-l">长按删除</view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import {
  47. serviceId,
  48. closeBlueAdapter,
  49. readCharacteristicId,
  50. writeCharacteristicId
  51. } from "@/utils/blue.js";
  52. import {
  53. mapState,
  54. } from 'vuex';
  55. export default {
  56. data() {
  57. return {
  58. privacyVisible: false,
  59. open: false,
  60. serviceId,
  61. readCharacteristicId,
  62. writeCharacteristicId,
  63. blist: [],
  64. keyword: "",
  65. savelist: [],
  66. isSearch: false
  67. }
  68. },
  69. computed: {
  70. ...mapState(['connectDev', 'connectState']),
  71. },
  72. watch: {
  73. connectDev(newVal) {
  74. this.savelist = this.savelist.filter(v => {
  75. v.isConnect = '0';
  76. if (newVal && v.deviceId == newVal.deviceId) v.isConnect = '1';
  77. if (v.name && v.deviceId) return v;
  78. })
  79. }
  80. },
  81. onShow() {
  82. this.keyword = uni.getStorageSync("imeiCode");
  83. if (this.connectState == true) {
  84. this.getBluetoothDevices();
  85. } else {
  86. this.savelist = this.savelist.filter(v => {
  87. v.isConnect = '0';
  88. if (that.connectDev && v.deviceId == that.connectDev.deviceId) v.isConnect = '1';
  89. if (v.name && v.deviceId) return v;
  90. })
  91. }
  92. },
  93. methods: {
  94. //搜索
  95. ermScan() {
  96. var that = this
  97. uni.scanCode({
  98. success: (res) => {
  99. that.keyword = res.result
  100. that.getBluetoothDevices();
  101. },
  102. fail: (err) => {
  103. console.error('扫码失败:' + err);
  104. }
  105. });
  106. },
  107. //打开蓝牙
  108. openBlue() {
  109. var that = this
  110. if (!that.connectState) {
  111. uni.showLoading({
  112. title: "链接中...",
  113. icon: "none"
  114. })
  115. uni.authorize({
  116. scope: 'scope.bluetooth',
  117. success: () => {
  118. uni.openBluetoothAdapter({
  119. success: (res) => {
  120. uni.hideLoading();
  121. that.$store.commit('setConnectState', true);
  122. that.getBluetoothAdapterState();
  123. },
  124. fail: (res) => {
  125. uni.hideLoading();
  126. uni.showToast({
  127. title: "蓝牙适配器打开失败",
  128. icon: "none"
  129. })
  130. }
  131. });
  132. },
  133. fail: () => {
  134. uni.hideLoading();
  135. uni.showModal({
  136. title: '提示',
  137. content: '需要使用蓝牙功能,请授权使用',
  138. showCancel: false,
  139. success: () => {
  140. uni.openSetting();
  141. }
  142. })
  143. }
  144. })
  145. } else {
  146. that.blist = [];
  147. that.savelist = [];
  148. closeBlueAdapter();
  149. }
  150. },
  151. //搜索蓝牙
  152. getBluetoothAdapterState() {
  153. var that = this;
  154. that.blist = [];
  155. uni.getBluetoothAdapterState({
  156. success: function(res) {
  157. if (!res.discovering) {
  158. that.startBluetoothDevicesDiscovery();
  159. }
  160. if (!res.available) {
  161. uni.showModal({
  162. title: '提示',
  163. content: '当前蓝牙适配器不可用',
  164. confirmColor: '#00b6b5',
  165. showCancel: false,
  166. success(res) {}
  167. })
  168. }
  169. },
  170. fail: function(res) {
  171. uni.showModal({
  172. title: '提示',
  173. content: '适配蓝牙失败',
  174. confirmColor: '#00b6b5',
  175. showCancel: false,
  176. success(res) {}
  177. })
  178. }
  179. });
  180. },
  181. //搜索蓝牙
  182. startBluetoothDevicesDiscovery() {
  183. var that = this;
  184. that.isSearch = true
  185. uni.startBluetoothDevicesDiscovery({
  186. allowDuplicatesKey: false,
  187. success: function(res) {
  188. setTimeout(() => {
  189. that.isSearch = false
  190. that.lanya5()
  191. }, 5000)
  192. that.onBluetoothDeviceFound();
  193. },
  194. fail: function(res) {
  195. that.isSearch = false
  196. uni.showModal({
  197. title: '提示',
  198. content: '搜索蓝牙设备失败'
  199. })
  200. }
  201. })
  202. },
  203. //获取所有已发现的设备
  204. getBluetoothDevices() {
  205. var that = this;
  206. that.savelist = [];
  207. let islist = uni.getStorageSync("connectDevList")?uni.getStorageSync("connectDevList"):[]
  208. that.savelist = islist.filter(v => {
  209. v.isConnect = '0';
  210. if (that.connectDev && v.deviceId == that.connectDev.deviceId) v.isConnect = '1';
  211. if (v.name && v.deviceId) return v;
  212. })
  213. console.log(that.keyword,"搜索的设备");
  214. uni.getBluetoothDevices({
  215. success(res){
  216. res.devices.filter(v => {
  217. console.log(v.name,"查找到的设备");
  218. if (!that.blist.find(v2 => v2.deviceId == v.deviceId)) {
  219. if (v.name.toLowerCase() == that.keyword.toLowerCase()) that.blist.push(v);
  220. }
  221. })
  222. }
  223. })
  224. },
  225. // 获取所有已发现的设备
  226. onBluetoothDeviceFound() {
  227. var that = this;
  228. that.savelist = [];
  229. let islist = uni.getStorageSync("connectDevList")?uni.getStorageSync("connectDevList"):[]
  230. that.savelist = islist.filter(v => {
  231. v.isConnect = '0';
  232. if (that.connectDev && v.deviceId == that.connectDev.deviceId) v.isConnect = '1';
  233. if (v.name && v.deviceId) return v;
  234. })
  235. console.log(that.keyword,"搜索的设备");
  236. uni.onBluetoothDeviceFound((res) => {
  237. res.devices.filter(v => {
  238. console.log(v.name,"查找到的设备");
  239. //判断blist中如果没有就添加
  240. if (!that.blist.find(v2 => v2.deviceId == v.deviceId)) {
  241. if (v.name.toLowerCase() == that.keyword.toLowerCase()) that.blist.push(v);
  242. }
  243. })
  244. })
  245. },
  246. //停止搜索周边设备
  247. lanya5() {
  248. uni.getBluetoothAdapterState({
  249. success: function(res) {
  250. if (res.discovering) {
  251. uni.stopBluetoothDevicesDiscovery({
  252. success: function(res) {
  253. console.log("蓝牙停止搜索")
  254. }
  255. })
  256. }
  257. }
  258. })
  259. },
  260. //关闭蓝牙
  261. closeBluetoothAdapter() {
  262. var that = this
  263. uni.closeBluetoothAdapter({
  264. complete() {}
  265. })
  266. },
  267. delOld(index){
  268. var that = this;
  269. uni.showModal({
  270. title: '提示',
  271. content: '确定要删除该工牌吗?',
  272. success(res) {
  273. if (res.confirm) {
  274. let item = that.savelist[index];
  275. if (item.isConnect == 1) {
  276. uni.setStorageSync("lyDetail", item)
  277. uni.showToast({
  278. title: '该工牌蓝牙已连接,请先进入"蓝牙详情"页面断开蓝牙',
  279. duration: 3000,
  280. icon: "none",
  281. })
  282. setTimeout(() => {
  283. uni.navigateTo({
  284. url: '/pages/bluetooth/detail'
  285. })
  286. }, 3000)
  287. return
  288. }
  289. that.savelist.splice(index,1);
  290. uni.setStorageSync("connectDevList",that.savelist);
  291. }
  292. }
  293. })
  294. },
  295. //去详情页
  296. goDetail(item) {
  297. uni.setStorageSync("lyDetail", item)
  298. uni.navigateTo({
  299. url: "/pages/bluetooth/detail"
  300. })
  301. }
  302. }
  303. }
  304. </script>
  305. <style scoped lang="scss">
  306. .bt {
  307. background-color: #F1F2F4;
  308. min-height: 100vh;
  309. width: 100%;
  310. box-sizing: border-box;
  311. padding: 40rpx 28rpx;
  312. .bt-t1 {
  313. font-weight: 400;
  314. font-size: 26rpx;
  315. color: #666666;
  316. margin-bottom: 20rpx;
  317. }
  318. .bt-c1 {
  319. height: 88rpx;
  320. background: #FFFFFF;
  321. border-radius: 12rpx;
  322. display: flex;
  323. align-items: center;
  324. justify-content: space-between;
  325. font-size: 28rpx;
  326. color: #333333;
  327. box-sizing: border-box;
  328. padding: 0 28rpx;
  329. }
  330. .bt-sw {
  331. width: 60rpx;
  332. height: 36rpx;
  333. background: #D8D8D8;
  334. box-shadow: 0rpx 2rpx 4rpx 0rpx rgba(9, 23, 44, 0.3);
  335. border-radius: 18rpx;
  336. display: flex;
  337. align-items: center;
  338. transition: all 0.3s linear;
  339. position: relative;
  340. }
  341. .bt-sw::after {
  342. content: " ";
  343. position: absolute;
  344. left: 8rpx;
  345. width: 20rpx;
  346. height: 20rpx;
  347. background: #FFFFFF;
  348. border-radius: 16rpx;
  349. transition: all 0.3s linear;
  350. }
  351. .bt-sw-ac {
  352. background: #2671E2;
  353. }
  354. .bt-sw-ac::after {
  355. left: 32rpx;
  356. }
  357. .bt-t2 {
  358. font-size: 30rpx;
  359. color: #999999;
  360. margin-top: 20rpx;
  361. line-height: 1.6;
  362. }
  363. .bt-t3 {
  364. margin-top: 60rpx;
  365. font-size: 26rpx;
  366. color: #666666;
  367. }
  368. .bt-input1 {
  369. width: 100%;
  370. background: #FFFFFF;
  371. border-radius: 12rpx;
  372. box-sizing: border-box;
  373. padding: 20rpx;
  374. display: flex;
  375. align-items: center;
  376. justify-content: space-between;
  377. margin-top: 20rpx;
  378. &-c {
  379. width: 100%;
  380. height: 66rpx;
  381. background: #F1F2F4;
  382. border-radius: 8rpx;
  383. display: flex;
  384. align-items: center;
  385. box-sizing: border-box;
  386. padding: 0 20rpx;
  387. image {
  388. width: 36rpx;
  389. height: 36rpx;
  390. margin-right: 10rpx;
  391. flex-shrink: 0;
  392. }
  393. input {
  394. width: 100%;
  395. font-weight: 400;
  396. font-size: 26rpx;
  397. color: #333;
  398. }
  399. }
  400. .scan {
  401. width: 30rpx;
  402. height: 30rpx;
  403. margin-left: 28rpx;
  404. flex-shrink: 0;
  405. }
  406. }
  407. .bt-c2 {
  408. width: 100%;
  409. display: flex;
  410. align-items: center;
  411. justify-content: space-between;
  412. margin-top: 60rpx;
  413. .bt-c2-l {
  414. font-weight: 400;
  415. font-size: 26rpx;
  416. color: #666666;
  417. transform: translateY(-2rpx);
  418. }
  419. .bt-c2-r {
  420. width: 32rpx;
  421. height: 32rpx;
  422. }
  423. .bt-c2-r-ani {
  424. animation: spin 2.5s linear infinite;
  425. }
  426. @keyframes spin {
  427. 0% {
  428. transform: rotate(0deg);
  429. }
  430. 100% {
  431. transform: rotate(360deg);
  432. }
  433. }
  434. }
  435. .bt-c3 {
  436. box-sizing: border-box;
  437. padding: 32rpx;
  438. background: #FFFFFF;
  439. border-radius: 12rpx;
  440. display: flex;
  441. align-items: center;
  442. justify-content: space-between;
  443. margin-top: 20rpx;
  444. .bt-c3-l {
  445. display: flex;
  446. align-items: center;
  447. font-weight: 400;
  448. font-size: 28rpx;
  449. color: #333333;
  450. image {
  451. height: 36rpx;
  452. margin-left: 12rpx;
  453. flex-shrink: 0;
  454. }
  455. }
  456. image {
  457. width: 12rpx;
  458. height: 28rpx;
  459. margin-left: 20rpx;
  460. flex-shrink: 0;
  461. }
  462. }
  463. }
  464. </style>