index.vue 12 KB

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