1. 程式人生 > >微信小程序 app註冊小程序+page註冊頁面代碼一

微信小程序 app註冊小程序+page註冊頁面代碼一

隱藏 index 程序代碼 處理 ready 由於 userinfo 程序 call

註冊小程序代碼:app.js

//app.js
App({
  onLaunch: function(){
    var log = wx.getStorageSync("logs") || []
    log.unshift(Date.now())
    wx.setStorageSync("logs", log)
    wx.login({
      success: res => {
        // 發送 res.code 到後臺換取 openId, sessionKey, unionId
      
      }
    })
    // 獲取用戶信息
    wx.getSetting({
      success: res 
=> { if (res.authSetting[‘scope.userInfo‘]) { // 已經授權,可以直接調用 getUserInfo 獲取頭像昵稱,不會彈框 wx.getUserInfo({ success: res => { // 可以將 res 發送給後臺解碼出 unionId this.globalData.userInfo = res.userInfo // 由於 getUserInfo 是網絡請求,可能會在 Page.onLoad 之後才返回
// 所以此處加入 callback 以防止這種情況 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) }, globalData: { userInfo: null } })

註冊頁面page代碼:index.js

var app = getApp();
var util = require(‘../../utils/util.js‘);
Page({

  
/** * 頁面的初始數據 */ data: { mooto: ‘這個是第一個程序的測試文字‘, userInfo:{} }, /** * 事件處理函數 */ bindViewTap: function(){ wx.navigateTo({ url: ‘../cake1/cake1‘, }) }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function () { console.log("onload") if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse) { // 由於 getUserInfo 是網絡請求,可能會在 Page.onLoad 之後才返回 // 所以此處加入 callback 以防止這種情況 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在沒有 open-type=getUserInfo 版本的兼容處理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, /** * 生命周期函數--監聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數--監聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數--監聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數--監聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關事件處理函數--監聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數 */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { } })

微信小程序 app註冊小程序+page註冊頁面代碼一