uni-app 登入功能 vuex使用
目录
功能介绍
vuex 本地缓存 对用户信息进行记入 请求封装 登入页面 个人页面展示
运行效果
未登入状态
点击头像部分即可跳登入页面
登入页面
进行登入完后
代码演示
功能结构
请求封装 详细文章
部分api.ts 关于 用户登入接口
-
//登入接口
-
interface LoginData {
-
userTelephone: string,
-
userPassword: string
-
}
-
export function login(data: LoginData) {
-
return request({
-
url: "/user/userLoginByPassword",
-
method: "post",
-
data
-
})
-
}
store index.ts
-
import { createStore } from 'vuex'
-
import user from '@/store/user.ts'
-
//创建store实例
-
const store = createStore({
-
//引入模块
-
modules:{
-
user
-
}
-
})
-
export default store
-
-
store user.ts
-
import {login} from '@/api/api.ts'
-
export default {
-
//开启命名空间后 访问所以属性都需要带模块名
-
namespaced:true,
-
state(){
-
return {
-
userInfo : null
-
}
-
},
-
mutations: {
-
initInfo(state,info){
-
state.userInfo=info
-
}
-
},
-
actions: {
-
userLogin(context,info){
-
login(info).then((res) => {
-
if(res){
-
context.commit('initInfo',res.data);
-
console.log(res.data);
-
//本地存储用户信息
-
uni.setStorage({
-
key:'userInfo',
-
data: res.data
-
})
-
//返回上一页
-
uni.navigateBack({
-
delta: 1
-
})
-
}
-
})
-
}
-
}
-
}
main.js
-
// #ifdef VUE3
-
import {
-
createSSRApp
-
} from 'vue'
-
import useAPP from './App.vue'
-
//引入store
-
import store from './store'
-
export function createApp() {
-
const app = createSSRApp(useAPP)
-
app.use(store)
-
return {
-
app
-
}
-
}
App.vue
-
<script setup>
-
import { onLaunch } from '@dcloudio/uni-app';
-
import { useStore } from 'vuex';
-
onLaunch(()=>{
-
//本地缓存已存在登入信息 直接放入store中
-
try{
-
const store = useStore();
-
const user=uni.getStorageSync('userInfo');
-
if(user){
-
store.commit('user/initInfo',user)
-
}
-
}catch(e){
-
console.log('提取用户信息失败');
-
}
-
})
-
</script>
-
-
<style lang="scss">
-
/*每个页面公共css */
-
@import '@/uni_modules/uni-scss/index.scss';
-
/* #ifndef APP-NVUE */
-
@import '@/static/customicons.css';
-
// 设置整个项目的背景色
-
page {
-
background-color: #f5f5f5;
-
}
-
-
/* #endif */
-
.example-info {
-
font-size: 14px;
-
color: #333;
-
padding: 10px;
-
}
-
</style>
type login.ts
-
export interface LoginForm {
-
userTelephone: string
-
userPassword: string
-
}
-
export class LoginData{
-
ruleForm:LoginForm={
-
userTelephone: "",
-
userPassword:""
-
}
-
}
login.vue
-
<template>
-
<view class="content">
-
<view class="avatorWrapper">
-
<view class="avator"><image class="img" src="../../static/logo.png" mode="widthFix"></image></view>
-
</view>
-
<view class="form">
-
<view class="inputWrapper"><input v-model="data.ruleForm.userTelephone" class="input" type="text" value="" placeholder="请输入电话号码" /></view>
-
<view class="inputWrapper"><input v-model="data.ruleForm.userPassword" class="input" type="password" value="" placeholder="请输入密码" /></view>
-
<view class="loginBtn" @click="toLogin()"><text class="btnValue">登入</text></view>
-
<view class="forgotBtn"><text>找回密码</text></view>
-
</view>
-
</view>
-
</template>
-
-
<script setup lang="ts">
-
import { LoginData } from '@/type/login';
-
import { login } from '../../api/api.ts';
-
import { reactive } from 'vue';
-
import { useStore } from 'vuex';
-
//用户输入数据
-
const data = reactive(new LoginData());
-
const store = useStore();
-
const toLogin = () => {
-
//通过vuex 进行登入操作
-
store.dispatch('user/userLogin', data.ruleForm);
-
};
-
</script>
-
-
<style lang="scss">
-
.content {
-
background: #377eb4;
-
width: 100vw;
-
height: 100vh;
-
background-image: url(@/static/bt/bt-2.jpg);
-
}
-
.avatorWrapper {
-
height: 30vh;
-
width: 100vw;
-
display: flex;
-
justify-content: center;
-
align-items: flex-end;
-
}
-
.avator {
-
width: 200upx;
-
height: 200upx;
-
overflow: hidden;
-
}
-
.avator .img {
-
width: 100%;
-
}
-
.form {
-
padding: 0 100upx;
-
margin-top: 80px;
-
}
-
.inputWrapper {
-
width: 100%;
-
height: 80upx;
-
background: white;
-
border-radius: 20px;
-
box-sizing: border-box;
-
padding: 0 20px;
-
margin-top: 25px;
-
}
-
.inputWrapper .input {
-
width: 100%;
-
height: 100%;
-
text-align: center;
-
font-size: 15px;
-
}
-
.loginBtn {
-
width: 100%;
-
height: 80upx;
-
background: #77b307;
-
border-radius: 50upx;
-
margin-top: 50px;
-
display: flex;
-
justify-content: center;
-
align-items: center;
-
}
-
.loginBtn .btnValue {
-
color: white;
-
}
-
.forgotBtn {
-
text-align: center;
-
color: #eaf6f9;
-
font-size: 15px;
-
margin-top: 20px;
-
}
-
</style>
user.vue
-
<template>
-
<view class="content">
-
<view class="userback" @click="toLogin(getUserName)">
-
<img :src="getUserImg">
-
<view class="userName">{{getUserName}}</view>
-
</view>
-
</view>
-
-
</template>
-
-
<script setup lang="ts">
-
import { computed ,ref} from 'vue'
-
import { useStore } from 'vuex';
-
//全局常量
-
import globalParam from '@/common/operate.ts'
-
const store = useStore();
-
//vuex 获取用户名称
-
const getUserName= computed({
-
get:()=>{
-
let {userInfo} = store.state.user;
-
let userName= userInfo ? userInfo.userName : '未登入'
-
return userName
-
}
-
})
-
//vuex 获取用户头像
-
const getUserImg= computed({
-
get:()=>{
-
let {userInfo} = store.state.user;
-
let userImg= userInfo ? globalParam.imgBaseURL userInfo.picUrl : '../../static/user/user.png'
-
return userImg
-
}
-
})
-
// 点击头像 如果未登入状态 点击跳登入页面
-
const toLogin=(userName)=>{
-
if(userName === '未登入'){
-
uni.navigateTo({
-
url: '../login/login'
-
})
-
}
-
}
-
-
</script>
-
-
<style lang="scss">
-
.content {
-
width: 100vw;
-
height: 90vh;
-
background-image: url(@/static/bt/bt-2.jpg);
-
text-align: center
-
}
-
-
.userback img {
-
margin: auto;
-
border-radius: 50%;
-
margin-top: 21%;
-
width: 300upx;
-
height: 300upx;
-
}
-
.userName{
-
color: #111111;
-
font-size: 2rem;
-
}
-
</style>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhcjbjfg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01