home.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // pages/home/home.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. navBarHeight: app.globalData.navBarHeight,
  9. MenuButtonheight: app.globalData.MenuButtonheight,
  10. MenuButtonTop: app.globalData.MenuButtonTop,
  11. rightId:wx.getStorageSync('rightId') || 0,
  12. nvabarData: {
  13. nvabarData: {
  14. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  15. title: 'Oh Play', //导航栏 中间的标题
  16. callback() {
  17. wx.navigateBack({
  18. delta: 1,
  19. })
  20. }
  21. },
  22. isNotruter: false,
  23. deviceList: [],
  24. bannerList: [],
  25. showLogin: false,
  26. selectIndex: -1,
  27. }
  28. },
  29. changeRole() {
  30. app.changeUserRight()
  31. },
  32. // 删除设备
  33. deleteDeviceTap: function () {
  34. },
  35. // 设备详情页
  36. devieDetailPageTap: function () {
  37. },
  38. deviceItemTap: function () {
  39. this.setData({
  40. // selectIndex = index
  41. });
  42. },
  43. changeDeviceTap: function () {
  44. },
  45. login() {
  46. wx.login({
  47. success: res => {
  48. let phone = undefined;
  49. try {
  50. var value = wx.getStorageSync('userInfo')
  51. if (value) {
  52. phone = value.phone || undefined;
  53. };
  54. } catch (e) {
  55. // Do something when catch error
  56. };
  57. // 请求登录
  58. login({code: res.code, phone}).then((res)=> {
  59. app.globalData.userInfo = res;
  60. wx.setStorage({
  61. key: "userInfo",
  62. data: res
  63. });
  64. // 需要用授权登录
  65. if((res.isNewUser && res.isNewUser === true) || !phone) {
  66. _this.setData({
  67. showLogin: true,
  68. });
  69. return;
  70. };
  71. // 获取设备本地数据
  72. this.getDeviceData();
  73. _this.setData({
  74. showLogin: false,
  75. });
  76. wx.setStorage({
  77. key:"token",
  78. data: JSON.stringify({
  79. id: res.userId,
  80. token: res.accessToken,
  81. }),
  82. });
  83. });
  84. },
  85. })
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. onLoad(options) {
  91. _this = this;
  92. // 版本自动更新代码
  93. const updateManager = wx.getUpdateManager();
  94. updateManager.onUpdateReady(function () {
  95. _this.setData({
  96. newVersion: true
  97. });
  98. })
  99. updateManager.onUpdateFailed(function () {
  100. // 新的版本下载失败
  101. wx.showModal({
  102. title: '已有新版本咯',
  103. content: '请您删除当前小程序,重新打开呦~',
  104. showCancel: false
  105. })
  106. })
  107. wx.showLoading({
  108. title: '加载中',
  109. });
  110. // 登录
  111. this.login();
  112. },
  113. /**
  114. * 生命周期函数--监听页面初次渲染完成
  115. */
  116. onReady() {
  117. },
  118. /**
  119. * 生命周期函数--监听页面显示
  120. */
  121. onShow() {
  122. if (typeof this.getTabBar === 'function' &&
  123. this.getTabBar()) {
  124. this.getTabBar().setData({
  125. selected: 0
  126. })
  127. }
  128. app.eventBus.on('rightChange', data => {
  129. if (data !== this.data.rightId) {
  130. this.setData({
  131. rightId: data
  132. })
  133. }
  134. })
  135. this.setData({
  136. deviceList: [
  137. { "devName": "名称1", "state": "online", "pic": "../../img/g.png", "power": 40, },
  138. { "devName": "名称2", "state": "online", "pic": "../../img/g.png", "power": 50, },
  139. { "devName": "名称3", "state": "online", "pic": "../../img/g.png", "power": 60, },
  140. ],
  141. selectIndex: 0,
  142. isLoginWx: true,
  143. 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"],
  144. });
  145. },
  146. addDeviceTap() {
  147. wx.navigateTo({
  148. url: './../bluConnect/bleconnect',
  149. });
  150. },
  151. /**
  152. * 生命周期函数--监听页面隐藏
  153. */
  154. onHide() {
  155. },
  156. /**
  157. * 生命周期函数--监听页面卸载
  158. */
  159. onUnload() {
  160. },
  161. /**
  162. * 页面相关事件处理函数--监听用户下拉动作
  163. */
  164. onPullDownRefresh() {
  165. },
  166. /**
  167. * 页面上拉触底事件的处理函数
  168. */
  169. onReachBottom() {
  170. },
  171. /**
  172. * 用户点击右上角分享
  173. */
  174. onShareAppMessage() {
  175. }
  176. })