123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- // 获取应用实例
- const app = getApp();
- const {
- login,
- listByDevice
- } = require('../../utils/api.js');
- const {
- isCN
- } = require('../../utils/util.js');
- let _this = null;
- const interval = null;
- // import routeUtil from '../../utils/route_util.js'
- import routeUtil from '../../utils/route_util'
- import route_constant from '../../utils/route_constant.js'
- Page({
- data: {
- indexPage: 0,
- showChannel: false,
- ////首页
- navBarHeight: app.globalData.navBarHeight,
- MenuButtonheight: app.globalData.MenuButtonheight,
- MenuButtonTop: app.globalData.MenuButtonTop,
- actionIndex: null,
- isShowDevicelist: false,
- luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
- channelData: [],
- deviceList: [],
- deviceListIndex: null,
- showLogin: false,
- isOneLoading: true,
- uid: null,
- showDelete: false,
- isSetWake: false,
- thisDeviceMac: null,
- battery: 4, // 0≤电量<20,0格
- newVersion: false,
- ////我的界面
- loginStatus: true,
- userName: "未登录",
- userPic: './../../img/head_pic.png',
- nvabarData: {
- showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
- title: '', //导航栏 中间的标题
- },
- islogin: false,
- },
- onLoad(options) {
- var that = this;
- that.onHomeLoad();
- that.onMeLoad();
- },
- onShow() {
- var that = this;
- that.onHomeShow();
- that.onMeShow();
- },
- onHomeLoad() {
- _this = this;
- // 版本自动更新代码
- const updateManager = wx.getUpdateManager();
- updateManager.onUpdateReady(function () {
- _this.setData({
- newVersion: true
- });
- })
- updateManager.onUpdateFailed(function () {
- // 新的版本下载失败
- wx.showModal({
- title: '已有新版本咯',
- content: '请您删除当前小程序,重新打开呦~',
- showCancel: false
- })
- })
- wx.showLoading({
- title: '加载中',
- });
- const str = setTimeout(() => {
- clearTimeout(str);
- if (_this.data.isOneLoading) {
- _this.setData({
- isOneLoading: false,
- });
- }
- }, 500);
- // 获取缓存的频道数据
- wx.getStorage({
- key: "channelData",
- success(res) {
- _this.setData({
- channelData: res.data
- })
- }
- });
- // 登录
- this.homeLogin();
- },
- onMeLoad() {
- // wx.getStorage("userInfo")
- const _this = this;
- wx.getStorage({
- key: "userInfo",
- success(res) {
- if (!res.data.phone) {
- return;
- };
- _this.setData({
- userName: res.data.nickname || "",
- userPic: res.data.headUrl || "",
- islogin: true,
- })
- }
- })
- },
- onHomeShow() {
- if ((!this.data.isOneLoading)) {
- wx.getStorage({
- key: 'userInfo',
- fail(res) {
- // 取消订阅
- if (_this.data.thisDeviceMac !== null) {
- app.unsubscribe(`/${_this.data.thisDeviceMac}/user/pub_response`);
- };
- _this.setData({
- showLogin: true,
- });
- },
- success() {
- if (app.globalData.newDeviceId) {
- // 获取设备本地数据
- _this.getHomeDeviceData();
- } else if (_this.data.deviceListIndex !== null) {
- // 更新
- _this.actionDevice(_this.data.deviceListIndex);
- }
- }
- });
- };
- },
- onMeShow() {
- this.onLoad();
- },
- homeLogin() {
- wx.login({
- success: res => {
- let phone = undefined;
- try {
- var value = wx.getStorageSync('userInfo')
- if (value) {
- phone = value.phone || undefined;
- };
- } catch (e) {
- // Do something when catch error
- };
- // 请求登录
- login({
- code: res.code,
- phone
- }).then((res) => {
- app.globalData.userInfo = res;
- wx.setStorage({
- key: "userInfo",
- data: res
- });
- // 需要用授权登录
- if ((res.isNewUser && res.isNewUser === true) || !phone) {
- _this.setData({
- showLogin: true,
- });
- return;
- };
- // 获取设备本地数据
- this.getHomeDeviceData();
- _this.setData({
- showLogin: false,
- });
- wx.setStorage({
- key: "token",
- data: JSON.stringify({
- id: res.userId,
- token: res.accessToken,
- }),
- });
- });
- },
- })
- },
- getHomeDeviceData() {
- // 获取设备数据
- wx.getStorage({
- key: 'devicelist',
- success(res) {
- wx.hideLoading();
- if (res.data) {
- const resData = JSON.parse(res.data);
- _this.setData({
- deviceList: resData
- });
- // 连接mqtt
- if (app.globalData.client === null) {
- app.connect();
- } else if (app.globalData.newDeviceId) {
- _this.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- // 监听
- let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
- app.subscribe(topic);
- const Timeout = setTimeout(() => {
- clearTimeout(Timeout);
- _this.actionDevice(0);
- }, 1000);
- }
- // // 有新设备
- // if(app.globalData.newDeviceId) {
- // _this.setData({
- // actionIndex: null,
- // deviceListIndex: null,
- // });
- // let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
- // app.subscribe(topic);
- // }
- }
- },
- fail(e) {
- wx.hideLoading();
- }
- });
- },
- // 回调
- mqttCallback(type, option) {
- let payloads = null;
- if (option) {
- payloads = JSON.parse(option.payload);
- };
- switch (type) {
- case "connect":
- _this.connectSuccess();
- break;
- case "message_onoffline":
- _this.online(payloads);
- break;
- case "message":
- // 接收设备播放信息
- if (payloads.type === "get_position" && payloads.other) {
- let actionIndex = null;
- _this.data.channelData.map((v, index) => {
- if (v.channelNum === payloads.other.channel) {
- actionIndex = index;
- }
- });
- _this.setData({
- actionIndex,
- });
- } else if (payloads.type === "play" || payloads.type === "play_state") {
- if (_this.data.deviceListIndex === null) {
- return;
- }
- // 接收设备当前播放状态
- const obj = {
- DstDeviceName: _this.getThisDeviceID()
- }
- app.PubMsg({
- type: "get_position",
- ...obj
- });
- } else if (payloads.type === "get_dev_info") {
- // 接收设备当前信息
- this.getchannelData(payloads.other.ProdModel);
- // 电量
- _this.setData({
- battery: _this._battery(payloads.other.Power),
- })
- // 当前设备木有设置定时
- _this.setData({
- isSetWake: false,
- });
- payloads.other.alarm.map((v) => {
- if (v.enable === "1") {
- _this.setData({
- isSetWake: true
- });
- }
- });
- // 更新信息
- _this.data.deviceList.map((v, index) => {
- if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
- _this.data.deviceList[index].ProdModel = payloads.other.ProdModel;
- _this.data.deviceList[index].devName = payloads.other.devName;
- }
- });
- // 更新缓存
- wx.setStorage({
- key: "devicelist",
- data: JSON.stringify(_this.data.deviceList),
- success() {
- _this.setData({
- deviceList: _this.data.deviceList
- })
- }
- });
- } else if (payloads.type === "battery" && payloads.other) {
- _this.setData({
- battery: _this._battery(payloads.other.battery),
- })
- }
- break;
- default:
- }
- },
- // 格式化电量
- _battery(battery) {
- let _battery = 0;
- if (battery < 20) {
- _battery = 0
- } else if (20 <= battery && battery < 40) {
- _battery = 1
- } else if (40 <= battery && battery < 60) {
- _battery = 2
- } else if (60 <= battery && battery < 80) {
- _battery = 3
- } else if (80 <= battery && battery <= 100) {
- _battery = 4
- } else if (battery > 100) {
- _battery = 5
- };
- return _battery
- },
- connectSuccess() {
- // 订阅设备在线信息
- _this.subscribeDevicesStatus();
- },
- online(payloads) {
- // 设置在线状态
- _this.data.deviceList.map((v, index) => {
- if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
- _this.data.deviceList[index].state = payloads.state;
- }
- });
- _this.setData({
- deviceList: _this.data.deviceList,
- });
- // 更新缓存
- wx.setStorage({
- key: "devicelist",
- data: JSON.stringify(_this.data.deviceList)
- });
- // 如没有选中,选中最新的
- (() => {
- if (_this.data.deviceListIndex === null) {
- let itue = false;
- _this.data.deviceList.map((v, index) => {
- if (v.state === "online" && !itue) {
- itue = true;
- _this.actionDevice(index);
- }
- });
- }
- // else {
- // _this.actionDevice(_this.data.deviceListIndex);
- // };
- })();
- // 当前播放设备离线
- if (_this.data.deviceListIndex !== null && _this.data.deviceList[_this.data.deviceListIndex].state !== "online") {
- _this.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- };
- },
- // 订阅设备在线状态
- subscribeDevicesStatus() {
- this.data.deviceList.forEach((value) => {
- let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
- app.subscribe(topic);
- })
- },
- subscribeCurrDevice() {
- if (!(app.globalData.client && app.globalData.client.connected)) {
- console.log("未连接MQTT服务器");
- const str = setInterval(() => {
- clearInterval(str);
- _this.subscribeCurrDevice();
- }, 500);
- return;
- };
- if (this.data.deviceList.length === 0 || this.data.deviceListIndex === null) {
- return
- };
- let topic = `/AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
- app.subscribe(topic);
- const obj = {
- DstDeviceName: _this.getThisDeviceID()
- };
- app.PubMsg({
- type: "get_dev_info",
- ...obj
- });
- },
- isShowDevicelistFun() {
- this.setData({
- isShowDevicelist: !_this.data.isShowDevicelist
- })
- },
- actionMusic(e) {
- if (e.currentTarget.dataset.index === this.data.actionIndex) {
- return;
- };
- if (this.data.deviceListIndex === null) {
- wx.showToast({
- title: '请选择设备',
- icon: "none"
- })
- return;
- };
- this.setData({
- actionIndex: e.currentTarget.dataset.index
- });
- const other = {
- "url": "",
- "media_data": "",
- "user_id": `${app.globalData.userInfo.deviceUid}`,
- "timestamp": `${Math.round(new Date() / 1000)}`,
- "channel_id": `${this.data.channelData[e.currentTarget.dataset.index].channelNum}`,
- "order": "",
- "resource_from": "",
- "songAlbumID": "",
- "version": 3,
- "is_debug": app.globalData.is_debug
- };
- app.PubMsg({
- type: "play",
- DstDeviceName: _this.getThisDeviceID(),
- other
- });
- },
- actionDeviceIndex(e) {
- if (e.currentTarget.dataset.index === this.data.deviceListIndex) {
- return
- };
- this.actionDevice(e.currentTarget.dataset.index)
- },
- actionDevice(index) {
- const device = this.data.deviceList[index];
- if (device.state !== "online") {
- return;
- };
- // 取消订阅
- if (this.data.thisDeviceMac !== null) {
- app.unsubscribe(`/${this.data.thisDeviceMac}/user/pub_response`);
- };
- this.setData({
- deviceListIndex: index,
- thisDeviceMac: `AIrSMArT_${this.data.deviceList[index].name.split("BLUFI_")[1]}`
- });
- // app.PubMsg({
- // type: "get_dev_info",
- // DstDeviceName: _this.getThisDeviceID()
- // });
- this.subscribeCurrDevice();
- },
- getThisDeviceID() {
- return `AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}`
- },
- goLogin() {
- wx.navigateTo({
- url: './../login/login',
- });
- },
- goDeviceConnect() {
- routeUtil.jump(route_constant.deviceList)
- return;
- wx.navigateTo({
- url: './../deviceConnect0/deviceConnect0',
- });
- this.setData({
- isShowDevicelist: false
- })
- },
- getchannelData(clientType) {
- let _this = this;
- if (this.data.deviceListIndex === null) {
- return;
- };
- listByDevice({
- clientType
- }).then((res) => {
- _this.setData({
- channelData: res
- });
- // 接收设备当前播放状态
- const obj = {
- DstDeviceName: _this.getThisDeviceID()
- }
- app.PubMsg({
- type: "get_position",
- ...obj
- });
- wx.setStorage({
- key: "channelData",
- data: res
- });
- // 有新设备
- if (app.globalData.newDeviceId) {
- app.globalData.newDeviceId = null;
- return;
- // 去掉此功能,先留着吧
- // const other= {
- // "url": "",
- // "media_data": "",
- // "user_id": `${app.globalData.userInfo.deviceUid}`,
- // "timestamp": `${Math.round(new Date() / 1000)}`,
- // "channel_id": `${res[1].channelNum}`,
- // "order": "",
- // "resource_from": "",
- // "songAlbumID":"",
- // "version":3,
- // "is_debug": app.globalData.is_debug
- // };
- // app.PubMsg({
- // type: "play",
- // DstDeviceName: _this.getThisDeviceID(),
- // other
- // });
- }
- })
- },
- showDelete() {
- // wx.navigateTo({
- // url: './../deviceRoter3/deviceRoter3',
- // })
- this.setData({
- showDelete: !this.data.showDelete
- });
- },
- deleteDevice(e) {
- const id = this.data.deviceList[e.currentTarget.dataset.index].deviceId;
- let name = _this.data.deviceListIndex !== null ? this.data.deviceList[_this.data.deviceListIndex].name : null;
- const deviceList = this.data.deviceList.filter((v, i) => {
- return id !== v.deviceId
- });
- // 取消订阅
- app.unsubscribe(`/AIrSMArT_${_this.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
- wx.setStorage({
- key: "devicelist",
- data: JSON.stringify(deviceList),
- success() {
- _this.setData({
- deviceList,
- thisDeviceMac: null
- });
- // 当前没有设备
- if (deviceList.length === 0) {
- _this.setData({
- showDelete: false,
- actionIndex: null,
- deviceListIndex: null,
- isShowDevicelist: false,
- });
- }
- if (_this.data.deviceListIndex === null) {
- return
- };
- if (e.currentTarget.dataset.index === _this.data.deviceListIndex) {
- let index_ = null;
- deviceList.map((v, index) => {
- if (v.state === "online" && index_ === null) {
- index_ = index;
- }
- });
- if (index_ !== null) {
- _this.actionDevice(index_);
- } else {
- _this.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- };
- } else {
- deviceList.map((v, index) => {
- if (v.name === name) {
- _this.setData({
- deviceListIndex: index,
- });
- }
- });
- }
- }
- })
- },
- goWake() {
- if (this.data.deviceListIndex === null) {
- return;
- };
- wx.navigateTo({
- url: './../deviceWake/deviceWake?deviceId=' + this.data.deviceList[this.data.deviceListIndex].deviceId + "&clientType=" + this.data.deviceList[this.data.deviceListIndex].ProdModel,
- });
- },
- goChnnel() {
- wx.setStorage({
- key: "channelDeta",
- data: this.data.channelData[this.data.actionIndex],
- success() {
- wx.navigateTo({
- url: './../channelDetails/channelDetails'
- })
- }
- })
- },
- updata() {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- wx.getUpdateManager().applyUpdate()
- },
- updataClone() {
- this.setData({
- newVersion: false
- })
- },
- onUnload() {
- if (app.globalData.client === null) {
- return;
- };
- app.globalData.client.end(true);
- app.globalData.client.end(true);
- app.globalData.client = null;
- },
- onTapIndex(e) {
- var that = this;
- var index = e.currentTarget.dataset.index;
- var indexPage = that.data.indexPage;
- if (indexPage != index) {
- that.setData({
- indexPage: index,
- });
- }
- },
- goMeAbout() {
- wx.navigateTo({
- url: './../about/about',
- })
- },
- goMeLogin() {
- const _this = this;
- if (this.data.islogin) {
- // 退出登录
- wx.removeStorage({
- key: 'userInfo',
- success(res) {
- _this.setData({
- islogin: false,
- userName: "未登录",
- userPic: "./../../img/head_pic.png",
- });
- wx.switchTab({
- url: `./../index/index`
- });
- }
- });
- wx.removeStorage({
- key: 'token',
- })
- } else {
- // 登录
- wx.navigateTo({
- url: './../login/login',
- });
- }
- },
- })
|