小程序和公衆號openID綁定

通過小程序登錄獲取小程序openID

<button hidden="{{is_login}}" class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo" style='width:100px'>
          登錄</button>
bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用戶按了允許授權按鈕
      var that = this;
      //插入登錄的用戶的相關信息到數據庫
      var openid = getApp().globalData.openid;
      var session = getApp().globalData.session
      getApp().globalData.userInfo = e.detail.userInfo;
      //通過wx.login獲取code  想要獲取用戶信息,必須登錄
      wx.login({
        success:  function(res){
          if(res.code){
                  var code = res.code;
                  // 獲取 encryptedData  iv
                  wx.getUserInfo({
                    withCredentials: true,
                    success: function(res2){
                      //請求自己的登錄接口
                        wx.request({
                          url: config.api_base_url + 'login',
                          data: {
                            userinfo: e.detail.userInfo,
                            openid: openid,
                            encryptedData:res2.encryptedData,
                            iv:res2.iv,
                            session:session
                          },
                          header: {
                            'content-type': 'application/json' // 默認值
                          },
                          method: 'post',
                          success(res) {
                            if (res.data.result == 1) {
                              wx.setStorageSync('user', res.data.msg);
                              that.onLoad();
                              that.setData({
                                is_login:true
                              })

                            } else {
                              console.log("寫入失敗")
                            }
                          }
                        })
                    }
                  })
          }else{
            console.log('獲取用戶登錄態失敗!'+res.errMsg);
          }
        }
      })

    return ;

      //授權成功後,跳轉進入小程序首頁
    } else {
      //用戶按了拒絕按鈕
      wx.showModal({
        title: '警告',
        content: '您點擊了拒絕授權,將無法進入小程序,請授權之後再進入!!!',
        showCancel: false,
        confirmText: '返回授權',
        success: function (res) {
          if (res.confirm) {
            console.log('用戶點擊了“返回授權”')
          }
        }
      })
    }
  },

小程序客服自定義回覆設置

開發->開發設置->找到消息推送,並設置

image-20200706200252305

image-20200706200342415

小程序點擊打開客服消息

image-20200706200513848

通過發送消息,生成關注公衆號帶參數二維碼

然後發送給用戶

用戶通過掃描二維碼關注公衆號,從而實現小程序和公衆號的綁定

image-20200706200823642

image-20200706200914473

image-20200706200949666

高顏值後臺管理系統免費使用 ### 子楓後臺管理系統 ###,可在寶塔面板直接安裝

歡迎關注我的公衆號:子楓的奇妙世界,獲得獨家整理的學習資源和日常乾貨推送。 如果您對我的其他專題內容感興趣,直達我的個人博客:www.wangmingchang.com

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