限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >网站框架 >小程序

小程序 | 在首页判断是否登录并跳转到登陆界面

分类:小程序时间:2018-12-09浏览:22438
login
//index.js


import { config } from '../../config.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //判断小程序的API,回调,参数,组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    login_pic:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              //从数据库获取用户信息
              that.queryUsreInfo();
              //用户已经授权过
              wx.switchTab({
                url: '/pages/index/index'
              })
            }
          });
        }
      }
    })
    // 登录图片
    wx.request({
      url: config.api_base_url + 'index/login_pic',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          login_pic: res.data.msg
        })
      }
    })

  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //插入登录的用户的相关信息到数据库
      var openid = getApp().globalData.openid; 
      wx.request({
        url: config.api_base_url + 'Xcx/login', 
        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'
            })
          }else{
            console.log("写入失败")
          }
        }
      })
      //授权成功后,跳转进入小程序首页
      
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  //获取用户信息接口
  queryUsreInfo: function () {
    // var openid = getApp().globalData.openid;
    // wx.request({
    //   url: getApp().globalData.urlPath + 'hstc_interface/queryByOpenid',
    //   data: {
    //     openid: getApp().globalData.openid
    //   },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data);
    //     getApp().globalData.userInfo = res.data;
    //   }
    // }) 
  },  
})

index.json


{
  "navigationBarTitleText": "授权登录"
}


index.wxml


    
        
    
    
    

请升级微信版本

index.js

.header {
    margin: 90rpx 0 90rpx 50rpx;
    /* border-bottom: 1px solid #ccc; */
    text-align: center;
    width: 650rpx;
    height: 600rpx;
    line-height: 450rpx;
}
 
.header image {
    width: 300px;
    height: 350px;
}
 
.content {
    margin-left: 50rpx;
    margin-bottom: 90rpx;
}
 
.content text {
    display: block;
    color: #9d9d9d;
    margin-top: 40rpx;
}
 
.bottom {
    border-radius: 80rpx;
    margin: 70rpx 50rpx;
    font-size: 35rpx;
}

index
onLoad: function (options) {
    //获取栏目
    var that = this
    wx.showLoading({
      title: ''
    })
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          //未登录,跳转到登录页
          wx.redirectTo({
            url: '/pages/login/index',
          })
        }
      }
    })
    //获取banner
    wx.request({
      url: config.api_base_url + 'index/banner', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          slider: res.data.msg.banner,
          text: res.data.msg.pmd
        })
      }
    })
    
    
    setTimeout(function () {
      wx.hideLoading()
      that.setData({
        hidden: false
      })
    }, 2000)
    
  },
app.js
//app.js
import { config } from './config.js'

App({
  onLaunch: function () {
    var that = this;
    wx.login({
      success: res => {
        wx.request({
          url: config.api_base_url + 'Xcx/get_openid', 
          // url: that.globalData.wx_url_1 + res.code + that.globalData.wx_url_2,
          data:{
            code: res.code
          },
          success: res => {
            that.globalData.openid = res.data.msg;
          }
        })
      }
    });

  },
  globalData: {
    

  }
})
config,js
const config = {
    api_base_url: 'https://xxxxxx.com/api/',
    appkey: "XXXXX",
}

export {config }
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.