当前位置: 首页 > article >正文

uniapp用户登录及获取用户信息(头像昵称)

低版本情况

  • 微信开发者工具的基础库版本要调到2.27版本以下,能够直接申请用户权限获取用户信息,但是会仅限于开发者调试,在真机测试或已上传的小程序在手机上就不能获取
  • 以上的原因是微信小程序wx.getUserProfilewx.getUserInfo 这两个获取用户信息的接口都已经停用了,取而代之的是:头像昵称填写能力
    在这里插入图片描述
    在这里插入图片描述
  • 所以我们使用高版本的同时要使用获取头像昵称的新方法:
    button按钮中 open-type=‘chooseAvatar’ 和chooseavatar方法结合 image组合实现选择头像
    input输入框type="nickname"实现选择微信昵称

以下代码展示:

<template>
  <view class="login-container">
    <view class="user-info">
      <!-- 圆形的头像按钮 -->
      <button
        class="avatar-wrapper"
        @chooseavatar="onchooseAvatar"
        open-type="chooseAvatar"
      >
        <image class="avatar" :src="avatarUrl" />
      </button>

      <!-- 输入框,获取用户昵称 -->
        class="nickname-input"
        type="nickname"
        v-model="nickname"
        placeholder="获取或输入昵称111"
      />
      <!-- 登录按钮 -->
      <button class="login-btn" @click="handleLogin" v-if="nickname">
        登录
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { useFormStore } from "@/store/formStore.js";
import { storeToRefs } from "pinia";
import config from "@/config/config.js"; // 导入配置文件

const avatarUrl = ref("");
const nickname = ref(""); // 存储昵称
const userInfo = ref(null);
const logininfo = ref("");

// 选择头像
const onchooseAvatar = (e) => {
  avatarUrl.value = e.detail.avatarUrl;
};

// 登录操作
const handleLogin = () => {
  uni.login({
    provider: "weixin",
    success: function (loginRes) {
      const code = loginRes.code;
      uni.showLoading({
        title: "登录成功",
        mask: true, // 使遮罩层不可点击
      });
      login(code); // 传递 code 调用登录接口
    },
    fail: function (error) {
      console.log("登录失败: ", error);
      uni.showLoading({
        title: `登录失败`,
        mask: true, // 使遮罩层不可点击
      });
    },
  });
};

// 登录请求
const login = (code) => {
  logininfo.value = code;
  const ip = uni.getStorageSync("ip");
  uni.request({
    url: `${config.baseUrl}/api/auth/login`,
    method: "POST",
    data: {
      jsCode: code,
    },
    header: {
      ip: ip,
    },
    success: (res) => {
      console.log("登录成功", res.data);
      if (res.data.code === 200) {
        // 登录成功,跳转到首页
        uni.navigateTo({
          url: `/pages/index/index`,
        });
      }
      const token = res.data.data.Authorization;
      uni.setStorage({
        key: "userToken",
        data: token,
        success: function () {
          console.log("数据存储成功");
        },
        fail: function (err) {
          console.log("数据存储失败", err);
        },
      });
    },
    fail: (err) => {
      console.error("登录失败", err);
    },
    complete: () => {
      uni.hideLoading(); // 隐藏 loading
    },
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
}

.avatar-wrapper {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 20px;
  border: 2px solid #e2e2e2;
  background-color: #e2e2e2;
}

.avatar {
  width: 100px;
  height: 100px;
  margin-left: -16px;
  object-fit: cover;
}

.nickname-input {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
  color: #333;
  outline: none;
  background-color: #fff;
}

.nickname-input::placeholder {
  color: #888;
}

.get-info-btn,
.login-btn {
  width: 100%;
  padding: 12px;
  margin: 15px 0;
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.get-info-btn:hover,
.login-btn:hover {
  background-color: #005bb5;
}

.get-info-btn {
  background-color: #34c759;
}

.get-info-btn:hover {
  background-color: #28a745;
}

button:disabled {
  background-color: #ddd;
  cursor: not-allowed;
}
</style>

效果图
在这里插入图片描述


http://www.kler.cn/a/612910.html

相关文章:

  • 全国职业技能大赛_网络安全_中职A模块解析
  • spring security的过滤器链
  • MySQL-事务与锁
  • skynet.crypt 使用详解
  • vllm+openwebui,玩转私有化AI
  • 蓝桥与力扣刷题(蓝桥 最少刷题数)
  • python力扣73.矩阵置零
  • 卷积神经网络输入通道和输出通道的确定
  • JVM 面经
  • 【vLLM 学习】快速入门
  • Windows .gitignore文件不生效的情况排查
  • 板端ros2 VM ubuntu 虚拟机之间通信
  • Java 基本数据类型 vs 包装类(引用数据类型)
  • flink 分组窗口聚合 与 窗口表值函数聚合 的区别
  • 基于飞腾FT2000+服务器主板与DeepSeek大模型的国产化AI算力探索
  • 典范硬币系统(Canonical Coin System)→ 贪心算法
  • React19源码系列之Hooks(useRef)
  • 基于DrissionPage的TB商品信息采集与可视化分析
  • 深度解析Spring Boot可执行JAR的构建与启动机制
  • ubuntu22.04 ROS2humble 路径文件