ソースを参照

feature: 抽取问候语时间类管理

Damon 8 ヶ月 前
コミット
a241391d1a
4 ファイル変更94 行追加95 行削除
  1. 37 36
      app.js
  2. 55 57
      pages/index/index.js
  3. 1 1
      pages/index/index.wxml
  4. 1 1
      pages/index/index.wxss

+ 37 - 36
app.js

@@ -41,6 +41,43 @@ App({
     },
   },
 
+  onLaunch() {
+    var that = this;
+    wx.onAppHide(() => {
+      if (that.globalData.client && that.globalData.client.connected) {
+        that.globalData.client.end(true);
+        that.globalData.client.end(true);
+        console.log("断开");
+      };
+    });
+
+    wx.onAppShow(() => {
+      console.log("加载")
+      if (that.globalData.userInfo !== null) {
+        if (that.globalData.client) {
+          that.globalData.client.end(true);
+          that.globalData.client.end(true);
+        }
+        console.log("重连");
+        that.connect();
+      };
+    });
+    that.getBluetoothStatus();
+    //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
+    wx.getSystemInfo({
+      success: (res) => {
+        let custom = wx.getMenuButtonBoundingClientRect();
+
+        that.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
+        that.globalData.MenuButtonheight = custom.height;
+        that.globalData.MenuButtonTop = custom.top;
+        var platform = res.platform;
+        that.globalData.isIOS = platform == "ios";
+        that.globalData.isAndroid = platform == "android";
+      }
+    });
+  },
+
   onShow: function () {
     update.update();
   },
@@ -187,43 +224,7 @@ App({
       // })
     }
   },
-  onLaunch() {
-    var _this = this;
-    wx.onAppHide(() => {
-      if (_this.globalData.client && _this.globalData.client.connected) {
-        _this.globalData.client.end(true);
-        _this.globalData.client.end(true);
-        console.log("断开");
-      };
-    });
-
-    wx.onAppShow(() => {
-      console.log("加载")
-      if (_this.globalData.userInfo !== null) {
-        if (_this.globalData.client) {
-          _this.globalData.client.end(true);
-          _this.globalData.client.end(true);
-        }
-        console.log("重连");
-        _this.connect();
-      };
-    });
-    this.getBluetoothStatus();
-    //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
-    wx.getSystemInfo({
-      success: (res) => {
-        let custom = wx.getMenuButtonBoundingClientRect();
-
-        _this.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
-        _this.globalData.MenuButtonheight = custom.height;
-        _this.globalData.MenuButtonTop = custom.top;
-        const platform = res.platform;
-        _this.globalData.isIOS = platform == "ios";
-        _this.globalData.isAndroid = platform == "android";
-      }
 
-    })
-  },
   // 获取蓝牙权限
   getBluetoothStatus() {
     const _this = this;

+ 55 - 57
pages/index/index.js

@@ -1,27 +1,22 @@
-// 获取应用实例
 const app = getApp();
 const {
   getBanner,
   login,
   listByDevice
 } = require('../../utils/api.js');
-const {
-  isCN
-} = require('../../utils/util.js');
-const interval = null;
-// import routeUtil from '../../utils/route_util.js'
-import routeUtil from '../../utils/route_util'
-import route_constant from '../../utils/route_constant.js'
+
+import timeUtil from '../../utils/time_util.js';
 
 Page({
   data: {
-    ///下午好
-    greeting: "",
-    phone: "",
     nvabarData: {
       showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
       title: 'OhPlay', //导航栏 中间的标题
     },
+    ///下午好
+    greeting: "",
+    ///用来判断是否已登录
+    phone: "",
     bannerList: [],
     autoplay: true,
     interval: 3000, // 切换时间间隔
@@ -48,19 +43,15 @@ Page({
 
     ////我的界面
     loginStatus: true,
-    nickname: "未登录",
+    nickName: "未登录",
     userPic: './../../img/head_pic.png',
     islogin: false,
   },
 
   onLoad(options) {
     var that = this;
-    var bannerList = wx.getStorageSync("homeBanner") || [];
-    if (that.data.bannerList.length == 0) {
-      that.setData({
-        bannerList: bannerList
-      });
-    }
+    that.onGreetLoad();
+    that.onBannerLoad();
     that.onHomeLoad();
     that.onMeLoad();
   },
@@ -69,35 +60,10 @@ Page({
     var that = this;
     that.onHomeShow();
     that.onMeLoad();
-    var nickname = that.data.nickname;
-    var greeting = that.updateGreeting();
-    if (nickname != "" && nickname != "未登录") {
-      greeting = greeting + ',' + nickname;;
-    }
-    that.setData({
-      greeting: greeting
-    });
-    getBanner({}).then((res) => {
-      that.setData({
-        bannerList: res
-      });
-      wx.setStorageSync("homeBanner", res);
-    })
+    that.onBannerShow();
   },
 
-  updateGreeting() {
-    const now = new Date();
-    const hour = now.getHours();
-    let greeting;
-    if (hour >= 5 && hour < 12) {
-      greeting = '上午好';
-    } else if (hour >= 12 && hour < 18) {
-      greeting = '下午好';
-    } else {
-      greeting = '晚上好';
-    }
-    return greeting;
-  },
+
 
   onHomeLoad() {
     var that = this;
@@ -137,16 +103,13 @@ Page({
           return;
         };
 
-        var nickname = res.data.nickname || "";
-        var greeting = that.updateGreeting();
-        if (nickname != "" && nickname != "未登录") {
-          greeting = greeting + ',' + nickname;;
-        }
+        var nickName = res.data.nickName || "";
+        var greeting = that.getGreetBuNickName(nickName);
 
         that.setData({
           phone: phone,
           greeting: greeting,
-          nickname: nickname,
+          nickName: nickName,
           userPic: res.data.headUrl || "",
           islogin: true,
         });
@@ -624,16 +587,14 @@ Page({
       wx.removeStorage({
         key: 'userInfo',
         success(res) {
-          var nickname = "未登录";
-          var greeting = that.updateGreeting();
-          if (nickname != "" && nickname != "未登录") {
-            greeting = greeting + ',' + nickname;;
-          }
+          var nickName = "未登录";
+          var greeting = that.getGreetBuNickName(nickName);
+
           that.setData({
             phone: "",
             greeting: greeting,
             islogin: false,
-            nickname: nickname,
+            nickName: nickName,
             userPic: "./../../img/head_pic.png",
           });
           // wx.switchTab({
@@ -955,6 +916,43 @@ Page({
     return finalList;
   },
 
+  ///******************************* 可折叠 ********************************************///
+  onGreetLoad() {
+    var that = this;
+    var nickName = that.data.nickName;
+    var greeting = that.getGreetBuNickName(nickName);
+    that.setData({
+      greeting: greeting
+    });
+  },
+
+  getGreetBuNickName(nickName) {
+    var greeting = timeUtil.getGreet();
+    if (nickName != "" && nickName != "未登录") {
+      greeting = greeting + ',' + nickName;;
+    }
+    return greeting;
+  },
+
+  onBannerLoad() {
+    var that = this;
+    var bannerList = wx.getStorageSync("homeBanner") || [];
+    if (that.data.bannerList.length == 0) {
+      that.setData({
+        bannerList: bannerList
+      });
+    }
+  },
+  onBannerShow() {
+    var that = this;
+    getBanner({}).then((res) => {
+      that.setData({
+        bannerList: res
+      });
+      wx.setStorageSync("homeBanner", res);
+    })
+  },
+
 })
 
 

+ 1 - 1
pages/index/index.wxml

@@ -129,7 +129,7 @@
       <view wx:elif="{{indexPage==2}}" class="me" style="height: calc(100vh - 100rpx - {{navBarHeight}}px);">
         <view class="head-logo">
           <image mode="scaleToFill" src="{{userPic}}"></image>
-          <text class="nickname" wx:if="{{nickname}}">{{nickname}}</text>
+          <text class="nick_name" wx:if="{{nickName}}">{{nickName}}</text>
         </view>
         <view class="br"></view>
         <view class="list">

+ 1 - 1
pages/index/index.wxss

@@ -623,7 +623,7 @@
    color: #999;
  }
 
- .me .head-logo .nickname {
+ .me .head-logo .nick_name {
    color: #353535;
  }