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

小程序 | 在首页判断是否登录并登陆执行

分类:小程序时间:2018-12-09浏览:8207
index.wxml

    //其他业务



index.js
import { config } from '../../config.js'
// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //判断小程序的API,回调,参数,组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    is_login:true
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 查看是否授权
    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: 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'
            })
            that.setData({
              is_login: true
            })
          } else {
            console.log("写入失败")
          }
        }
      })
      //授权成功后,跳转进入小程序首页
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },

 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onSearch: function () {
    wx.navigateTo({
      url: `/pages/hs_search/index`,
    })
  },
 

})
index.wxss
/* 授权 */
.dew image{
  width: 100%;
  height:600rpx;
}
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: {
    userInfo:null,
    openid:''
  }
})
config.js
const config = {
    api_base_url: 'https://xxx.com/api/',
    appkey: "xxxxxx",
}

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