Q: 微信小程序登录

这里使用的mpvue

第一步组件DOM部分

/pages/index
<button class="app_btn" open-type="getUserInfo" @getuserinfo="getUserInfo" type="primary"> 欢迎使用XXX </button>

第二步登录方法

/pages/index
getUserInfo() {
      let _this = this;
      wx.login({
        success: async function(res) {
          if (res.code) {
            //发起网络请求
            let ares = await execteGet('/v1/inner/wechat/getOpenId',{code: res.code})
            if(ares){
              _this.operId = ares;
              wx.setStorageSync('operId', ares);
              // 存在groupId及通过组分享进来的,直接加入对应组
              console.log('开始加入群组----:'+ _this.groupId)
              if(_this.groupId){
                let join_data = { 
                  groupId: _this.groupId,
                  "weChatId": ares
                }
                let jres = await exectePost('/v1/inner/group/joinGroup', join_data)
                console.log('加入群组参数---:' + JSON.stringify(join_data))
                console.log('加入群组状态----:'+jres)
              }
              // 获取用户信息
              wx.getSetting({
                success(cres) {
                  if (cres.authSetting['scope.userInfo']) {
                    console.log("已授权=====")
                    // 已经授权,可以直接调用 getUserInfo 获取头像昵称
                    wx.getUserInfo({
                      success(zres) {
                        console.log("获取用户信息成功", zres);
                        _this.globalData.userInfo = zres.userInfo;
                        if(!wx.getStorageSync('nickName')){
                          wx.setStorageSync('nickName', zres.userInfo.nickName)
                        }
                        // 存用户数据
                        let sdata = {
                          address:`${zres.userInfo.country}-${zres.userInfo.province}-${zres.userInfo.city}`,
                          name: zres.userInfo.nickName,
                          sex: zres.userInfo.gender,
                          weChatId: _this.operId,
                          portrait: zres.userInfo.avatarUrl
                        }
                        exectePost('/v1/inner/task/loginOrInsertUser', sdata).then(data =>{
                          gotabbar()
                        })
                      },
                      fail(res) {
                        console.log("获取用户信息失败", res)
                      }
                    })
                  } else {
                    console.log("未授权=====")
                    that.showSettingToast("请授权")
                  }
                }
              })
            }else{
              wx.showToast({
                title: '获取operid失败!',
                icon: 'loading',
                duration: 1500
              })
            }
          } else {
            console.log('登录失败!' + res.errMsg)
          }
        }
      })      
    },

    // 打开权限设置页提示框
    showSettingToast(e) {
      wx.showModal({
        title: '温馨提示提示!',
        confirmText: '去设置',
        showCancel: false,
        content: e,
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../setting/main',
            })
          }
        }
      })
    }

第三步编写授权页面

/pages/setting
<button class="primary_btn" type="primary" open-type="openSetting">去设置开启权限</button>

 随便记录下分享

/pages/sharePage
<button type="primary" open-type='share'>直接分享</button>
//与methods方法同级
onShareAppMessage: function(options) {
    console.log(JSON.stringify('分享来源'+ JSON.stringify(options)))
    return {
      title: '邀请组二维码',
      path: `/pages/index/main?groupId=${this.$root.$mp.query.groupid}`,//被分享人点击进来的界面
      // imageUrl: '../../static/images/user.png',
      success: function (res) {
        console.log(res)
        wx.getShareInfo({
          shareTicket: res.shareTickets[0],
          success: function (res) { console.log(res) },
          fail: function (res) { console.log(res) },
          complete: function (res) { console.log(res) }
        })
      },
      fail: function (res) {
        console.log(res)
      }
    }
    console.log('分享参数' + `/pages/index/main?groupId=${this.$root.$mp.query.groupid}`)
    }

 

posted @ 2019-11-29 11:01  qinhuansky  阅读(305)  评论(0编辑  收藏  举报