index.vue 11 KB

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