微信小程序 && 手机号授权登录

手机号授权登录

效果展示

 这里面用的是 uni-app 官方的登录 他支持多端发布

https://zh.uniapp.dcloud.io/api/plugins/login.html#loginicon-default.png?t=N7T8https://zh.uniapp.dcloud.io/api/plugins/login.html#login

下面是代码 

<template>
  <!-- 授权按钮 -->
  <button v-if="!headerAvatar || !getName" class="game" open-type="getPhoneNumber" @getphonenumber="fnlogin">
    <image class="weixinIcon" src="../../../static/login/weixin.png" mode=""></image>
    <text class="buttonTitle">微信用户登录</text>
  </button>
</template>

为了方便手机号的全局使用我们可以在pinia中 把手机号进行一个存储

首先现在 store文件夹下创建一个conter.js文件 里面来存储我们手机号登录的一些相关信息

import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useWxStore = defineStore('counter',
  () => {
    // 微信 手机号存储
    const wxMobile = ref('')

    const saveWxMobile = (encryptedData) => {
      uni.setStorageSync('mobile', encryptedData)
      wxMobile.value = encryptedData
    }

    return { wxMobile, saveWxMobile }
  }, {
    persist: {

      paths: ['count']
    }
  }
)

<script setup>
// 微信----授权登录
// 先导入刚刚-store写好的状态管理
import { useWxStore } from '@/stores/conter.js';
const wxMobile = useWxStore();
// 登录接口
import { wxLogin } from '@/apis/login.js';
const Mobile = ref('');
const Code = ref('');
const fnlogin = (e) => {
  console.log('手机号', e);
  Mobile.value = e.detail.encryptedData;
  Code.value = e.detail.code;

  if (e.detail.errMsg == 'getPhoneNumber:ok') {
    wxMobile.saveWxMobile(e.detail.encryptedData);
    uni.showToast({
      title: '登录成功',
      icon: 'success',
      duration: 2000
    });
  } else {
    uni.showToast({
      title: '取消登录',
      icon: 'none',
      duration: 2000
    });
    return;
  }

  uni.login({
    success: async (res) => {
      console.log('success---res', res);
      if (res.code) {
        //发起网络请求--调接口
        const res = await wxLogin({ code: Code.value });
        console.log('接口--res', res);
        uni.request({
          // url: 'wxLogin',
          data: {
            code: res.code
          },
          method: 'post',
          success: (res) => {
            // console.log('-----success----');
            // console.log(res);
          },
          fail: (err) => {
            // console.log('---error----', err);
          }
        });
        uni.navigateTo({
          url: '/pages/index/index'
        });

        // 获取用户信息
        uni.getUserInfo({
          success: (infoRes) => {
            console.log('用户信息----', infoRes);
            avatar.value = infoRes.userInfo.avatarUrl;
            nickname.value = infoRes.userInfo.nickName;
          },
          fail: (error) => {
            console.log('获取用户信息失败', error);
          }
        });
      } else {
        console.log('登录失败!' + res.errMsg);
      }
    }
  });
};
</script>

  • 42
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值