1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150 |
- const constant = require('../../../utils/constant.js');
- import {
- BtHelper
- } from '../../../devices/bt_helper.js';
- import {
- EnumCmdEvent,
- CmdEvent
- } from '../../../devices/cmd_key_event';
- Page({
- data: {
- navBarHeight: constant.app.globalData.navBarHeight,
- safeBottomHeight: constant.app.globalData.safeBottomHeight,
- bannerList: constant.app.globalData.bannerList,
- nvabarData: {
- showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
- title: 'OhPlay', //导航栏 中间的标题
- },
- ///下午好
- isLogin: false,
- greeting: "",
- // bannerList: [{
- // "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
- // },
- // {
- // "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
- // }
- // ],
- autoplay: true,
- interval: 3000, // 切换时间间隔
- duration: 500, // 滑动动画时长
- circular: true, // 衔接滑动
- indexPage: 0,
- ///是否展示频道
- showChannel: false,
- ////首页
- navBarHeight: constant.app.globalData.navBarHeight,
- MenuButtonheight: constant.app.globalData.MenuButtonheight,
- MenuButtonTop: constant.app.globalData.MenuButtonTop,
- actionIndex: null,
- luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
- channelData: [],
- isOneLoading: true,
- uid: null,
- isSetWake: false,
- battery: 4, // 0≤电量<20,0格
- ////我的界面
- loginStatus: true,
- nickName: "未登录",
- userPic: './../../../img/head_pic.png',
- intervalId: null,
- /// 是否第一次自动连接过
- autoConnected: false,
- intervalId1: null,
- intervalId2: null,
- deviceMacId: null,
- deviceListSelect: null,
- deviceList: [],
- _willConnectBle: null,
- _hasShowModal: null,
- _clickOtherDevice: false,
- connectDeviceIding: "",
- },
- onLoad(options) {
- var that = this;
- that.onLoginLoad();
- that.onDeviceLoad();
- that.onUserInfoLoad();
- that.onBannerLoad();
- },
- onShow() {},
- onLoginLoad() {
- var that = this;
- var userInfo = wx.getStorageSync("userInfo") || "";
- if (!constant.strings.isEmpty(userInfo)) {
- var phone = userInfo.phone;
- if (!constant.strings.isEmpty(phone)) {
- that.loginReset(phone, null, null, null);
- };
- }
- },
- ///重新登录
- loginReset(phone, succeedCall1, succeedCall2, failCall) {
- // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
- var that = this;
- wx.login({
- success: res0 => {
- // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":933625,"deviceUid":1,"accessToken":"kaKdNAb9eO0iUGKsaLP18UE0wRwrvoCr4opjNC3AieaigH4Fu1IspQKi2zTPgLNG","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥","isNewUser":false}
- constant.apiUtil.login({
- code: res0.code,
- phone: phone,
- }).then((res1) => {
- constant.app.globalData.userInfo = res1;
- constant.store.setStore("userInfo", res1);
- that.setData({
- isLogin: true,
- });
- constant.store.setStore("token", JSON.stringify({
- id: res1.userId,
- token: res1.accessToken,
- }));
- that.onUserInfoLoad();
- if (succeedCall1 != null) {
- succeedCall1();
- }
- if (succeedCall2 != null) {
- succeedCall2();
- }
- });
- },
- fail(fail4) {
- if (failCall != null) {
- failCall();
- }
- },
- });
- },
- testData() {
- return {
- "deviceId": "xxxxx",
- "deviceName": "xxxxxx",
- "deviceType": "xxxx",
- "type": 1,
- "deviceIcon": "xxxx",
- "deviceMacId": "xxxx",
- "deviceBattery": 4,
- "state": "online",
- "connectType": 1,
- "clientType": "猫王妙播黑胶音箱(BLE)",
- "deviceConnectStatus": 1,
- "deviceConnectStatusText": "xxxx",
- }
- },
- onDeviceLoad() {
- var that = this;
- var deviceList = constant.store.getStore("deviceList");
- if (!constant.strings.isEmpty(deviceList)) {
- // deviceList = deviceList.concat(deviceList);
- that.updateDeviceList(deviceList, true, false);
- }
- // that.addBlueDevice(that.testData())
- let hasBle = that.data.deviceList.find(item => {
- return item.connectType == 1;
- })
- if (hasBle) {
- ///监听蓝牙设备
- BtHelper.getInstance().initBluetoothAdapter();
- BtHelper.getInstance().getBluetoothDevices();
- BtHelper.getInstance().startScan(null, null, function (res) {});
- var isFirst = true;
- console.log("开始监听蓝牙设备");
- that.startBleTimer(isFirst, true);
- that.addNotification()
- }
- },
- startBleTimer(isFirst, openTimer) {
- let that = this;
- let deviceList = that.getDeviceList()
- let findBle = deviceList.find(item => {
- return (item.connectType === 1 && item.state === "online")
- })
- let findWifi = deviceList.find(item => {
- return (item.connectType === 3 && item.state === "online")
- })
- if (!openTimer && (findBle || findWifi)) {
- // 强制开启监听 && (有蓝牙在线 || 有wifi在线)就return
- return;
- }
- ///再秒再对比一次
- that.stopIntervalId1();
- let count = 0;
- that.data.intervalId1 = setInterval(async function () {
- console.log("开始扫描蓝牙设备");
- that.compareList();
- // if (count > 4) {
- if (isFirst) {
- that.stopIntervalId1();
- var newIsFirst = false
- that.startBleTimer(newIsFirst, true);
- }
- // } else {
- // count++;
- // }
- isFirst = false;
- }, isFirst ? 3 * 1000 : 10 * 1000);
- },
- /// 对比蓝牙数据
- compareList() {
- let that = this;
- constant.lexinScan.compareList2(that.data.deviceList, function (device) {
- if (device) {
- console.log("找到蓝牙了");
- that.data._willConnectBle = device;
- getApp().getBluetoothStatus();
- }
- });
- // constant.lexinScan.compareList(function (list) {
- // console.log("对比蓝牙数据", list);
- // that.updateDeviceList(list, false, false);
- // }, function (item) {
- // that.addBlueDevice(item);
- // });
- },
- getBluetoothStatusCallck(v) {
- var that = this;
- if (!that.data._willConnectBle) {
- // true 走下面
- return;
- }
- if (v) {
- var device = that.data._willConnectBle;
- console.log("开始连接设备0", device);
- // if (device.state === 'online' || device.connectType != 1) {
- // return;
- // }
- BtHelper.getInstance().connect(device, function (isConnected, d) {
- console.log("自动连接成功", isConnected)
- if (isConnected) {
- device.state = 'online';
- that.addBlueDevice(device);
- that.stopIntervalId1();
- } else {
- device.state = 'offline';
- }
- });
- } else {
- // wx.showModal({
- // title: '温馨提示',
- // content: '请检查手机蓝牙是否打开',
- // showCancel: false,
- // success: function (res) { }
- // });
- }
- that.data._willConnectBle = null;
- },
- // 回调
- mqttCallback(type, option) {
- // console.log("gadsfadsfadsfa==999===" + type + "====" + JSON.stringify(option));
- var that = this;
- let payloads = null;
- try {
- if (option && option.payload) {
- payloads = JSON.parse(option.payload);
- };
- switch (type) {
- ///先走订阅
- case "connect":
- constant.lexinSubscribe.subscribeAllDevice();
- break;
- ///再走有在线
- case "message_onoffline":
- constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
- that.updateDeviceList(list, false, false);
- }, function () {
- var indexPage = that.data.indexPage;
- that.setData({
- actionIndex: null,
- deviceListSelect: null,
- indexPage: indexPage == 1 ? 0 : indexPage,
- });
- }, function () {
- var indexPage = that.data.indexPage;
- that.setData({
- actionIndex: null,
- deviceListSelect: null,
- indexPage: indexPage == 1 ? 0 : indexPage,
- });
- });
- break;
- case "message":
- if (payloads) {
- var payloadType = payloads.type;
- var other = payloads.other;
- ///获取频道位置
- if (payloadType === "get_position" && other) {
- constant.lexinMessage.getPosition(other, function (actionIndex) {
- that.setData({
- actionIndex: actionIndex,
- });
- });
- }
- // 获取歌曲信息
- else if (payloadType === "get_dev_info" && other) {
- that.setDeviceInfor(other);
- constant.lexinMessage.getDeviceInfo(payloads);
- }
- ///获取电量
- else if (payloadType === "battery" && other) {
- if (other.battery) {
- var battery = constant.lexinMessage.getBattery(other.battery);
- that.setData({
- battery: battery,
- });
- }
- }
- ///获取播放状态
- else if (payloadType === "play" || payloadType === "play_state") {
- constant.lexinMessage.getPlay();
- }
- }
- break;
- default:
- }
- } catch (e) {}
- },
- ///去连接设备数据
- actionDevice(device) {
- var that = this;
- // 取消订阅
- var deviceMacId = that.data.deviceMacId;
- if (!constant.strings.isEmpty(deviceMacId)) {
- var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
- constant.app.unsubscribe(pubResponse);
- };
- that.subscribeCurrDevice(device);
- },
- ///数据处理
- subscribeCurrDevice(device) {
- var that = this;
- var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
- if (!isOK) {
- constant.app.connect();
- setTimeout(() => {
- that.subscribeCurrDevice(device);
- }, 500);
- return;
- };
- var deviceId = device.deviceId;
- var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
- that.setData({
- deviceMacId: deviceMacId,
- });
- /// 订阅设备
- var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
- constant.app.subscribe(pubResponse);
- /// 获取设备信息
- const obj = {
- DstDeviceName: deviceMacId
- };
- constant.app.PubMsg({
- type: "get_dev_info",
- ...obj
- });
- },
- ///选择对应的频道
- onTapChannelIndex(e) {
- var that = this;
- var index = e.currentTarget.dataset.index;
- constant.lexinJump.toChannelIndex(index, function (i) {
- that.setData({
- actionIndex: i,
- });
- });
- },
- ///去频道详情
- onTapToChannel() {
- var that = this;
- var actionIndex = that.data.actionIndex;
- if (actionIndex == null) {
- return;
- }
- var channelData = that.getChannelData();
- if (channelData.length > actionIndex) {
- constant.store.setStore("channelDeta", channelData[actionIndex]);
- constant.routeUtil.jump(constant.routePath.channelDetail);
- }
- },
- ///选择对应的频道
- getchannelData(clientType) {
- var that = this;
- constant.apiUtil.listByDevice({
- clientType: clientType,
- unShowLoad: true,
- }).then((res) => {
- constant.store.setStore("channelData", res);
- that.setData({
- channelData: res,
- });
- // 接收设备当前播放状态
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
- return;
- };
- var deviceId = deviceList[deviceListSelect].deviceId;
- var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
- const obj = {
- DstDeviceName: deviceMacId
- }
- constant.app.PubMsg({
- type: "get_position",
- ...obj
- });
- })
- },
- onTapIndex(e) {
- var that = this;
- var index = e.currentTarget.dataset.index;
- var indexPage = that.data.indexPage;
- if (indexPage != index) {
- that.setData({
- indexPage: index,
- });
- }
- },
- onTapLogin() {
- var that = this;
- ///退出登录
- var isLogin = that.getIsLogin();
- if (isLogin) {
- that.logOut();
- return;
- }
- constant.routeUtil.jump(constant.routePath.login);
- },
- ///退出登录
- logOut() {
- var that = this;
- wx.removeStorageSync('userInfo');
- wx.removeStorageSync('token');
- var nickName = "未登录";
- var greeting = that.getGreetBuNickName(nickName);
- that.setData({
- isLogin: false,
- greeting: greeting,
- nickName: nickName,
- userPic: "./../../../img/head_pic.png",
- });
- var device = that.getCurrentDevice();
- if (device == null) {
- return;
- }
- ///有设备在线被选中,则让它不被选择
- if (device.connectType == 3) {
- var deviceMacId = that.data.deviceMacId;
- if (!constant.strings.isEmpty(deviceMacId)) {
- var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
- constant.app.unsubscribe(pubResponse);
- var indexPage = that.data.indexPage;
- that.setData({
- deviceMacId: null,
- actionIndex: null,
- deviceListSelect: null,
- indexPage: indexPage == 1 ? 0 : indexPage,
- });
- };
- } else {
- // console.log("退出登录,断开连接", deviceList);
- // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
- // var indexPage = that.data.indexPage;
- // that.setData({
- // deviceMacId: null,
- // actionIndex: null,
- // deviceListSelect: null,
- // indexPage: indexPage == 1 ? 0 : indexPage,
- // });
- }
- },
- ///点击item
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
- async onTapItem(e) {
- var that = this;
- var isLogin = that.getIsLogin();
- if (!isLogin) {
- constant.routeUtil.jump(constant.routePath.login);
- return;
- }
- var index = e.currentTarget.dataset.index;
- var device = e.currentTarget.dataset.item;
- var connectType = device.connectType;
- var deviceListSelect = that.getDeviceListSelect();
- // wifi只支持在线点击
- if (connectType == 3 && device.state === "online") {
- if (index === deviceListSelect) {
- that.goWake();
- } else {
- // 现断线第一个
- // that.disconnectFirst()
- that.addWifiDevice(device);
- }
- return;
- }
- ///去蓝牙连接处理
- // {"connectType":1,"deviceId":"E4:9F:80:09:40:EC","name":"E4:9F:80:09:40:EC","state":"online","ProdModel":"MW-SR1(4G_WIFI)","devName":"猫王音响x阿基米德 SR1 妙播收音机"}
- var deviceList = that.getDeviceList();
- let curDevice = deviceList[index]
- if (index === deviceListSelect && device.state === "online") {
- var param = "?param=" + JSON.stringify(curDevice);
- constant.routeUtil.jumpParam(constant.routePath.deviceDetail, param);
- } else if (device.state === "offline") {
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect != null) {
- if (deviceList.length > deviceListSelect) {
- var currentDevice = deviceList[deviceListSelect];
- console.log("断开连接0", currentDevice)
- // 现断线第一个
- // that.disconnectFirst()
- }
- that.setData({
- deviceListSelect: null,
- });
- }
- console.log("断开连接2", device)
- device.name = device.devName;
- that.data._clickOtherDevice = true;
- BtHelper.getInstance().connect(device, function (isConnected, d) {
- that.data._clickOtherDevice = false;
- if (isConnected) {
- device.state = "online";
- that.addBlueDevice(device);
- }
- });
- } else {
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect != null) {
- if (deviceList.length > deviceListSelect) {
- var currentDevice = deviceList[deviceListSelect];
- if (currentDevice.connectType === 1) {
- console.log("断开连接1", currentDevice)
- await BtHelper.getInstance().disconnect(currentDevice);
- }
- // await time_util.delayMills(500);
- }
- that.setData({
- deviceListSelect: null,
- });
- }
- that.data._clickOtherDevice = true;
- device.name = device.devName;
- BtHelper.getInstance().connect(device, function (isConnected, d) {
- that.data._clickOtherDevice = false;
- if (isConnected) {
- device.state = "online";
- that.addBlueDevice(device);
- }
- });
- }
- },
- disconnectFirst() {
- let that = this;
- let deviceList = that.getDeviceList()
- let first = deviceList[0] ?? {}
- if (first.state == "online") {
- if (first.connectType === 1) {
- BtHelper.getInstance().disconnect(first)
- first.state == "offline"
- } else {
- // 取消订阅
- // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
- //constant.app.unsubscribe(pubResponse);
- }
- }
- },
- // 去唤醒界面
- goWake() {
- var that = this;
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
- return
- };
- var device = deviceList[deviceListSelect];
- var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
- constant.routeUtil.jumpParam(constant.routePath.deviceWake, param);
- },
- ///删除当前设备
- deleteDevice(e) {
- var that = this;
- var index = e.currentTarget.dataset.index;
- wx.showModal({
- title: '确定删除?',
- success: function (res) {
- if (res.confirm) {
- var deviceList = that.getDeviceList();
- if (deviceList[index].connectType == 3) {
- that.cancelWifi(index, false);
- } else {
- console.log("确定删除0", index, deviceList[index].deviceId)
- that.cancelBlue(index, false);
- }
- }
- }
- });
- },
- ///是否是同一个蓝牙
- isTheSameBlue(device) {
- var that = this;
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
- return false;
- };
- if (deviceList[deviceListSelect].connectType == 3) {
- return false;
- } else {
- if (deviceList[deviceListSelect].deviceId == device.deviceId) {
- return true;
- } else {
- return false;
- }
- }
- },
- ///断开当前的
- cancelCurrent() {
- var that = this;
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceList[deviceListSelect].connectType == 3) {
- that.cancelWifi(deviceListSelect, true);
- } else {
- that.cancelBlue(deviceListSelect, true);
- }
- },
- /// 断开蓝牙连接
- cancelBlue(index, onlyCancel) {
- var that = this;
- if (onlyCancel) {
- return;
- }
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- ///是否删除的当前播放这个
- var isCurrentIndex = false;
- if (deviceListSelect != null && deviceList.length > deviceListSelect) {
- isCurrentIndex = (deviceListSelect == index);
- };
- let device = deviceList[index];
- ///删除当前设备
- var deviceId = device.deviceId;
- deviceList = deviceList.filter((item, i) => {
- return deviceId !== item.deviceId;
- });
- constant.store.setStore("deviceList", deviceList);
- console.log("确定删除1", isCurrentIndex, deviceListSelect)
- var indexPage = that.data.indexPage;
- that.setData({
- deviceList: deviceList,
- indexPage: indexPage == 1 ? 0 : indexPage,
- deviceListSelect: isCurrentIndex ? null : deviceListSelect,
- });
- console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
- if (device.state === "online") {
- BtHelper.getInstance().disconnect({
- "deviceId": deviceId
- });
- }
- },
- /// 断开连接wifi
- cancelWifi(index, onlyCancel) {
- var that = this;
- var deviceList = that.getDeviceList();
- if (index === null || deviceList.length <= index) {
- return;
- };
- var deviceId = deviceList[index].deviceId;
- // 取消订阅
- var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
- constant.app.unsubscribe(pubResponse);
- if (onlyCancel) {
- return;
- }
- ///是否删除的当前播放这个
- var isCurrentIndex = false;
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect != null && deviceList.length > deviceListSelect) {
- isCurrentIndex = (deviceListSelect == index);
- };
- deviceList = deviceList.filter((item, i) => {
- return deviceId !== item.deviceId;
- });
- constant.store.setStore("deviceList", deviceList);
- var indexPage = that.data.indexPage;
- var deviceMacId = that.data.deviceMacId;
- var actionIndex = that.data.actionIndex;
- that.setData({
- deviceList: deviceList,
- deviceMacId: isCurrentIndex ? null : deviceMacId,
- actionIndex: isCurrentIndex ? null : actionIndex,
- deviceListSelect: isCurrentIndex ? null : deviceListSelect,
- indexPage: indexPage == 1 ? 0 : indexPage,
- });
- },
- /// 新添加蓝牙设备
- addBlueDevice(device) {
- var that = this;
- var addDeviceList = constant.lexinAdd.addBlueDevice(device);
- let hasOnline = 0
- addDeviceList.forEach(item => {
- // 蓝牙设备下线。wifi不动
- if (item.deviceId != device.deviceId && item.connectType === 1) {
- item.state = "offline"
- }
- })
- console.log("新添加蓝牙设备", addDeviceList)
- that.updateDeviceList(addDeviceList, false, false);
- that.setData({
- deviceListSelect: 0,
- autoConnected: true,
- });
- that.addNotification()
- },
- disconnectDev(event, device) {
- let _this = this;
- if (_this.data._clickOtherDevice) {
- _this.data._clickOtherDevice = false
- return;
- }
- console.log("断开连接--", event.commonValue, event.deviceId);
- if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
- _this.data._hasShowModal = true
- wx.showModal({
- title: '提示',
- content: '设备断开连接,请重新连接设备',
- showCancel: false,
- success: function (res) {
- _this.data._hasShowModal = false;
- if (res.confirm) {
- if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
- getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
- }
- }
- }
- })
- }
- // 设备断开连接,请重新连接设备
- },
- addNotification() {
- let that = this;
- constant.eventBus.removeNotification(CmdEvent.eventName, that);
- constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
- let deviceList = that.getDeviceList()
- // console.log("收到监听:", JSON.stringify(event))
- if (event.cmdEvent == EnumCmdEvent.btMac) {
- let disDevice = deviceList.find(item => {
- return item.state == "online" && item.connectType === 1
- })
- if (disDevice) {
- disDevice.mac = event.btMac
- console.log("设备mac:", disDevice.mac)
- }
- } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
- let isFirst = false;
- // 有下线就开启监听扫描
- if (event.commonValue == "online") {
- console.log("有蓝牙设备上线")
- that.stopIntervalId1();
- } else {
- // 有wifi设备在线
- let wifiDevice = deviceList.find(item => {
- return item.state == "online" && item.connectType === 3
- })
- if (!wifiDevice) {
- console.log("开启蓝牙监听:", event.deviceId, event.commonValue)
- that.startBleTimer(isFirst, true);
- }
- }
- console.log("首页ble通知:", event.deviceId, event.commonValue)
- let hasOnleBle = deviceList.find(item => {
- return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
- })
- let disDevice = deviceList.find(item => {
- return item.deviceId == event.deviceId && item.connectType == 1
- })
- // that.data.deviceList.forEach(element => {
- // if (item.deviceId == tmp.deviceId) {
- // }
- // });
- if (!disDevice) {
- // console.log("")
- console.log("没有找到在线的对应的蓝牙设备", JSON.stringify(deviceList))
- return;
- }
- disDevice.state = event.commonValue;
- console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
- if (event.commonValue == "online") {
- // BtHelper.getInstance().connect()
- console.log("首页连接上线", disDevice)
- } else {
- if (hasOnleBle) {
- that.disconnectDev(event, disDevice)
- }
- console.log("首页连接断开", disDevice)
- that.updateDeviceList(deviceList, false, false);
- }
- }
- }, that);
- },
- // 新添加Wifi设备
- addWifiDevice(device) {
- var that = this;
- var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
- if (!isOK) {
- constant.app.connect();
- setTimeout(() => {
- that.addWifiDevice(device);
- }, 500);
- return;
- };
- console.log("有新的wifi设备0", device)
- var deviceId = device.deviceId;
- var addDeviceList = constant.lexinAdd.addWifiDevice(device);
- console.log("有新的wifi设备", addDeviceList)
- that.updateDeviceList(addDeviceList, false, false);
- var indexPage = that.data.indexPage;
- that.setData({
- deviceListSelect: null,
- connectDeviceIding: deviceId,
- indexPage: indexPage == 1 ? 0 : indexPage,
- });
- constant.lexinSubscribe.subscribeSingleDevice(device);
- },
- refreshCurrentDevice(device) {
- var that = this;
- console.log("刷新列表0:", device)
- var addDeviceList = constant.lexinAdd.addWifiDevice(device);
- console.log("刷新列表1:", addDeviceList)
- that.updateDeviceList(addDeviceList, false, false);
- that.setData({
- autoConnected: true,
- deviceListSelect: 0,
- connectDeviceIding: "",
- });
- },
- /// 手机关闭蓝牙,所有蓝牙设备离线
- closeBlueResetOffline(isInit, closeAllBlue) {
- var that = this;
- var deviceList = that.getDeviceList();
- that.updateDeviceList(deviceList, isInit, closeAllBlue);
- },
- /// 更新列表排序
- updateDeviceList(deviceList, isInit, closeAllBlue) {
- var that = this;
- var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
- constant.store.setStore("deviceList", finalList);
- that.setData({
- deviceList: finalList,
- });
- },
- ///点击banner事件
- onTapBanner(e) {
- var item = e.currentTarget.dataset.item;
- try {
- if (constant.strings.isEmpty(item.forwardUrl)) {
- return;
- }
- // 内链
- if (item.forwardType == 0) {
- var param = "?param=" + item.forwardUrl;
- constant.app.webSkipUrl = item.forwardUrl;
- constant.routeUtil.jumpParam(constant.routePath.webview, param);
- }
- // 外链
- else if (item.forwardType == 1) {
- } else {
- }
- } catch (err) {
- console.log("跳转失败", err)
- }
- },
- ///去充值内容会员
- toRecharge(e) {
- var device = e.currentTarget.dataset.item;
- },
- ///添加设备
- jumpToAddDevice() {
- var that = this;
- var isLogin = that.getIsLogin();
- if (!isLogin) {
- var param = "?toDeviceList=" + true;
- constant.routeUtil.jumpParam(constant.routePath.login, param);
- return;
- }
- constant.routeUtil.jump(constant.routePath.deviceList);
- },
- // 关于我们
- jumpToAboutUs() {
- constant.lexinJump.toAboutUs();
- },
- /// 是否已登录
- getIsLogin() {
- var that = this;
- var isLogin = that.data.isLogin;
- return isLogin;
- },
- /// 获取列表数据
- getDeviceList() {
- var that = this;
- var deviceList = that.data.deviceList;
- return deviceList;
- },
- ///获取当前选中的
- getDeviceListSelect() {
- var that = this;
- var deviceListSelect = that.data.deviceListSelect;
- return deviceListSelect;
- },
- ///获取是否已经第一次连接过
- getAutoConnected() {
- var that = this;
- var autoConnected = that.data.autoConnected;
- return autoConnected;
- },
- ///当前正在连接的设备id
- getConnectDeviceIding() {
- var that = this;
- var connectDeviceIding = that.data.connectDeviceIding
- return connectDeviceIding;
- },
- ///获取频道数据
- getChannelData() {
- var that = this;
- var channelData = that.data.channelData;
- return channelData;
- },
- /// 当前播放频道
- getActionIndex() {
- var that = this;
- var actionIndex = that.data.actionIndex;
- return actionIndex;
- },
- /// 获取当前的device
- getCurrentDevice() {
- var that = this;
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
- return null;
- };
- var device = deviceList[deviceListSelect];
- return device;
- },
- /// 获取当前的deviceId
- getCurrentDeviceId() {
- var that = this;
- var deviceList = that.getDeviceList();
- var deviceListSelect = that.getDeviceListSelect();
- if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
- return "";
- };
- var deviceId = deviceList[deviceListSelect].deviceId;
- return deviceId;
- },
- ///设置设备相关信息
- setDeviceInfor(other) {
- var that = this;
- // 接收设备当前信息
- that.getchannelData(other.ProdModel);
- // 电量
- if (other.Power) {
- var battery = constant.lexinMessage.getBattery(other.Power);
- that.setData({
- battery: battery,
- });
- }
- // 当前设备木有设置定时
- that.setData({
- isSetWake: false,
- });
- other.alarm.map((v) => {
- if (v.enable === "1") {
- that.setData({
- isSetWake: true
- });
- }
- });
- },
- ///******************************* 可折叠 ********************************************///
- onUserInfoLoad() {
- var that = this;
- var userInfo = wx.getStorageSync("userInfo") || "";
- if (!constant.strings.isEmpty(userInfo)) {
- var phone = userInfo.phone;
- if (!phone) {
- return;
- };
- var nickName = userInfo.nickname || "";
- var greeting = that.getGreetBuNickName(nickName);
- // 获取缓存的频道数据
- var channelData = wx.getStorageSync("channelData") || "";
- if (!constant.strings.isEmpty(channelData)) {
- that.setData({
- channelData: channelData,
- greeting: greeting,
- nickName: nickName,
- userPic: userInfo.headUrl || "",
- isLogin: true,
- })
- } else {
- that.setData({
- greeting: greeting,
- nickName: nickName,
- userPic: userInfo.headUrl || "",
- isLogin: true,
- });
- }
- } else {
- var greeting = that.getGreetBuNickName("");
- that.setData({
- greeting: greeting,
- })
- }
- },
- getGreetBuNickName(nickName) {
- var greeting = constant.timeUtil.getGreet();
- if (nickName != "" && nickName != "未登录") {
- greeting = greeting + ',' + nickName;;
- }
- return greeting;
- },
- onBannerLoad() {
- var that = this;
- constant.apiUtil.getBanner({}).then((res) => {
- that.setData({
- bannerList: res,
- });
- constant.store.setStore("homeBanner", res);
- });
- },
- stopIntervalId1: function () {
- var that = this;
- if (!constant.strings.isEmpty(that.data.intervalId1)) {
- clearInterval(that.data.intervalId1);
- that.data.intervalId1 = null;
- }
- },
- stopIntervalId2: function () {
- var that = this;
- if (!constant.strings.isEmpty(that.data.intervalId2)) {
- clearInterval(that.data.intervalId2);
- that.data.intervalId2 = null;
- }
- },
- ///销毁蓝牙
- onUnload() {
- var that = this;
- constant.eventBus.removeNotification(CmdEvent.eventName, that)
- console.log("界面onUnload")
- that.stopIntervalId1();
- that.stopIntervalId2();
- if (!constant.strings.isEmpty(that.data.intervalId)) {
- clearInterval(that.data.intervalId);
- that.data.intervalId = null;
- }
- BtHelper.getInstance().disconnect();
- if (constant.app.globalData.client === null) {
- return;
- };
- constant.app.globalData.client.end(true);
- constant.app.globalData.client = null;
- },
- })
|