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

手机号授权登录

效果展示

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

https://zh.uniapp.dcloud.io/api/plugins/login.html#loginhttps://zh.uniapp.dcloud.io/api/plugins/login.html#login

下面是代码

html 复制代码
<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文件 里面来存储我们手机号登录的一些相关信息

html 复制代码
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']
    }
  }
)
javascript 复制代码
<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>
相关推荐
icebreaker3 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker3 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
大米饭消灭者3 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
FliPPeDround4 天前
Vitest Environment UniApp:让 uni-app E2E 测试变得前所未有的简单
微信小程序·e2e·前端工程化
FliPPeDround4 天前
微信小程序自动化的 AI 新时代:wechat-devtools-mcp 智能方案
微信小程序·ai编程·mcp
吴声子夜歌4 天前
小程序——布局示例
小程序
码云数智-大飞4 天前
如何创建自己的小程序,码云数智与有赞平台对比
微信小程序
luffy54594 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序
Slow菜鸟4 天前
微信小程序开发(二)目录结构完全指南
微信小程序·小程序
攀登的牵牛花4 天前
给女朋友写了个轻断食小程序:去老丈人家也是先动筷了
前端·微信小程序