小程序 拉取登錄授權

 

小程序在某次更新後,已經不支持自動拉取權限,而需要手動去獲取. 
以一下是代碼

1) 彈窗提示框 

(authorizeAlert  放在 根目錄 的 comm[名字隨意] 下的 AuthorizeAlert文件下)

<!--comm/AuthorizeAlert/authorizeAlert.wxml-->
<view class='wx_dialog_container' hidden="{{!isShow}}">
  <view class='wx-mask'></view>
  <view class='wx-dialog'>
    <view class='wx-dialog-title'>{{ title }}</view>
    <view wx:if="{{content!='' && content != null}}" class='wx-dialog-content'>{{ content }}</view>
    <view class='wx-dialog-footer'>
      <!-- <view class='wx-dialog-btn' catchtap='_cancelEvent'>{{ cancelText }}</view> -->
      <button class='wx-dialog-certainBtn' open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 確定 </button>
    </view>
  </view>
</view>
// comm/AuthorizeAlert/authorizeAlert
//獲取應用實例
const app = getApp()

Component({
  options: {
    multipleSlots: true // 在組件定義時的選項中啓用多slot支持
  },
  /**
   * 組件的屬性列表
   * 用於組件自定義設置
   */
  properties: {
    // 彈窗標題
    title: {            // 屬性名
      type: String,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      value: '標題'     // 屬性初始值(可選),如果未指定則會根據類型選擇一個
    },
    // 彈窗內容
    content: {
      type: String,
      value: '彈窗內容'
    },
    // 彈窗取消按鈕文字
    cancelText: {
      type: String,
      value: '取消'
    },
    // 彈窗確認按鈕文字
    confirmText: {
      type: String,
      value: '確定'
    }
  },

  /**
   * 私有數據,組件的初始數據
   * 可用於模版渲染
   */
  data: {
    // 彈窗顯示控制
    isShow: false
  },

  /**
   * 組件的方法列表
   * 更新屬性和數據的方法與更新頁面數據的方法類似
   */
  methods: {
    /*
     * 公有方法
     */

    //隱藏彈框
    hideDialog() {
      this.setData({
        isShow: !this.data.isShow
      })
    },
    //展示彈框
    showDialog() {
      this.setData({
        isShow: !this.data.isShow
      })
    },
    /*
    * 內部私有方法建議以下劃線開頭
    * triggerEvent 用於觸發事件
    */
    _cancelEvent() {
      //觸發取消回調
      this.triggerEvent("cancelEvent")
    },
    _confirmEvent() {
      //觸發成功回調
      this.triggerEvent("confirmEvent");
    },
    getUserInfo(e){
      console.log("getUserInfo")
      this.hideDialog()
      //觸發成功回調
      this.triggerEvent("getWxUserInfo",{});
      // 自己後續的邏輯

    }
  }
})
//comm/AuthorizeAlert/authorizeAlert.json

{
  "component": true,
  "usingComponents": {}
}
/* comm/AuthorizeAlert/authorizeAlert.wxss */

.wx-mask {
  position: fixed;
  z-index: 1000;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

.wx-dialog {
  position: fixed;
  z-index: 5000;
  width: 80%;
  max-width: 600rpx;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background-color: #fff;
  text-align: center;
  border-radius: 8px;
  overflow: hidden;
}

.wx-dialog-title {
  font-size: 18px;
  padding: 15px 15px 15px;
}

.wx-dialog-content {
  padding: 15px 15px 5px;
  min-height: 40px;
  font-size: 16px;
  line-height: 1.3;
  word-wrap: break-word;
  word-break: break-all;
  color: #999;
}

.wx-dialog-footer {
  display: flex;
  align-items: center;
  position: relative;
  line-height: 45px;
  font-size: 17px;
}

.wx-dialog-footer::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1px solid #d5d5d6;
  color: #d5d5d6;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
}

.wx-dialog-btn {
  display: block;
  -webkit-flex: 1;
  flex: 1;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  position: relative;
}

.wx-dialog-footer .wx-dialog-btn:nth-of-type(1) {
  color: blue;
}

.wx-dialog-footer .wx-dialog-btn:nth-of-type(2) {
  color: blue;
}

.wx-dialog-footer .wx-dialog-btn:nth-of-type(2):after {
  content: " ";
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  bottom: 0;
  border-left: 1px solid #d5d5d6;
  color: #d5d5d6;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  -webkit-transform: scaleX(0.5);
  transform: scaleX(0.5);
}

.wx-dialog-certainBtn {
  background-color: white;
  width: 110%;
  border: 0px solid white;
}

2) 在首頁定義 彈框 及 相關方法

設 首頁 爲 menu.wxml

<!-- menu.wxml -->


<!-- 在頁面底部加入 -->

<authorizeAlert id='authorizeAlert' 
          title='獲取用戶頭像、暱稱' 
          content='如需正常使用小程序功能,請點擊確定進行授權獲取您的用戶頭像、暱稱' 
          cancelText='取消' 
          confirmText='確定'
          bind:cancelEvent="_cancelEvent"  
          bind:confirmEvent="_confirmEvent">
  </authorizeAlert>

menu.json 內容

{
  "usingComponents": {
    "authorizeAlert": "/comm/AuthorizeAlert/authorizeAlert"
  }
}

 menu.js 內容

 /**
   * 生命週期函數--監聽頁面加載
   */
  onLoad: function(options) {
    //獲得dialog組件
    this.authorizeAlert = this.selectComponent("#authorizeAlert");
    getUserInfo();
  },
  getUserInfo(){
    var self = this;
    wx.getUserInfo({
      success: res => {
        console.log("獲取用戶信息成功")

      },
      fail: res => {
        console.log("微信用戶信息獲取失敗")
        console.log(res)
        if (wx.canIUse('button.open-type.getUserInfo')) {
          self.showDialog()
        } else {
          wx.showToast({
            title: '您當前版本過低',
            icon: 'none'
          })
        }
      }
    })
  },
  showDialog: function() {
    this.authorizeAlert.showDialog();
  },

 

 

以上 是拉取登錄權限的代碼.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章