123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // pages/auth/auth.js
- const app = getApp();
- var Api = require('../../utils/api.js');
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- canIUse: wx.canIUse('button.open-type.getUserInfo')
- },
- onAuth() {
- wx.getSetting({
- success: (res) => {
- if (res.authSetting['scope.userInfo']) {
- // app.globalData.userInfo = res.userInfo
- wx.getUserInfo({
- success: res => {
- app.globalData.userInfo = res.userInfo
- }
- })
- }
- }
- })
- //登录
- wx.login({
- success: res => {
- // console.log(res)
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- if (res.code) {
- //发起网络请求
- wx.request({
- url: Api.postgetOpenId(),
- data: {
- codeId: res.code
- },
- success (res) {
- // console.log(res)
- if(res.statusCode==200){
- // app.globalData.openid=res.data.data.openid
- wx.setStorageSync('openid', res.data.data.openid)
- var pages = getCurrentPages(); // 当前页面
- var beforePage = pages[pages.length - 2]
- wx.navigateBack({
- success: function() {
- beforePage.onLoad(); // 执行前一个页面的onLoad方法
- }
- })
- }else{
- }
- }
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- wx.hideHomeButton({
- success: function() {
- },
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|