微信小程序授权页面,进入小程序如果没授权跳转到授权页面,授权后跳转到首页,如果用户点拒绝下次进入小程序还是能跳转到授权页面,授权页面如下 作者微信/qq:1445579000  接各类软件开发及私活,只有你想不到的,没有我做不到的

1.app.js  中的 onLaunch或onShow中加如下代码,如果没授权跳转到授权页面

// 获取用户信息
    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)
        }else{
          // 未授权,跳转到授权页面
          wx.reLaunch({
            url: '/pages/auth/auth',

2.创建一个授权页面 auth.wxml 授权页面结构

<view class="auth">
  <image src="https://res.wx.qq.com/wxopenres/htmledition/images/favicon32f740.ico" class="img" mode="aspectFill"></image>
  <view class="title">微信授权页面</view>
  <view class="describe">此页面是微信授权页面,点击下方按钮弹出授权或跳转页面</view>
  <button class="btn" open-type='getUserInfo' wx:if="{{canIUse}}" bindgetuserinfo='onAuth'>点击微信授权</button>
  <navigator wx:if="{{!canIUse}}" class="btn" url="/pages/auth/auth" open-type="reLaunch" hover-class="other-navigator-hover">已经授权点击调转</navigator>
</view>

3.auth.wxss  授权页面样式

/* 开始 */
page {
  height: 100%;
  display: table;
.auth {
  margin-top: 0;
  text-align: center;
  display: table-cell;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  padding: 100rpx;
  vertical-align: middle;
.img {
  border-radius: 50%;
  border: 1px solid #fff;
  background-color: #fff;
  margin: 0 0 60rpx;
  display: inline-block;
  width: 200rpx;
  height: 200rpx;
  line-height: 0;
.title {
  display: inline-block;
  width: 100%;
  margin: 0 0 60rpx;
.describe {
  color: #a7aaa9;
  font-size: 26rpx;
  margin: 0 0 60rpx;
  border-radius: 50%;
  text-align: center;
  display: inline-block;
  width: 100%;
.btn {
  padding: 0 60rpx;
  background-color: #19be6b;
  margin: 20rpx 0 200rpx;
  text-align: center;
  vertical-align: middle;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none;
  white-space: nowrap;
  user-select: none;
  font-size: 14px;
  border: 0 !important;
  position: relative;
  text-decoration: none;
  height: 44px;
  line-height: 44px;
  box-shadow: inset 0 0 0 1px #19be6b;  
  background: #fff !important;
  color: #19be6b !important;
  display: inline-block;
  border-radius: 10rpx;

4.auth.js  授权页面js,点击授权后跳转到首页

var app = getApp();
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  onAuth() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.reLaunch({
            url: '../index/index',
                                    微信小程序是一种基于微信平台开发的应用程序,它具有轻量、快速、灵活等特点。微信登录和微信授权小程序中常见的功能,通过微信登录可以方便地获取用户的基本信息,实现用户身份认证;而微信授权可以获取用户的更多信息,实现个性化的功能。接下来,我将以一个示例来演示如何实现微信登录和微信授权功能。
                                    小程序授权登录获得用户投头像后,做一些页面跳转功能,但是如果需要再次跳转的话,小程序就会有回到登录前的状态,这样体验感会很差。要想不用再次登录,也不难,只需要判断userInfo是否还存有信息就可以了。
                                    第三步访问不到的原因:spring security oauth2  授权码模式流程2.跳到登录页面,进行登录。3.登录成功跳到授权页面。4.授权成功访问回调地址。
                                    正常情况下我们直接使用 location.reload 方法来刷新。
    function realod() {
        var { search, href } = window.location;
        href = href.replace(/&?t_reload=(\d+)/g, '')
        window.location.href = href ...
                                    最近在弄一个小程序项目,处理用户授权地理位置信息的候,发现用户在拒绝一次授权弹框之后,每次获取地理位置信息都是直接进入fail回调,并没有出现第二次弹框。
网上百度了一会,大致上解决办法是引导用户前往权限设置页面。基于这种办法,我将代码逻辑进行封装,具体如下:
判断用户是否已经授权
hasLocation: function () {
        //第一次小程序会弹出一个授权框,拒绝之后不会再次弹出,直接进入fail回调,需引导用户进入设置页面打开授权
        // 利用promise
一开始在项目中总是发现微信登录只在第一次给用户授权后面微信登录后就不显示授权登录的界面。这样就导致微信用户无法切换,无法进行新用户的授权。查了很久才找到解决的方法:
UMWXHandler.setRefreshTokenAvailable(false);
这里设置为false,授权页面可以再次出现。
设置完这个后每次