// pages/home/home.js const app = getApp(); const { login } = require('./../../utils/api.js'); const { selectIndex, eventOn } = require('./../../custom-tab-bar/tabbar.js'); Page({ /** * 页面的初始数据 */ data: { navBarHeight: app.globalData.navBarHeight, MenuButtonheight: app.globalData.MenuButtonheight, MenuButtonTop: app.globalData.MenuButtonTop, rightId: wx.getStorageSync('rightId') || 0, nvabarData: { nvabarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: 'Oh Play', //导航栏 中间的标题 callback() { wx.navigateBack({ delta: 1, }) } }, isNotruter: false, }, deviceList: [], bannerList: ["https://img1.baidu.com/it/u=2003442481,2528062378&fm=253&fmt=auto&app=138&f=JPG?w=499&h=182", "https://hbimg.huabanimg.com/0ff93215a6d8d1c5f80117fdbfa24936dbd9097026ad99-CNduY8_fw658", "https://img.zcool.cn/community/0177ed5f365bdaa801215aa0a0c1c3.jpg@1280w_1l_2o_100sh.jpg"], selectIndex: 0, isLoginWx: true, }, changeRole() { app.changeUserRight() }, // 删除设备 deleteDeviceTap: function () { }, // 设备详情页 devieDetailPageTap: function () { }, deviceItemTap: function () { this.setData({ // selectIndex = index }); }, changeDeviceTap: function () { }, login() { var _this = this; 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({ isLoginWx: true, }); return; }; // 获取设备本地数据 this.getDeviceData(); _this.setData({ isLoginWx: false, }); wx.setStorage({ key: "token", data: JSON.stringify({ id: res.userId, token: res.accessToken, }), }); }); }, }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { var _this = this; eventOn(_this); // 版本自动更新代码 const updateManager = wx.getUpdateManager(); updateManager.onUpdateReady(function () { _this.setData({ newVersion: true }); }) updateManager.onUpdateFailed(function () { // 新的版本下载失败 wx.showModal({ title: '已有新版本咯', content: '请您删除当前小程序,重新打开呦~', showCancel: false }) }) wx.showLoading({ title: '加载中', }); // 登录 _this.login(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow(options) { selectIndex(0); if (this.data.isLoginWx) { var key = 'home_device_list' + wx.getUserInfo().userId; var saveStr = wx.getStorageSync(key) || "[]"; var devList = JSON.parse(saveStr); this.setData({ deviceList: devList }); } }, addDeviceTap() { wx.navigateTo({ url: './../bluConnect/bleconnect', events: { getBackData: res => { var key = 'home_device_list' + wx.getUserInfo().userId; var saveStr = wx.getStorageSync(key) || "[]"; var oldList = JSON.parse(saveStr); var findElement; for (let index = 0; index < oldList.length; index++) { const element = oldList[index]; if (element.address == res.address) { findElement = element; break; } } if (findElement) { oldList.pop(findElement); } oldList.push(res); wx.setStorageSync(key, JSON.stringify(oldList)); this.setData({ deviceList: oldList }); } } }); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })