zoukankan      html  css  js  c++  java
  • 微信小程序-获取用户信息(getUserInfo)

    当小程序抹杀掉这个接口的时候,多少人心凉了。。

     

    作为一个初级web前端开发,我是更加懵逼,小程序员跑路了。。。

    当时以及现在用的办法就是:

    1.增加一个登陆或授权页

    2.上线以后自动获取

    3.增加一个模态框

     

    现在说说第三种吧

     

    index.wxml

     

     <view class='show-author' style='display:{{ismask}}'>
        <view class='show-author-title'>
          <button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" bindtap='closeHide'>授权登录</button>
        </view>
      </view>
    View Code

    index.wxss


    .show-author {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      z-index: 99;
      background: #000;
      opacity: .7;
    }
    
    .show-author-title {
      position: absolute;
      top: 50%;
      left: 22.3%;
      color: #fff;
    }
    
    .show-author-title button {
      width: 200%;
      height: 100rpx;
      background-color: #ff0;
      line-height: 100rpx;
    }
    View Code

     

     

    index.js

     

    closeHide:function(e){
        this.setData({
          ismask: 'none'
        });
      },
    View Code

     

     

    index.js onload生命周期

     1  wx.getSetting({
     2       success: res => {
     3         if (res.authSetting['scope.userInfo']) {
     4           // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
     5           wx.getUserInfo({
     6             success: res => {
     7               this.setData({
     8                 ismask: 'none'
     9               });
    10             }
    11           })
    12         }else{
    13           this.setData({
    14             ismask: 'block'
    15           });
    16         }
    17       }
    18     });
    View Code

    很简单吧? 载入页面时判断是否带有所有权限,否则就弹出遮罩层! 通过点击按钮,再获取用户信息(不过第一次进入是要点2次。。 的确麻烦)

    所以现在开发都是线下测试增加button获取用户信息,线上去掉button(用以前的老代码还是会自动获取用户信息)

     

  • 相关阅读:
    fedora 20 安裝 及 配置桌面環境
    2014上半年—Linux操作系统—嵌入式开发—中考
    【hdu 1864】最大报销额
    【hdu 1067】Gap
    【t055】成绩统计
    【b094&&z14】靶形数独
    【z06】观光公交
    【u213&&t037】修剪花卉
    【t062】最厉害的机器人
    【t075】郁闷的记者
  • 原文地址:https://www.cnblogs.com/cisum/p/9231076.html
Copyright © 2011-2022 走看看