Browse Source

feature: 主界面多方法类合体

332777428@qq.com 3 tháng trước cách đây
mục cha
commit
49f1e867f5
3 tập tin đã thay đổi với 129 bổ sung106 xóa
  1. 83 106
      pages/pageA/index/index.js
  2. 1 0
      utils/apiUtil.js
  3. 45 0
      utils/constant.js

+ 83 - 106
pages/pageA/index/index.js

@@ -1,29 +1,8 @@
-const app = getApp();
-const {
-  login,
-  listByDevice,
-  getBanner
-} = require('../../../utils/apiUtil.js');
-
-import timeUtil from '../../../utils/timeUtil.js';
-import strings from '../../../utils/strings.js';
-import store from '../../../utils/store.js';
-import routePath from '../../../utils/routePath.js';
-import routeUtil from '../../../utils/routeUtil.js';
-
-/// 乐鑫相关
-import lexin_util from '../../../utils/lexin/util.js';
-import lexin_jump from '../../../utils/lexin/jump.js';
-import lexin_add from '../../../utils/lexin/add.js';
-import lexin_scan from '../../../utils/lexin/scan.js';
-
-import lexin_subscribe from '../../../utils/lexin/subscribe.js';
-import lexin_connect from '../../../utils/lexin/connect.js';
-import lexin_message from '../../../utils/lexin/message.js';
+const constant = require('../../../utils/constant.js');
 import {
   BtHelper
 } from '../../../devices/bt_helper.js';
-import eventBus from '../../../utils/eventBus.js';
+
 import {
   EnumCmdEvent,
   CmdEvent
@@ -31,9 +10,9 @@ import {
 
 Page({
   data: {
-    navBarHeight: app.globalData.navBarHeight,
-    safeBottomHeight: app.globalData.safeBottomHeight,
-    bannerList: app.globalData.bannerList,
+    navBarHeight: constant.app.globalData.navBarHeight,
+    safeBottomHeight: constant.app.globalData.safeBottomHeight,
+    bannerList: constant.app.globalData.bannerList,
     nvabarData: {
       showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
       title: 'OhPlay', //导航栏 中间的标题
@@ -57,9 +36,9 @@ Page({
     ///是否展示频道
     showChannel: false,
     ////首页
-    navBarHeight: app.globalData.navBarHeight,
-    MenuButtonheight: app.globalData.MenuButtonheight,
-    MenuButtonTop: app.globalData.MenuButtonTop,
+    navBarHeight: constant.app.globalData.navBarHeight,
+    MenuButtonheight: constant.app.globalData.MenuButtonheight,
+    MenuButtonTop: constant.app.globalData.MenuButtonTop,
     actionIndex: null,
     luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
     channelData: [],
@@ -176,9 +155,9 @@ Page({
   onLoginLoad() {
     var that = this;
     var userInfo = wx.getStorageSync("userInfo") || "";
-    if (!strings.isEmpty(userInfo)) {
+    if (!constant.strings.isEmpty(userInfo)) {
       var phone = userInfo.phone;
-      if (!strings.isEmpty(phone)) {
+      if (!constant.strings.isEmpty(phone)) {
         that.loginReset(phone, null, null, null);
       };
     }
@@ -191,13 +170,13 @@ Page({
     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}
-        login({
+        constant.apiUtil.login({
           code: res0.code,
           phone: phone,
         }).then((res1) => {
-          app.globalData.userInfo = res1;
-          store.setStore("userInfo", res1);
-          store.setStore("token", JSON.stringify({
+          constant.app.globalData.userInfo = res1;
+          constant.store.setStore("userInfo", res1);
+          constant.store.setStore("token", JSON.stringify({
             id: res1.userId,
             token: res1.accessToken,
           }));
@@ -235,9 +214,9 @@ Page({
   },
   onDeviceLoad() {
     var that = this;
-    var deviceList = store.getStore("deviceList");
+    var deviceList = constant.store.getStore("deviceList");
 
-    if (!strings.isEmpty(deviceList)) {
+    if (!constant.strings.isEmpty(deviceList)) {
       // deviceList = deviceList.concat(deviceList);
       that.updateDeviceList(deviceList, true, false);
     }
@@ -291,9 +270,8 @@ Page({
   },
   /// 对比蓝牙数据
   compareList() {
-
     let that = this;
-    lexin_scan.compareList2(that.data.deviceList, function (device) {
+    constant.lexinScan.compareList2(that.data.deviceList, function (device) {
       if (device) {
         that.data._willConnectBle = device;
         getApp().getBluetoothStatus();
@@ -301,7 +279,7 @@ Page({
 
     });
 
-    // lexin_scan.compareList(function (list) {
+    // constant.lexinScan.compareList(function (list) {
     //   console.log("对比蓝牙数据", list);
     //   that.updateDeviceList(list, false, false);
     // }, function (item) {
@@ -356,11 +334,11 @@ Page({
       switch (type) {
         ///先走订阅
         case "connect":
-          lexin_subscribe.subscribeAllDevice();
+          constant.lexinSubscribe.subscribeAllDevice();
           break;
           ///再走有在线
         case "message_onoffline":
-          lexin_connect.searchOnlineDevice(payloads, function (list) {
+          constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
             that.updateDeviceList(list, false, false);
           }, function () {
             var indexPage = that.data.indexPage;
@@ -386,7 +364,7 @@ Page({
 
             ///获取频道位置
             if (payloadType === "get_position" && other) {
-              lexin_message.getPosition(other, function (actionIndex) {
+              constant.lexinMessage.getPosition(other, function (actionIndex) {
                 that.setData({
                   actionIndex: actionIndex,
                 });
@@ -396,13 +374,13 @@ Page({
             // 获取歌曲信息
             else if (payloadType === "get_dev_info" && other) {
               that.setDeviceInfor(other);
-              lexin_message.getDeviceInfo(payloads);
+              constant.lexinMessage.getDeviceInfo(payloads);
             }
 
             ///获取电量
             else if (payloadType === "battery" && other) {
               if (other.battery) {
-                var battery = lexin_message.getBattery(other.battery);
+                var battery = constant.lexinMessage.getBattery(other.battery);
                 that.setData({
                   battery: battery,
                 });
@@ -411,7 +389,7 @@ Page({
 
             ///获取播放状态
             else if (payloadType === "play" || payloadType === "play_state") {
-              lexin_message.getPlay();
+              constant.lexinMessage.getPlay();
             }
           }
           break;
@@ -425,9 +403,9 @@ Page({
     var that = this;
     // 取消订阅
     var deviceMacId = that.data.deviceMacId;
-    if (!strings.isEmpty(deviceMacId)) {
-      var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
-      app.unsubscribe(pubResponse);
+    if (!constant.strings.isEmpty(deviceMacId)) {
+      var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
+      constant.app.unsubscribe(pubResponse);
     };
     that.subscribeCurrDevice(device);
   },
@@ -435,9 +413,9 @@ Page({
   ///数据处理
   subscribeCurrDevice(device) {
     var that = this;
-    var isOK = app.globalData.client && app.globalData.client.connected;
+    var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
     if (!isOK) {
-      app.connect();
+      constant.app.connect();
       setTimeout(() => {
         that.subscribeCurrDevice(device);
       }, 500);
@@ -445,20 +423,20 @@ Page({
     };
 
     var deviceId = device.deviceId;
-    var deviceMacId = lexin_util.getDeviceMacId(deviceId);
+    var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
     that.setData({
       deviceMacId: deviceMacId,
     });
 
     /// 订阅设备
-    var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
-    app.subscribe(pubResponse);
+    var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
+    constant.app.subscribe(pubResponse);
 
     /// 获取设备信息
     const obj = {
       DstDeviceName: deviceMacId
     };
-    app.PubMsg({
+    constant.app.PubMsg({
       type: "get_dev_info",
       ...obj
     });
@@ -468,7 +446,7 @@ Page({
   onTapChannelIndex(e) {
     var that = this;
     var index = e.currentTarget.dataset.index;
-    lexin_jump.toChannelIndex(index, function (i) {
+    constant.lexinJump.toChannelIndex(index, function (i) {
       that.setData({
         actionIndex: i,
       });
@@ -485,19 +463,19 @@ Page({
 
     var channelData = that.getChannelData();
     if (channelData.length > actionIndex) {
-      store.setStore("channelDeta", channelData[actionIndex]);
-      routeUtil.jump(routePath.channelDetail);
+      constant.store.setStore("channelDeta", channelData[actionIndex]);
+      constant.routeUtil.jump(constant.routePath.channelDetail);
     }
   },
 
   ///选择对应的频道
   getchannelData(clientType) {
     var that = this;
-    listByDevice({
+    constant.apiUtil.listByDevice({
       clientType: clientType,
       unShowLoad: true,
     }).then((res) => {
-      store.setStore("channelData", res);
+      constant.store.setStore("channelData", res);
       that.setData({
         channelData: res,
       });
@@ -510,11 +488,11 @@ Page({
       };
 
       var deviceId = deviceList[deviceListSelect].deviceId;
-      var deviceMacId = lexin_util.getDeviceMacId(deviceId);
+      var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
       const obj = {
         DstDeviceName: deviceMacId
       }
-      app.PubMsg({
+      constant.app.PubMsg({
         type: "get_position",
         ...obj
       });
@@ -540,7 +518,7 @@ Page({
       that.logOut();
       return;
     }
-    routeUtil.jump(routePath.login);
+    constant.routeUtil.jump(constant.routePath.login);
   },
 
   ///退出登录
@@ -565,9 +543,9 @@ Page({
     ///有设备在线被选中,则让它不被选择
     if (device.connectType == 3) {
       var deviceMacId = that.data.deviceMacId;
-      if (!strings.isEmpty(deviceMacId)) {
-        var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
-        app.unsubscribe(pubResponse);
+      if (!constant.strings.isEmpty(deviceMacId)) {
+        var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
+        constant.app.unsubscribe(pubResponse);
         var indexPage = that.data.indexPage;
         that.setData({
           deviceMacId: null,
@@ -595,7 +573,7 @@ Page({
     var that = this;
     var isLogin = that.getIsLogin();
     if (!isLogin) {
-      routeUtil.jump(routePath.login);
+      constant.routeUtil.jump(constant.routePath.login);
       return;
     }
 
@@ -621,7 +599,7 @@ Page({
     let curDevice = deviceList[index]
     if (index === deviceListSelect && device.state === "online") {
       var param = "?param=" + JSON.stringify(curDevice);
-      routeUtil.jumpParam(routePath.deviceDetail, param);
+      constant.routeUtil.jumpParam(constant.routePath.deviceDetail, param);
     } else if (device.state === "offline") {
 
       var deviceListSelect = that.getDeviceListSelect();
@@ -685,8 +663,8 @@ Page({
         first.state == "offline"
       } else {
         // 取消订阅
-        // var pubResponse = lexin_util.getResponseByDeviceId(first.deviceId);
-        // app.unsubscribe(pubResponse);
+        // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
+        //constant.app.unsubscribe(pubResponse);
       }
     }
   },
@@ -701,7 +679,7 @@ Page({
     };
     var device = deviceList[deviceListSelect];
     var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
-    routeUtil.jumpParam(routePath.deviceWake, param);
+    constant.routeUtil.jumpParam(constant.routePath.deviceWake, param);
   },
 
   ///删除当前设备
@@ -777,7 +755,7 @@ Page({
     deviceList = deviceList.filter((item, i) => {
       return deviceId !== item.deviceId;
     });
-    store.setStore("deviceList", deviceList);
+    constant.store.setStore("deviceList", deviceList);
     console.log("确定删除1", isCurrentIndex, deviceListSelect)
     var indexPage = that.data.indexPage;
     that.setData({
@@ -804,8 +782,8 @@ Page({
 
     var deviceId = deviceList[index].deviceId;
     // 取消订阅
-    var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
-    app.unsubscribe(pubResponse);
+    var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
+    constant.app.unsubscribe(pubResponse);
     if (onlyCancel) {
       return;
     }
@@ -821,7 +799,7 @@ Page({
       return deviceId !== item.deviceId;
     });
 
-    store.setStore("deviceList", deviceList);
+    constant.store.setStore("deviceList", deviceList);
     var indexPage = that.data.indexPage;
     var deviceMacId = that.data.deviceMacId;
     var actionIndex = that.data.actionIndex;
@@ -837,7 +815,7 @@ Page({
   /// 新添加蓝牙设备
   addBlueDevice(device) {
     var that = this;
-    var addDeviceList = lexin_add.addBlueDevice(device);
+    var addDeviceList = constant.lexinAdd.addBlueDevice(device);
     let hasOnline = 0
     addDeviceList.forEach(item => {
       // 蓝牙设备下线。wifi不动
@@ -882,8 +860,8 @@ Page({
   },
   addNotification() {
     let that = this;
-    eventBus.removeNotification(CmdEvent.eventName, that);
-    eventBus.addNotification(CmdEvent.eventName, function (event) {
+    constant.eventBus.removeNotification(CmdEvent.eventName, that);
+    constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
       let deviceList = that.getDeviceList()
 
       if (event.cmdEvent == EnumCmdEvent.btMac) {
@@ -948,9 +926,9 @@ Page({
   // 新添加Wifi设备
   addWifiDevice(device) {
     var that = this;
-    var isOK = app.globalData.client && app.globalData.client.connected;
+    var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
     if (!isOK) {
-      app.connect();
+      constant.app.connect();
       setTimeout(() => {
         that.addWifiDevice(device);
       }, 500);
@@ -959,7 +937,7 @@ Page({
     console.log("有新的wifi设备0", device)
 
     var deviceId = device.deviceId;
-    var addDeviceList = lexin_add.addWifiDevice(device);
+    var addDeviceList = constant.lexinAdd.addWifiDevice(device);
     console.log("有新的wifi设备", addDeviceList)
     that.updateDeviceList(addDeviceList, false, false);
 
@@ -969,13 +947,13 @@ Page({
       connectDeviceIding: deviceId,
       indexPage: indexPage == 1 ? 0 : indexPage,
     });
-    lexin_subscribe.subscribeSingleDevice(device);
+    constant.lexinSubscribe.subscribeSingleDevice(device);
   },
 
   refreshCurrentDevice(device) {
     var that = this;
     console.log("刷新列表0:", device)
-    var addDeviceList = lexin_add.addWifiDevice(device);
+    var addDeviceList = constant.lexinAdd.addWifiDevice(device);
     console.log("刷新列表1:", addDeviceList)
     that.updateDeviceList(addDeviceList, false, false);
     that.setData({
@@ -995,8 +973,8 @@ Page({
   /// 更新列表排序
   updateDeviceList(deviceList, isInit, closeAllBlue) {
     var that = this;
-    var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
-    store.setStore("deviceList", finalList);
+    var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
+    constant.store.setStore("deviceList", finalList);
     that.setData({
       deviceList: finalList,
     });
@@ -1006,17 +984,17 @@ Page({
   onTapBanner(e) {
     var item = e.currentTarget.dataset.item;
     try {
-      if (strings.isEmpty(item.forwardUrl)) {
+      if (constant.strings.isEmpty(item.forwardUrl)) {
         return;
       }
       // 内链,暂时不支持
       if (item.forwardType == 0) {
-        routeUtil.jump(item.forwardUrl, item.forwardUrl);
+        constant.routeUtil.jump(item.forwardUrl, item.forwardUrl);
       }
       // 外链
       else if (item.forwardType == 1) {
         var param = "?param=" + item.forwardUrl;
-        routeUtil.jumpParam(routePath.webview, param);
+        constant.routeUtil.jumpParam(constant.routePath.webview, param);
       } else {
 
       }
@@ -1035,15 +1013,15 @@ Page({
     var that = this;
     var isLogin = that.getIsLogin();
     if (!isLogin) {
-      routeUtil.jump(routePath.login);
+      constant.routeUtil.jump(constant.routePath.login);
       return;
     }
-    routeUtil.jump(routePath.deviceList);
+    constant.routeUtil.jump(constant.routePath.deviceList);
   },
 
   // 关于我们
   jumpToAboutUs() {
-    lexin_jump.toAboutUs();
+    constant.lexinJump.toAboutUs();
   },
 
   /// 是否已登录
@@ -1126,7 +1104,7 @@ Page({
     that.getchannelData(other.ProdModel);
     // 电量
     if (other.Power) {
-      var battery = lexin_message.getBattery(other.Power);
+      var battery = constant.lexinMessage.getBattery(other.Power);
       that.setData({
         battery: battery,
       });
@@ -1149,7 +1127,7 @@ Page({
   onUserInfoLoad() {
     var that = this;
     var userInfo = wx.getStorageSync("userInfo") || "";
-    if (!strings.isEmpty(userInfo)) {
+    if (!constant.strings.isEmpty(userInfo)) {
       var phone = userInfo.phone;
       if (!phone) {
         return;
@@ -1159,7 +1137,7 @@ Page({
       var greeting = that.getGreetBuNickName(nickName);
       // 获取缓存的频道数据
       var channelData = wx.getStorageSync("channelData") || "";
-      if (!strings.isEmpty(channelData)) {
+      if (!constant.strings.isEmpty(channelData)) {
         that.setData({
           channelData: channelData,
           greeting: greeting,
@@ -1184,7 +1162,7 @@ Page({
   },
 
   getGreetBuNickName(nickName) {
-    var greeting = timeUtil.getGreet();
+    var greeting = constant.timeUtil.getGreet();
     if (nickName != "" && nickName != "未登录") {
       greeting = greeting + ',' + nickName;;
     }
@@ -1193,17 +1171,17 @@ Page({
 
   onBannerLoad() {
     var that = this;
-    getBanner({}).then((res) => {
+    constant.apiUtil.getBanner({}).then((res) => {
       that.setData({
         bannerList: res,
       });
-      store.setStore("homeBanner", res);
+      constant.store.setStore("homeBanner", res);
     });
   },
 
   stopIntervalId1: function () {
     var that = this;
-    if (!strings.isEmpty(that.data.intervalId1)) {
+    if (!constant.strings.isEmpty(that.data.intervalId1)) {
       clearInterval(that.data.intervalId1);
       that.data.intervalId1 = null;
     }
@@ -1211,7 +1189,7 @@ Page({
 
   stopIntervalId2: function () {
     var that = this;
-    if (!strings.isEmpty(that.data.intervalId2)) {
+    if (!constant.strings.isEmpty(that.data.intervalId2)) {
       clearInterval(that.data.intervalId2);
       that.data.intervalId2 = null;
     }
@@ -1220,21 +1198,20 @@ Page({
   ///销毁蓝牙
   onUnload() {
     var that = this;
-    eventBus.removeNotification(CmdEvent.eventName, that)
+    constant.eventBus.removeNotification(CmdEvent.eventName, that)
     console.log("界面onUnload")
     that.stopIntervalId1();
     that.stopIntervalId2();
-    if (!strings.isEmpty(that.data.intervalId)) {
+    if (!constant.strings.isEmpty(that.data.intervalId)) {
       clearInterval(that.data.intervalId);
       that.data.intervalId = null;
     }
     BtHelper.getInstance().disconnect();
-    if (app.globalData.client === null) {
+    if (constant.app.globalData.client === null) {
       return;
     };
-    app.globalData.client.end(true);
-    app.globalData.client.end(true);
-    app.globalData.client = null;
-
+    constant.app.globalData.client.end(true);
+    constant.app.globalData.client.end(true);
+    constant.app.globalData.client = null;
   },
 })

+ 1 - 0
utils/apiUtil.js

@@ -1,6 +1,7 @@
 const {
   request
 } = require('./requestUtil.js')
+
 module.exports = {
   // 首页banner
   getBanner: (data) => {

+ 45 - 0
utils/constant.js

@@ -0,0 +1,45 @@
+const app = getApp();
+
+///request
+const dRequest = require('../request/deviceListRequest.js');
+
+
+///utils
+const routeUtil = require('../utils/routeUtil.js');
+const routePath = require('../utils/routePath.js');
+const strings = require('../utils/strings.js');
+const store = require('../utils/store.js');
+const timeUtil = require('../utils/timeUtil.js');
+const apiUtil = require('../utils/apiUtil.js');
+const eventBus = require('../utils/eventBus.js');
+
+///utils/lexin
+const lexinDevices = require('../utils/lexin/devices.js');
+const lexinUtil = require('../utils/lexin/util.js');
+const lexinJump = require('../utils/lexin/jump.js');
+const lexinAdd = require('../utils/lexin/add.js');
+const lexinScan = require('../utils/lexin/scan.js');
+const lexinSubscribe = require('../utils/lexin/subscribe.js');
+const lexinConnect = require('../utils/lexin/connect.js');
+const lexinMessage = require('../utils/lexin/message.js');
+
+
+module.exports = {
+  eventBus: eventBus,
+  lexinMessage: lexinMessage,
+  lexinConnect: lexinConnect,
+  lexinSubscribe: lexinSubscribe,
+  lexinScan: lexinScan,
+  lexinAdd: lexinAdd,
+  lexinJump: lexinJump,
+  lexinUtil: lexinUtil,
+  apiUtil: apiUtil,
+  timeUtil: timeUtil,
+  lexinDevices: lexinDevices,
+  store: store,
+  strings: strings,
+  routePath: routePath,
+  routeUtil: routeUtil,
+  dRequest: dRequest,
+  app: app,
+}