zoukankan      html  css  js  c++  java
  • 在首页判断是否登录并执行登陆

    首页先判断是否登录

    index.wxml

    <!-- 判断是否登录 -->
    <modal class="modal" hidden="{{is_login}}" no-cancel bindconfirm="close" confirmText=" ">
      <view class="dew">
        <image src='/images/login.png' />
        <button class='bottom' type='primary' open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">
          请您授权登录,否则无法正常操作
        </button>
      </view>
    </modal>

    index.js

    
    
    var api = require('../../api.js');
    var app = getApp();
    Page({
     
      /**
       * 页面的初始数据
       */
      data: {
        //判断小程序的API,回调,参数,组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        is_login:true
      },
      
      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {// 查看是否授权
        wx.getSetting({
          success: function (res) {
            if (!res.authSetting['scope.userInfo']) {
              that.setData({
                is_login: false
              })
            }
          }
        })
      },
      //执行登陆
      bindGetUserInfo: function (e) {
        if (e.detail.userInfo) {
          //用户按了允许授权按钮
          var that = this;
          //插入登录的用户的相关信息到数据库
          var openid = getApp().globalData.openid;
          getApp().globalData.userInfo = e.detail.userInfo;
          // console.log(getApp().globalData.userInfo)
          wx.request({
            url: api.wx.UserLogin,
            data: {
              userinfo: e.detail.userInfo,
              openid: openid
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: 'post',
            success(res) {
              if (res.data.result == 1) {
                wx.switchTab({
                  url: '/pages/index/index'
                })
                that.setData({
                  is_login: true
                })
              } else {
                console.log("写入失败")
              }
            }
          })
          //授权成功后,跳转进入小程序首页
        } else {
          //用户按了拒绝按钮
          wx.showModal({
            title: '警告',
            content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
            showCancel: false,
            confirmText: '返回授权',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击了“返回授权”')
              }
            }
          })
        }
      },
     
    })

    app.js

     onLaunch: function () {
        var that = this;
        wx.login({
          success: res => {
            wx.request({
              url: api.wx.jsCode + '?jsCode=' + res.code,
              data: {
              },
              success: res => {
                 var obj = {};
                 obj.openid = res.data.openid;
                 ...
                 wx.setStorageSync('user', obj); //存储openid 
              }
            })
          }
        });
     
      },    

    获取用户手机号:

    index.wxml

    <modal class="modal" hidden="{{is_login}}" no-cancel bindconfirm="close" confirmText=" ">
          <view class="dew">
            <image src='/images/login.png' />
            <button class='bottom' type='primary' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
            请您授权登录,否则无法正常操作
            </button>
          </view>
    </modal>

    index.js

     /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
        var that = this;
        ...
    
        //是否登录
        if (!fzuser){
          that.setData({
            is_login: false
          })
        }
     },
    
    //获取用户手机号
      getPhoneNumber: function (e) {
        var that = this;
        console.log(e)
        if (e.detail.errMsg == 'getPhoneNumber:fail user deny' || e.detail.encryptedData == null || e.detail.iv == null) {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '未授权,您不能使用服务!',
            success: function (res) {
              console.log(res)
            }
          })
        } else {
          ...
          wx.request({
            url:api.wx.UserLogin,
            data: {
              encryptedData: e.detail.encryptedData,
              sessionKey: user.session_key,
              iv: e.detail.iv,
              openid: openid,
              version: api.wx.version,
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT      
            success: function (res) {
              console.log(res);
              ...
              that.setData({
                is_login: true
              })
    
            }
          });
        }
    
      },
    
    /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {   
        var that = this;
        ...
    //验证登录是否过期 wx.checkSession({ success() { // session_key 未过期,并且在本生命周期一直有效 console.log('登录未过期') }, fail() { // session_key 已经失效,需要重新执行登录流程 // wx.login() // 重新登录 that.setData({ is_login: false }) } }) },
  • 相关阅读:
    编译原理-第二章 一个简单的语法指导编译器-2.4 语法制导翻译
    编译原理-第二章 一个简单的语法指导编译器-2.3 语法定义
    编译原理-第二章 一个简单的语法指导编译器-2.2 词法分析
    LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram
    LeetCode 1348. Tweet Counts Per Frequency
    1349. Maximum Students Taking Exam(DP,状态压缩)
    LeetCode 1345. Jump Game IV(BFS)
    LeetCode 212. Word Search II
    LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)
    LeetCode 187. Repeated DNA Sequences(位运算,hash)
  • 原文地址:https://www.cnblogs.com/joe235/p/10671025.html
Copyright © 2011-2022 走看看