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

UniappDay04

1.登录模块-小程序快捷登录

在这里插入图片描述

  1. 定义接口,封装
import { http } from '@/utils/http'type loginParams = {code: stringencryptedData: stringiv: string
}
export const postLoginWxMinAPI = (data: loginParams) => {return http({method: 'POST',url: '/login/wxMin',data,})
}
  1. 获取登录凭证和手机号并登录
// 获取code登录凭证
let code = ''
onLoad(async () => {const res = await wx.login()code = res.code
})
// 获取用户手机号码
const onGetphonenumber: UniHelper.ButtonOnGetphonenumber = async (ev) => {const encryptedData = ev.detail!.encryptedData!const iv = ev.detail!.iv!const res = await postLoginWxMinAPI({code,encryptedData,iv,})
}<!-- 小程序端授权登录 --><button class="button phone" @getphonenumber="onGetphonenumber"><text class="icon icon-phone"></text>手机号快捷登录</button>

注意:获取手机号功能针对非个人开发者,且完成认证 的小程序开放

2.模拟快捷登录

  1. 封装模拟手机号,小程序内测版
// 小程序内测版
export const postLoginWxMinSimpleAPI = (phoneNumber: string) => {return http({method: 'POST',url: '/login/wxMin/simple',data: {phoneNumber,},})
}
  1. 模拟手机号快捷登录
// 模拟手机号快捷登录
const postLoginWxMinSimple = async () => {const res = await postLoginWxMinSimpleAPI('13815869691')uni.showToast({icon: 'none',title: '登录成功',})
}

3.保存登录信息

  1. 类型声明
/** 小程序登录 登录用户信息 */
export type LoginResult = {/** 用户ID */id: number/** 头像  */avatar: string/** 账户名  */account: string/** 昵称 */nickname?: string/** 手机号 */mobile: string/** 登录凭证 */token: string
}
  1. 状态管理
// 定义 Store
export const useMemberStore = defineStore('member',() => {// 会员信息const profile = ref<LoginResult>()// 保存会员信息,登录时使用const setProfile = (val: LoginResult) => {profile.value = val}
)
  1. 成功提示
  2. 页面跳转
// 获取用户手机号码
const onGetphonenumber: UniHelper.ButtonOnGetphonenumber = async (ev) => {const encryptedData = ev.detail!.encryptedData!const iv = ev.detail!.iv!const res = await postLoginWxMinAPI({code,encryptedData,iv,})loginSuccess(res.result)
}
// 模拟手机号快捷登录
const postLoginWxMinSimple = async () => {const res = await postLoginWxMinSimpleAPI('13815869691')loginSuccess(res.result)
}const loginSuccess = (profile: LoginResult) => {// 保存会员信息const memberStore = useMemberStore()memberStore.setProfile(profile)// 成功提示uni.showToast({icon: 'success',title: '登录成功',})setTimeout(() => {// 页面跳转//小程序开发页面可以分为tabbar页面和普通页面,普通页面可以用navigateTo,tabbar页面要用switchtabuni.switchTab({ url: '/pages/my/my' })}, 500)
}

小程序开发页面可以分为tabbar页面和普通页面,普通页面可以用navigateTo,tabbar页面要用switchtab

4.会员信息展示

  1. 静态结构
  2. 自定义导航
	{"path": "pages/my/my","style": {"navigationStyle": "custom","navigationBarTextStyle": "white","navigationBarTitleText": "我的"}},
  1. 渲染会员信息
const memberStore = useMemberStore()<!-- 情况1:已登录 --><view class="overview" v-if="memberStore.profile"><navigator url="/pagesMember/profile/profile" hover-class="none"><image class="avatar" mode="aspectFill" :src="memberStore.profile.avatar"></image></navigator><view class="meta"><view class="nickname">{{ memberStore.profile.nickname || memberStore.profile.account  }}</view><navigator class="extra" url="/pagesMember/profile/profile" hover-class="none"><text class="update">更新头像昵称</text></navigator></view></view>
http://www.lryc.cn/news/602095.html

相关文章:

  • 【跳跃游戏】
  • Vue、微信小程序、Uniapp 面试题整理最新整合版
  • Entity Framework Core (EF Core) 中Database
  • uniapp,uview icon加载太慢了,老是显示叉叉,将远程加载改到本地加载。
  • 设计模式(二十三)行为型:模板方法模式详解
  • 常用设计模式系列(十四)—模板方法模式
  • 开源智能体-JoyAgent集成ollama私有化模型
  • C#与C++交互开发系列(二十六):构建跨语言共享缓存,实现键值对读写与数据同步(实践方案)
  • 基于百度 iframe 框架与语音解析服务的数字人交互系统实现
  • 元宇宙工厂前端新形态:Three.js与WebGL实现3D产线交互的轻量化之路
  • Python系统交互库全解析
  • CentOS 7 安装 dnsmasq 解决nginx无法指定dns的问题
  • 新能源行业B端极简设计:碳中和目标下的交互轻量化实践
  • GitLab 18.2 发布几十项与 DevSecOps 有关的功能,可升级体验【三】
  • Windows 系统分辨率切换** 与 **Qt4 无边框窗口管理机制** 的交互
  • 全面解析MySQL(4)——三大范式与联合查询实例教程
  • Deep Learning_ Foundations and Concepts-Springer (2024)【拜读】前向编码器20章
  • 【CSS】设置表格表头固定
  • 深度学习----视觉里程计
  • 工业场景工服识别准确率↑32%!陌讯多特征融合算法实战解析
  • STM32 usb HOST audio USB 音频设备 放音乐
  • Pandas 里的分箱操作
  • 负载均衡集群HAproxy
  • MCP工作原理
  • Java-泛型类的定义与使用
  • 浅谈面试中的递归算法
  • 【Linux】编辑器vim和编译器gcc/g++
  • 解析分区、挂载与块设备:Linux 存储管理核心命令详解
  • 近屿智能正式发布AI得贤招聘官的AI面试官智能体6.3版本:交付替代人类面试官的打分结果
  • 零基础学习性能测试第九章:全链路追踪-项目实操