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

手机号授权登录

效果展示

这里面用的是 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>
相关推荐
计算机毕设指导65 分钟前
基于微信小程序的智能停车场管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
2501_9339072135 分钟前
如何选择西安优质小程序开发服务与本凡码农合作?
科技·微信小程序·小程序
说私域1 小时前
破局互联网产品开发困境:开源AI智能名片链动2+1模式S2B2C商城小程序的实践与启示
人工智能·小程序·开源·私域运营
宁夏雨科网16 小时前
文具办公用品小程序商城,开发一个难吗
小程序·商城小程序·文具小程序·文具商城
说私域1 天前
开源链动2+1模式商城小程序在深度分销数字化转型中的应用研究
人工智能·小程序·开源·流量运营·私域运营
咖啡の猫1 天前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
咖啡の猫1 天前
微信小程序全局数据共享
微信小程序·小程序
桐溪漂流1 天前
微信小程序cli脚本预览上传
微信小程序·小程序
咖啡の猫1 天前
微信小程序使用 npm 包
微信小程序·小程序·npm
说私域1 天前
开源链动2+1模式商城小程序的营销技术与私域运营策略研究
人工智能·小程序·开源·流量运营·私域运营