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

手机号授权登录

效果展示

 这里面用的是 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>

相关推荐

  1. 程序 手机授权登录 偶尔后端解密失败

    2024-05-04 10:50:01       59 阅读
  2. 程序实现用户手机授权

    2024-05-04 10:50:01       33 阅读
  3. 程序手机号码授权登录

    2024-05-04 10:50:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 10:50:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 10:50:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 10:50:01       82 阅读
  4. Python语言-面向对象

    2024-05-04 10:50:01       91 阅读

热门阅读

  1. 【无标题】

    2024-05-04 10:50:01       28 阅读
  2. docker常用容器启动命令

    2024-05-04 10:50:01       28 阅读
  3. Python进阶之-ast使用详解

    2024-05-04 10:50:01       30 阅读
  4. 前端 TS

    2024-05-04 10:50:01       31 阅读
  5. 时间复杂度和空间复杂度

    2024-05-04 10:50:01       34 阅读
  6. C# do...while循环

    2024-05-04 10:50:01       34 阅读
  7. 富格林:了解黑幕套路正规方法预防

    2024-05-04 10:50:01       29 阅读
  8. 算法--回溯法

    2024-05-04 10:50:01       26 阅读
  9. 第十三节:Vben Admin实战-系统管理之菜单管理

    2024-05-04 10:50:01       35 阅读
  10. 动态sql

    动态sql

    2024-05-04 10:50:01      32 阅读
  11. vue2 + antvx6 实现流程图功能

    2024-05-04 10:50:01       26 阅读
  12. 如何判断嵌入式平台OpenCV在使用硬件编解码器?

    2024-05-04 10:50:01       23 阅读