m0_59371898 2021-06-16 19:20 采纳率: 100%
浏览 68
已采纳

微信授权登录 获取用户信息

授权登录后获取微信用户信

能否获取到用户通讯录信息

 

  • 写回答

5条回答 默认 最新

  • 王大师王文峰 企业官方账号 2021-06-17 09:47
    关注

    可以,代码如下,望采纳,谢谢!

    <template>
      <view class="authorize-contianer">
        <div class="auth-item" @click="login">请微信授权</div>
        <div class="btn-authorize">
          <button open-type="getUserInfo" @getuserinfo="bindGetUserInfo">确认授权</button>
        </div>
        <div v-if="cancelAuth" class="refuse">请授权,否则无法进入哦~</div>
    
          <div v-if="isPone" class="alert" >
          <div class="auth">
            <div class="apply">
              <span class="">需要获取您的手机权限</span>
              <p>(请开启手机权限)</p>
            </div>
            <button class="confirm" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">确认</button>
          </div>
        </div>
      </view>
    </template>
    <script>
    import { getToken } from '@/api/main'
    export default {
      data () {
        return {
          cancelAuth: false,//判断用户是否拒绝获取信息
          newType: '',
          path: '../home/main',
          shareDetail: {},
          code: '',
          isPone: false  //用于显示获取手机权限窗口
    
        }
      },
      methods: {
          //获取手机号
        getPhoneNumber(e) {
          console.log(e)
          if (e.mp.detail.errMsg==="getPhoneNumber:ok") {
            this.isPone = false
            this.params.iv = e.mp.detail.iv
            this.params.encryptedData = e.mp.detail.encryptedData
            console.log(this.params)
            this.getTokenAsync(this.params)
          }
        },
          //调用wx.login获取code
        login() {
          let me = this
          let obj = wx.getStorageSync('USER_INFO')
          console.log(obj, 'USER_INFOUSER_INFO')
          if (obj) {
            wx.switchTab({ url: this.path })
          } else {
            wx.login({
              success (res) {
                if (res.code) {
                  me.code = res.code
                }
              }
            })
          }
        },
        // 请求后获取token
        async getTokenAsync(params) {
          let res = await getToken(params)
          if (res.success) {
            wx.setStorageSync('USER_INFO', JSON.stringify(res.data))
          	wx.switchTab({ url: this.path })
          } else {
            console.log("获取失败")
          }
        },
        //  调用用户信息授权页面
        bindGetUserInfo(e) {
          if (!this.cancelAuth) {
            this.isPone = true
          }
          wx.setStorageSync('USER_MSG', JSON.stringify(e.mp.detail.userInfo))
          console.log(e)
          if (e.mp.detail.userInfo) {
            this.params = {
              code: this.code,
              name: e.mp.detail.userInfo.nickName,
              userIcon: e.mp.detail.userInfo.avatarUrl,
            }
          } else {
            // 用户按了拒绝按钮
            this.cancelAuth = true
          }
        }
      },
      onShow () {
        this.path ='../home/main'
        this.login()
      }
    }
    </script>
    <style lang="scss">
    .authorize-contianer {
      height: 100%;
       background: #fff;
      background-size:100% 100%;
      text-align: center;
      .authorize-icon {
        padding-top: 254rpx;
        width: 200rpx;
        height: 200rpx;
        display: block;
        margin: 0 auto;
        padding-bottom: 10rpx;
      }
      .auth-item {
        margin-top:92rpx;
        font-size: 36rpx;
        line-height:40rpx;
        color:rgb(255,145,37);
        font-weight: bold;
      }
      .refuse{
        padding:18rpx 0;
        font-size:24rpx;
        width:80%;
        margin:0rpx auto;
        background:#000000d1;
        color:#fff;
        margin-top:140rpx;
        border-radius:6rpx;
      }
      .btn-authorize {
        margin: 0 auto;
        margin-top:160rpx;
        button{
          width: 80%;
          background:-webkit-linear-gradient(left,rgb(255,152,22),rgb(255,120,17));
          background:-o-linear-gradient(right,rgb(255,152,22),rgb(255,120,17));
          background:-moz-linear-gradient(right,rgb(255,152,22),rgb(255,120,17));
          background:linear-gradient(to right,rgb(255,152,22),rgb(255,120,17));
          border-radius: 40rpx;
          color:rgb(255,255,255);
          font-size: 34rpx;
          font-weight: bold;
        }
      }
      .alert{
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        overflow: hidden;
        outline: 0;
        -webkit-overflow-scrolling: touch;
        background-color: rgb(0, 0, 0);  
        filter: alpha(opacity=60);  
        background-color: rgba(0, 0, 0, 0.6); 
        z-index: 9999;
        .auth{
          position: absolute;
          top: 50%;
          left: 50%;
          width: 80%;
          height: 340rpx;
          border-radius: 20rpx;
          transform:translate(-50%,-50%);
          background: #fff;
          display: flex;
          flex-direction: column;
          .apply{
            margin: 0rpx auto;
            padding: 60rpx 40rpx;
            flex:1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-between;
            font-size: 34rpx;
            color:rgb(34,34,34);
          
            p{
              font-size:30rpx
            }
          }
          .confirm{
            text-align: center;
            height: 100rpx;
            width: 100%;
            line-height: 100rpx;
            font-size: 34rpx;
            color:rgb(255,145,37);
          }
        }
      }
    }
    </style>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持