Ver código fonte

to: handle user login permission logic

wangyuan 2 anos atrás
pai
commit
3bb4301a3e

+ 5 - 2
app.js

@@ -7,7 +7,10 @@ App({
     console.log('App onLaunch');
   },
   globalData: {
-    userInfo: null,
-    selectedInex: 2
+    userInfo: {
+      name: '',
+      login: false
+    },
+    selectedInex: 2 // 自定义tabbar选中的index
   }
 })

+ 12 - 2
custom-tab-bar/index.js

@@ -32,8 +32,18 @@ Component({
     switchTab(e) {
       const data = e.currentTarget.dataset
       const url = data.path
-      app.globalData.selectedInex = data.index
-      wx.switchTab({url})
+      if (data.index == 0 || data.index == 1) {
+        if (!app.globalData.userInfo.login) {
+          wx.showToast({
+            title: '请先授权登录',
+            icon: 'error'
+          })
+          return false
+        } else {
+          app.globalData.selectedInex = data.index
+          wx.switchTab({url})
+        }
+      }
     }
   }
 })

+ 1 - 1
pages/address/address.wxml

@@ -16,7 +16,7 @@
         </view>
         <view class="delete-btn btn ittflex">
           <image class="btn-icon" src="./../../imaes/shanchu@2x.png"></image>
-          <text class="btn-text">编辑</text>
+          <text class="btn-text">删除</text>
         </view>
       </view>
     </view>

+ 1 - 1
pages/addressCheck/addressCheck.wxml

@@ -16,7 +16,7 @@
         </view>
         <view class="delete-btn btn ittflex">
           <image class="btn-icon" src="./../../imaes/shanchu@2x.png"></image>
-          <text class="btn-text">编辑</text>
+          <text class="btn-text">删除</text>
         </view>
       </view>
     </view>

+ 46 - 27
pages/my/my.js

@@ -1,89 +1,108 @@
 // pages/my/my.js
+import itt from '../../utils/util'
+const app = getApp()
 Page({
 
   /**
    * 页面的初始数据
    */
   data: {
-
+    pageLogin: false,
+    name: '点击授权登录',
+    avatarUrl: '',
+    count: 0
   },
 
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
-
+    
   },
 
   /**
-   * 生命周期函数--监听页面初次渲染完成
+   * 获取用户信息
    */
-  onReady() {
-
+  getUserProfile() {
+    var that = this;
+    wx.getUserProfile({
+      desc:"授权信息",
+      success:function(res){
+        if(res.userInfo){
+          that.setData({
+            name:res.userInfo.nickName,
+            avatarUrl:res.userInfo.avatarUrl,
+            pageLogin: true
+          })
+          app.globalData.userInfo.login = true
+        }
+      },
+    })
   },
 
   /**
    * 生命周期函数--监听页面显示
    */
   onShow() {
-
+    this.setData({
+      pageLogin: app.globalData.userInfo.login
+    })
   },
   /* 我的档案 */
   handleMyFile () {
-    wx.navigateTo({
-      url: '/pages/myFile/myFile',
-    })
+     this.authNavTo('/pages/myFile/myFile')
   },
   /**
    * 我的预约
    */
   handleMyAppointment() {
-    wx.navigateTo({
-      url: '/pages/myAppointment/myAppointment',
-    })
+     this.authNavTo('/pages/myAppointment/myAppointment')
   },
 
   /**
    * 点击充值记录
    */
   handleMyRecord() {
-    wx.navigateTo({
-      url: '/pages/myRecord/myRecord',
-    })
+     this.authNavTo('/pages/myRecord/myRecord')
   },
 
   /**
    * 点击开通区域
    */
   handleOpenarea() {
-    wx.navigateTo({
-      url: '/pages/openArea/openArea',
-    })
+     this.authNavTo('/pages/openArea/openArea')
   },
 
   /**
    * 点击设置
    */
   handleSet() {
-    wx.navigateTo({
-      url: '/pages/set/set'
-    })
+     this.authNavTo('/pages/set/set')
   },
 
   /**
    * 用户点击地址
    */
   handleMyAddress() {
-    wx.navigateTo({
-      url: '/pages/address/address',
-    })
+    this.authNavTo('/pages/address/address')
   },
   /**
    *  我的报告
    * **/
   handleMyReport () {
-    wx.navigateTo({
-      url: '/pages/report/report',
-    })
+    this.authNavTo('/pages/report/report')
+  },
+
+  handleAppoint () {
+    this.authNavTo('/pages/appointment/appointment')
+  },
+  authNavTo (url) {
+    if (itt.loginAuth()) {
+      wx.navigateTo({
+        url: url,
+      })
+    } else {
+      itt.errorToast('请先授权登录')
+    }
   }
 })

+ 7 - 4
pages/my/my.wxml

@@ -3,16 +3,19 @@
   <view class="top my-info ittflex">
     <view class="main-info ittflex-jcb">
       <view class="left-con ittflex-jcs">
-        <view class="avatar"></view>
-        <text class="username">测试用户</text>
+        <view class="avatar ittflex" bindtap="getUserProfile">
+          <van-icon wx:if="{{avatarUrl == ''}}" name="contact" size="60rpx" color="#999"/>
+          <image wx:else class="image-ac" src="{{avatarUrl}}"></image>
+        </view>
+        <text class="username" bindtap="getUserProfile">{{name}}</text>
       </view>
       <view class="right-con">
         <view class="check-count">
           <text class="noraltext">检测机会:</text>
-          <text class="boldtext">5</text>
+          <text class="boldtext">{{count}}</text>
           <text class="noraltext">次</text>
         </view>
-        <view class="appoin-btn ittflex">立即预约</view>
+        <view class="appoin-btn ittflex" bindtap="handleAppoint">立即预约</view>
       </view>
     </view>
   </view>

+ 7 - 1
pages/my/my.wxss

@@ -18,13 +18,19 @@
   width: 120rpx;
   height: 120rpx;
   border-radius: 50%;
-  background-color: #999;
+  background-color: #fff;
   margin-right: 20rpx;
 }
+.image-ac {
+  width: 120rpx;
+  height: 120rpx;
+  border-radius: 50%;
+}
 .username {
   font-size: 30rpx;
   font-weight: bold;
   color: #FFFFFF;
+  padding: 20rpx 0;
 }
 .check-count {
   margin-bottom: 10rpx;

+ 11 - 4
pages/report/report.js

@@ -1,32 +1,39 @@
 // pages/report/report.js
+import itt from '../../utils/util'
+const app = getApp()
 Page({
 
   /**
    * 页面的初始数据
    */
   data: {
-    reportList: [1,1,1,1]
+    reportList: [],
+    showPage: true
   },
 
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
-
+    console.log('load');
   },
 
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady() {
-
+    console.log('ready');
   },
 
   /**
    * 生命周期函数--监听页面显示
    */
   onShow() {
-
+    var isLogin = itt.loginAuth()
+    console.log(isLogin, 'isLoginisLogin');
+    this.setData({
+      showPage: isLogin
+    })
   },
 
   /**

+ 36 - 16
utils/util.js

@@ -1,17 +1,37 @@
-
-const formatNumber = n => {
-  n = n.toString()
-  return n[1] ? n : `0${n}`
-}
-
-const ITTLoading = (title) => {
-  wx.showLoading({
-    title: title ? title : '加载中...',
-    mask: true
-  })
-}
-
-module.exports = {
-  formatNumber,
-  ITTLoading
+const app = getApp()
+export default {
+  loading (title) {
+    wx.showLoading({
+      title: title || '加载中...',
+      mask: true
+    })
+  },
+  errorToast (title) {
+    wx.showToast({
+      title: title || '请先登录',
+      icon: 'error'
+    })
+  },
+  navigateTo (url,permissionTitle) { 
+    if (url) {
+      if (app.globalData.userInfo.login) {
+        wx.navigateTo({
+          url: 'url'
+        })
+      } else {
+        wx.showToast({
+          title: permissionTitle || '请先登录',
+          icon: 'error'
+        })        
+      }
+    } else {
+      wx.showToast({
+        title: '地址错误',
+        icon: 'error'
+      })
+    }
+  },
+  loginAuth () {
+    return app.globalData.userInfo.login
+  }
 }