zoukankan      html  css  js  c++  java
  • 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}`)
        }
    本想把日子过成诗,时而简单,时而精致,不料日子却过成了一首歌,时而不靠谱,时而不着调
  • 相关阅读:
    Fix the Package System is Broken error in Ubuntu
    linux源镜像网站
    VS2010快捷键大全
    自定义函数与存储过程的比较
    vbcr,vblf和 vbcrlf之间的区别?
    GridView序号问题
    Outlook2007设置备份账号、联系人和个性签名的方法
    c#.net4.0利用odac连接oracle取数
    无法打开登录所请求的数据库DbName 。登录失败。 用户 'IIS APPPOOL\DefaultAppPool' 登录失败。 的解决方案
    C#开发和调用Web Service
  • 原文地址:https://www.cnblogs.com/chuanq/p/11956442.html
Copyright © 2011-2022 走看看