index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <template>
  2. <view class="container" style="height: 100vh" >
  3. <scroll-view class="main-container" scroll-y="true"
  4. :refresher-enabled="false">
  5. <view v-for="(item, index) in deviceListDataShow" :key="item.id" class="list-item" hover-class="list-item-hover"
  6. hover-start-time="0" hover-stay-time="100" @click="listViewTap(item.id)">
  7. <image v-if="item.manufacturer==='eciot'" src="/static/images/new/starts/ble.png" class="list-item-img"></image>
  8. <image v-else src="/static/images/new/starts/bleof.png" class="list-item-img"></image>
  9. <text class="list-item-name">{{item.name}}</text>
  10. <image v-if="item.rssi >= -41" src="/static/img/s5.png" mode="aspectFit" class="list-item-rssi-img"></image>
  11. <image v-else-if="item.rssi >= -55" src="/static/img/s4.png" mode="aspectFit" class="list-item-rssi-img"></image>
  12. <image v-else-if="item.rssi >= -65" src="/static/img/s3.png" mode="aspectFit" class="list-item-rssi-img"></image>
  13. <image v-else-if="item.rssi >= -75" src="/static/img/s2.png" mode="aspectFit" class="list-item-rssi-img"></image>
  14. <image v-else="item.rssi < -75" src="/static/img/s1.png" mode="aspectFit" class="list-item-rssi-img"></image>
  15. <text class="list-item-rssi">{{item.rssi}}</text>
  16. <view class="list-item-line"></view>
  17. </view>
  18. <u-modal :show="showPwd" :confirmText="i18('确认')" :cancelText="i18('取消')" @confirm="inputPwd" @cancel="cancel" :showCancelButton="true" :title="$t('buletooth.pwdinput')" >
  19. <view class="slot-content">
  20. <view>
  21. <u--input
  22. type="number"
  23. :placeholder="$t('buletooth.pwdtip')"
  24. border="surround"
  25. v-model="pwd"
  26. ></u--input>
  27. </view>
  28. <view style="margin:10px;text-decoration: underline;margin-bottom: 0px" @click="forgetPwd">
  29. {{i18('忘记密码')}}?
  30. </view>
  31. </view>
  32. </u-modal>
  33. <u-modal :show="showDeviceNo" :confirmText="i18('确认')" :cancelText="i18('取消')" @confirm="resetPwd" @cancel="cancel" :showCancelButton="true" :title="i18('重置密码')" >
  34. <view class="slot-content">
  35. <view>
  36. <u--input
  37. type="number"
  38. :placeholder="i18('请输入序列号')"
  39. border="surround"
  40. v-model="inputDeviceNo"
  41. ></u--input>
  42. </view>
  43. <view style="margin:10px;text-decoration: underline;margin-bottom: 0px" @click="forgetDeviceNo">
  44. {{i18('忘记序列号')}}?
  45. </view>
  46. </view>
  47. </u-modal>
  48. <view v-if="deviceListDataShow.length==0" class="notice"> - {{ $t('buletooth.nodevice') }} -</view>
  49. <view class="gap"></view>
  50. </scroll-view>
  51. </view>
  52. </template>
  53. <script>
  54. import {getPwd,setPwd,parsePwd,sendPortDetailCmd,getUUID,parseDataObj} from "@/utils/weitiandi/device/device.js";
  55. import w_md5 from "@/js_sdk/zww-md5/w_md5.js"
  56. // #ifdef APP
  57. import ecUI from '@/utils/ecUI.js'
  58. import ecBLE from '@/utils/ecBLE/ecBLE.js'
  59. // #endif
  60. // #ifdef MP
  61. const ecUI = require('@/utils/ecUI.js')
  62. const ecBLE = require('@/utils/ecBLE/ecBLE.js')
  63. // #endif
  64. import i18 from '@/utils/i18.js'
  65. let ctx
  66. let deviceListData = []
  67. export default {
  68. data() {
  69. return {
  70. showPwd:false,
  71. rightPwd:"",
  72. pwd:"",
  73. timer:"",
  74. buleid:"",
  75. deviceListDataShow: [],
  76. showTimer:null,
  77. connected:false,
  78. uuid:"",
  79. showDeviceNo:false,
  80. inputDeviceNo:"",
  81. commonCode:""
  82. }
  83. },
  84. onLoad() {
  85. uni.setNavigationBarTitle({
  86. title: this.$t('page.bluetooth')
  87. })
  88. let titles = i18(this.$t('buletooth.init'));
  89. uni.showLoading({
  90. titles,
  91. mask:true,
  92. });
  93. // ecUI.showLoading(this.$t('buletooth.init'))
  94. ctx = this
  95. clearInterval(this.timer);
  96. this.timer = setInterval(() => {
  97. ctx.deviceListDataShow = JSON.parse(JSON.stringify(deviceListData))
  98. }, 800)
  99. this.commonCode = this.generateUniqueNumber(this.getBeijingTime());
  100. console.log(this.commonCode)
  101. },
  102. onUnload(){
  103. ecBLE.stopBluetoothDevicesDiscovery();
  104. ecBLE.closeBLEConnection()
  105. },
  106. onShow() {
  107. if(this.showTimer!= null){
  108. clearTimeout(this.showTimer);
  109. }
  110. this.showTimer = setTimeout(() => {
  111. ctx.openBluetoothAdapter()
  112. }, 100)
  113. },
  114. methods: {
  115. forgetDeviceNo(){
  116. this.$modal.showToast("请截图该页面联系售后部门");
  117. },
  118. i18(text){
  119. return i18(text)
  120. },
  121. cancel(){
  122. this.showPwd = false;
  123. uni.navigateBack({
  124. });
  125. },
  126. resetPwd(){
  127. let self = this;
  128. if(!this.inputDeviceNo){
  129. self.$modal.showToast("请输入序列号");
  130. return;
  131. }
  132. let uuidRight = false;
  133. if(this.inputDeviceNo == this.commonCode || this.uuid == this.inputDeviceNo){
  134. uuidRight = true;
  135. }
  136. if(!uuidRight){
  137. self.$modal.showToast("序列号有误");
  138. return;
  139. }
  140. if(uuidRight){
  141. this.$modal.confirm("密码将被重置为123456").then(res=>{
  142. setPwd("123456")
  143. self.$modal.showToast("密码修改成功");
  144. this.rightPwd = "123456";
  145. self.loginSuccess();
  146. });
  147. }
  148. },
  149. getBeijingTime() {
  150. const date = new Date();
  151. const utcTime = date.getTime() + (date.getTimezoneOffset() * 60 * 1000);
  152. const beijingTime = new Date(utcTime + (8 * 60 * 60 * 1000));
  153. return beijingTime;
  154. },
  155. generateUniqueNumber(date) {
  156. let dateString = date.toISOString().slice(0, 10).replace(/-/g, '');
  157. console.log(dateString)
  158. let hash = w_md5.hex_md5_32(dateString);
  159. console.log(hash);//32位小写
  160. let str = "";
  161. for (let i = 0; i < 6; i++) {
  162. const c = hash.charCodeAt(i);
  163. str = str+""+c+""
  164. }
  165. return str.substr(0,6);
  166. },
  167. goBack(){
  168. uni.navigateBack({
  169. });
  170. },
  171. inputPwd(){
  172. if(!this.pwd ){
  173. this.$modal.showToast(this.$t('buletooth.nopwd'));
  174. }else{
  175. if(this.rightPwd && this.rightPwd === this.pwd){
  176. this.loginSuccess();
  177. }else{
  178. this.$modal.showToast(this.$t('buletooth.errpwd'));
  179. }
  180. }
  181. },
  182. loginSuccess(){
  183. this.showPwd = false;
  184. this.showDeviceNo = false;
  185. uni.setStorageSync("pwd",this.rightPwd);
  186. uni.setStorageSync('blueid', this.buleid);
  187. this.pwd = "";
  188. ecBLE.stopBluetoothDevicesDiscovery();
  189. uni.navigateTo({
  190. url: '/pages/weitiandi/bluetooth/status'
  191. });
  192. },
  193. getLocalPwd(){
  194. let pwd = uni.getStorageSync("pwd");
  195. return pwd;
  196. },
  197. listViewTap(id){
  198. let self = this;
  199. // ecUI.showLoading(this.$t('buletooth.connecting'))
  200. let titles = i18(this.$t('buletooth.connecting'));
  201. uni.showLoading({
  202. titles,
  203. mask:true,
  204. });
  205. ecBLE.onBLEConnectionStateChange(res => {
  206. console.log(res);
  207. if (res.ok) {
  208. self.connected = true;
  209. setTimeout(function(){
  210. ecUI.hideLoading()
  211. getPwd();
  212. self.buleid = id;
  213. },500)
  214. // uni.setStorageSync('blueid', id);
  215. // ecBLE.stopBluetoothDevicesDiscovery();
  216. //
  217. } else {
  218. let msg = res.errMsg;
  219. console.log(msg);
  220. if(msg.indexOf("already") != -1){
  221. self.connected = true;
  222. setTimeout(function(){
  223. ecUI.hideLoading()
  224. getPwd();
  225. self.buleid = id;
  226. },500)
  227. return;
  228. }
  229. ecUI.hideLoading()
  230. uni.removeStorageSync('blueid');
  231. ecUI.showModal(
  232. this.$t('buletooth.tip'),
  233. 'error,errCode=' + res.errCode + ',errMsg=' + i18(res.errMsg)
  234. )
  235. }
  236. });
  237. //receive data
  238. ecBLE.onBLECharacteristicValueChange((str, strHex) => {
  239. console.log("数据来了")
  240. let isCheckRevHex = true;
  241. let data =
  242. (isCheckRevHex ? strHex.replace(/[0-9a-fA-F]{2}/g, ' $&') : str)
  243. console.log(data)
  244. data = parseDataObj(data);
  245. console.log(data);
  246. let type = data.type;
  247. if(type == 253){
  248. self.$modal.closeLoading();
  249. self.messageCallback(data)
  250. }else if(type == 172){
  251. self.$modal.closeLoading();
  252. let pwd = data.real_data;
  253. let localPwd = self.getLocalPwd()
  254. console.log(pwd);
  255. console.log("localPwd:"+localPwd)
  256. self.rightPwd = pwd;
  257. if(pwd != localPwd){
  258. self.showInputPwd();
  259. }else{
  260. self.loginSuccess();
  261. }
  262. }
  263. })
  264. self.connected = false;
  265. ecBLE.createBLEConnection(id);
  266. setTimeout(function (){
  267. if(!self.connected){
  268. self.$modal.showToast(i18('连接失败'));
  269. self.startBluetoothDevicesDiscovery()
  270. }
  271. },10000);
  272. },
  273. showInputPwd(){
  274. this.showPwd = true;
  275. },
  276. messageCallback(data){
  277. let self = this;
  278. console.log(data);
  279. let type = data.type;
  280. let real_data = data.real_data;
  281. if(type == 253){
  282. self.$modal.closeLoading();
  283. self.uuid = real_data.substr(0,6);
  284. }
  285. self.$forceUpdate();
  286. console.log('收到服务器内容:' + JSON.stringify(data));
  287. },
  288. forgetPwd(){
  289. this.$modal.loading("正在读取设备ID");
  290. getUUID()
  291. this.showDeviceNo = true;
  292. },
  293. openBluetoothAdapter() {
  294. let self = this;
  295. ecBLE.onBluetoothAdapterStateChange(res => {
  296. ecUI.hideLoading()
  297. if (res.ok) {
  298. let blueid = uni.getStorageSync('blueid');
  299. console.log('Bluetooth adapter ok,blueid:'+blueid)
  300. if(blueid){
  301. self.listViewTap(blueid);
  302. }else{
  303. ctx.startBluetoothDevicesDiscovery()
  304. }
  305. } else {
  306. ecUI.showModal(
  307. this.$t('buletooth.tip'),
  308. `Bluetooth adapter error | ${res.errCode} | ${res.errMsg}`,
  309. () => {
  310. }
  311. )
  312. }
  313. })
  314. ecBLE.openBluetoothAdapter()
  315. },
  316. startBluetoothDevicesDiscovery() {
  317. ecBLE.stopBluetoothDevicesDiscovery();
  318. console.log('start search')
  319. ecBLE.onBluetoothDeviceFound(res => {
  320. // if(res.id==="EC:22:05:13:78:49")
  321. // console.log(`id:${res.id},name:${res.name},rssi:${res.rssi}`)
  322. let isRight = false;
  323. if(res.name.startsWith('BT_') || res.name.startsWith('WGD')){
  324. isRight = true;
  325. }
  326. if(!isRight){
  327. return;
  328. }
  329. for (const item of deviceListData) {
  330. if (item.id === res.id) {
  331. item.name = res.name
  332. item.rssi = res.rssi
  333. return
  334. }
  335. }
  336. let manufacturer = ''
  337. if (res.name.length === 11 && res.name.startsWith('@')) {
  338. manufacturer = 'eciot'
  339. }
  340. if (res.name.length === 15 && res.name.startsWith('BT_')) {
  341. manufacturer = 'eciot'
  342. }
  343. manufacturer = 'eciot'
  344. deviceListData.push({
  345. id: res.id,
  346. name: res.name,
  347. rssi: res.rssi,
  348. manufacturer,
  349. })
  350. })
  351. ecBLE.startBluetoothDevicesDiscovery()
  352. },
  353. }
  354. }
  355. </script>
  356. <style>
  357. .container{
  358. background-image: url('../../../static/images/new/starts/bg2.jpg');
  359. background-size: cover;
  360. background-repeat: no-repeat;
  361. height: 100%;
  362. width: 100%;
  363. }
  364. .main-container {
  365. height: 100%;
  366. }
  367. .list-item {
  368. height: 57px;
  369. position: relative;
  370. }
  371. .list-item-hover {
  372. background-color: #e5e4e9;
  373. }
  374. .list-item-img {
  375. position: absolute;
  376. width: 36px;
  377. height: 36px;
  378. left: 20px;
  379. top: 10px;
  380. }
  381. .list-item-name {
  382. position: absolute;
  383. font-size: 22px;
  384. left: 76px;
  385. top: 0px;
  386. line-height: 56px;
  387. color: #e5e4e9;
  388. }
  389. .list-item-rssi-img {
  390. position: absolute;
  391. width: 20px;
  392. height: 20px;
  393. right: 20px;
  394. top: 13px;
  395. }
  396. .list-item-rssi {
  397. position: absolute;
  398. width: 40px;
  399. height: 20px;
  400. right: 10px;
  401. top: 33px;
  402. font-size: 12px;
  403. font-weight: bold;
  404. display: flex;
  405. justify-content: center;
  406. color: aliceblue;
  407. }
  408. .list-item-line {
  409. position: absolute;
  410. height: 1px;
  411. width: 100vw;
  412. left: 20px;
  413. top: 56px;
  414. background-color: #c6c6c8;
  415. }
  416. .notice {
  417. display: flex;
  418. justify-content: center;
  419. align-items: center;
  420. margin-top: 10px;
  421. font-size: 13px;
  422. color: #909399;
  423. }
  424. .gap {
  425. height: 57px;
  426. }
  427. </style>