zoukankan      html  css  js  c++  java
  • 微信小程序 app注册小程序+page注册页面代码一

    注册小程序代码: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 () {
        
      }
    })
  • 相关阅读:
    PHP培训教程 PHP里10个鲜为人知但却非常有用的函数
    跟我学Spring Boot(一)创建Spring Boot 项目
    android 4.0 webview 无法播放视频
    vs2008 安装部署 启动项
    android 使用webview 加载网页
    hbase 无法打开60010网页
    oracle 提示没有监听
    hbase 基本命令
    hbase 单机版安装
    win7 64位远程连接oracle11g64位
  • 原文地址:https://www.cnblogs.com/thongyan/p/7520744.html
Copyright © 2011-2022 走看看