uni-app两个入口模块(客户端和用户端)

news2024/10/5 14:26:10

思路:使用vuex对小程序进行登录状态管理,采用集中式存储管理应用的登录状态

Vuex 是什么? | Vuex

效果:

新建store文件夹,在文件夹中新建一个index.js文件和一个modules文件夹,modules文件夹中新建一个stormoduls.js文件

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import storemoduls from './modules/storemoduls'
Vue.use(Vuex)
const store = new Vuex.Store({
	modules: {
		'storemoduls': storemoduls,
	}
})
export default store

 store/modules/stormoduls.js

uni.setStorageSync(key, data)设置缓存数据

uni.getStorageSync(key)获取缓存数据

export default {
	namespaced: true, //命名空间
	state: () => ({
		token: uni.getStorageSync('token') || '',//存储的登录之后获取到的头像昵称
		userinfo: uni.getStorageSync('userinfo') || '{}',//存储的登录之后获取到的头像昵称
		clcl: uni.getStorageSync('clcl') || '',
	}),
	//提交 mutation,更改state的状态
	mutations: {
		updateUserInfo(state, userinfo) {
			state.userinfo = userinfo
			this.commit('storemoduls/saveUserInfoToStorage')
		},
		saveUserInfoToStorage(state) {
			uni.setStorageSync('userinfo', state.userinfo)
		},
		updateToken(state, token) {
			state.token = token
			this.commit('storemoduls/saveTokenToStorage')
		},
		saveTokenToStorage(state) {
			uni.setStorageSync('token', state.token)
		},
		updateclcl(state, clcl) {
			state.clcl = clcl
			this.commit('storemoduls/saveclclToStorage')
		},
		saveclclToStorage(state) {
			uni.setStorageSync('clcl', state.clcl)
		},
	}
}

在main.js中引入

import store from './store'
Vue.prototype.$store = store
const app = new Vue({
	...App,
	store
})

pages.json中的tabBar里我的页面:pages/login/login

 ...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])

<template>
	<view>
		<Login v-if="!token && !clcl"></Login>
		<barberucell v-else-if="token"></barberucell>
		<clientucell v-if="clcl"></clientucell>
	</view>
</template>

<script>
	import Login from '@/components/login/login.vue'
	import barberucell from '@/components/cellgroup/barberucell.vue'
	import clientucell from '@/components/cellgroup/clientucell.vue'
	import { mapState } from 'vuex'
	export default {
		data() {
			return {

			}
		},
		components: {
			Login,
			barberucell,
			clientucell
		},
		computed: {
            //将storemoduls模块下的token,clcl内容映射到当前页面
			...mapState('storemoduls', ['token', 'clcl'])
		},
	}
</script>

components/login/login.vue

<template>
	<view>
		<view class="bg-box">
			<view v-if="login" class="bg-sing">
				<view class="sing-cen">
					<view class="sing-ing">
						<u--image radius="50px" :src="userinfo.avatarUrl" height="100px" width="100px"></u--image>
						<text class="sing-tex">{{userinfo.nickName}}</text>
					</view>
				</view>
			</view>
			<view v-else class="sing-out">
				<view class="sing-ing">
					<u--image radius="50px"
						src="https://img1.baidu.com/it/u=2673751307,2157785445&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
						height="100px" width="100px"></u--image>
				</view>
				<view>
					<button type="warn" size="mini" @click="Signin">客户端</button>
					<button type="warn" style="margin-left: 20px;" size="mini" @click="barberSignin">用户端</button>
				</view>
			</view>
		</view>
		<u-cell-group v-for="(g,index) in grouplist" :key="index">
			<u-cell icon="map" :title="g.title" :name="g.name" :value="g.value" :isLink="true" @click="cellrow()">
			</u-cell>
		</u-cell-group>
		<u-modal :show="showcellrow" :showConfirmButton="false" width="260px">
			<view class="modal-slot">
				<view class="modal-img">
					<u--image radius="50px"
						src="https://img1.baidu.com/it/u=2673751307,2157785445&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
						height="60px" width="60px"></u--image>
				</view>
				<view>欢迎使用</view>
				<view class="model-p">为了您良好的体验小程序,我们需要您授权信息进行登录</view>
				<view class="model-btn">
					<u-button type="error" shape="circle" text="关闭" @click="closemodal"></u-button>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'//导入mutations的辅助方法mapMutations
	import { mapState } from 'vuex'//引入state的辅助函数mapState

	export default {
		data() {
			return {
				login: false,
				grouplist: [{
						title: "优惠劵",
						name: "coupon",
						value: "优惠福利"
					},
					{
						title: "消费积分",
						name: "integral",
						value: "积分换礼品"
					}, {
						title: "我的订单",
						name: "orderform",
						value: ""
					}
				],
				clientuser: {},
				showcellrow: false,
			}
		},
		computed: {
           //将storemoduls模块下的userinfo的内容映射到当前页面作为计算属性来使用
			...mapState('storemoduls', ['userinfo']),
		},
		methods: {
           //将storemoduls模块下的updateUserInfo,updateclcl方法映射到当前页面
			...mapMutations('storemoduls', ['updateUserInfo', 'updateclcl']),
			Signin() {
				wx.getUserProfile({
					desc: '必须授权才能继续使用',
					success: (res) => {
						this.clientuser = res.userInfo
                        //更新updateUserInfo方法中的数据
						this.updateUserInfo(this.clientuser)
                        //更新updateclcl方法中的数据
						this.updateclcl("storemoduls")
						this.login = true
						this.showcellrow = false
					},
					fail: (err) => {
						console.log('授权失败', err);
					}
				})
			},
			barberSignin() {
				uni.navigateTo({
					url: '/pages/login/barberlogin'
				})
			},
			cellrow() {
				this.showcellrow = true
			},
			closemodal() {
				this.showcellrow = false
			},
		},
	}
</script>

<style lang="scss" scoped>
	.bg-box {
		background-image: linear-gradient(#e66465, #8081e5);
		height: 480rpx;

		.sing-out {
			padding-top: 50rpx;
			text-align: center;
			line-height: 120rpx;

			.sing-ing {
				display: inline-block;
			}
		}

		.bg-sing {
			padding: 10rpx;
			padding-top: 50rpx;

			.sing-btn {
				text-align: right;
			}

			.sing-cen {
				text-align: center;
				line-height: 50rpx;

				.sing-ing {
					display: inline-block;

					.sing-tex {
						font-size: 14px;
						color: white;
					}
				}
			}
		}
	}

	.modal-slot {
		line-height: 70rpx;
		text-align: center;

		.modal-img {
			display: inline-block;
		}

		.model-btn {
			margin-top: 20rpx;
			margin-bottom: 20rpx;
		}

		.model-p {
			font-size: 14px;
			color: #686868;
			line-height: 40rpx;
		}
	}
</style>

 /pages/login/barberlogin

<template>
	<view class="barber-box">
		<u-form :model="form" ref="uForm" label-width="120" :rules="rules">
			<u-form-item label="用户名" prop="username">
				<u-input v-model="form.username" />
			</u-form-item>
			<u-form-item label="密码" prop="password">
				<u-input type="password" v-model="form.password" />
			</u-form-item>
		</u-form>
		<button type="warn" @click="submit">登陆</button>
	</view>
</template>

<script>
	import { mapMutations, mapState } from 'vuex'
	export default {
		data() {
			return {
				form: {
					username: '',
					password: ''
				},
				rules: {
					username: [{
						required: true,
						message: '请输入用户名',
						trigger: ['blur', 'change']
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: ['blur', 'change']
					}]
				},
			}
		},
		computed: {

		},
		methods: {
            //将storemoduls模块下的updateUserInfo,updateToken方法映射到当前页面
			...mapMutations('storemoduls', ['updateUserInfo', 'updateToken']),
			submit() {
				this.$refs.uForm.validate().then(res => {
					uni.showToast({
						title: '登录成功',
					});
					let obj = {
						avatarUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
						nickName: '用户端'
					}
					let ken = '897658767'
                   //更新updateToken方法中的数据
					this.updateToken(ken)
                   //更新updateUserInfo方法中的数据
					this.updateUserInfo(obj)
					uni.switchTab({
						url: '/pages/login/login'
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.barber-box {
		padding: 50rpx;
	}
</style>

components/cellgroup/barberucell.vue

<template>
	<view>
		<view class="bg-box">
			<view class="bg-sing">
				<view class="sing-btn">
					<button type="warn" size="mini" class="btn" @click="Logout">退出</button>
				</view>
				<view class="sing-cen">
					<view class="sing-ing">
						<u--image radius="50px" :src="userinfo.avatarUrl" height="100px" width="100px"></u--image>
						<text class="sing-tex">{{userinfo.nickName}}</text>
					</view>
				</view>
			</view>
		</view>
		<u-cell-group v-for="(g,index) in grouplist" :key="index">
			<u-cell icon="map" :title="g.title" :name="g.name" :value="g.value" :isLink="true" @click="cellrow()">
			</u-cell>
		</u-cell-group>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				login: false,
				grouplist: [{
						title: "客户预约信息",
						name: "customerappointment",
						value: "信息"
					},
					{
						title: "工作时间",
						name: "workinghours",
						value: "时间"
					}, {
						title: "客户消费",
						name: "customerconsumption",
						value: "消费"
					},
					{
						title: "报表",
						name: "reportforms",
						value: "报表"
					}
				],
				clientuser: {},
				showcellrow: false,
			}
		},
		computed: {
            //将storemoduls模块下的userinfo的内容映射到当前页面作为计算属性来使用
			...mapState('storemoduls', ['userinfo']),
		},

		methods: {
            //将storemoduls模块下的updateUserInfo,updateToken方法映射到当前页面
			...mapMutations('storemoduls', ['updateUserInfo', 'updateToken']),
			Logout() {
				wx.clearStorageSync(); //一键清除所有本地数据缓存
				this.updateToken('')//更新updateToken方法中的数据
			},
			cellrow(name) {
				 console.log(name);
			},
		},
	}
</script>

<style lang="scss" scoped>
	.bg-box {
		background-image: linear-gradient(#e66465, #8081e5);
		height: 480rpx;

		.sing-out {
			padding-top: 50rpx;
			text-align: center;
			line-height: 120rpx;

			.sing-ing {
				display: inline-block;
			}
		}

		.bg-sing {
			padding: 10rpx;
			padding-top: 50rpx;

			.sing-btn {
				text-align: right;
			}

			.sing-cen {
				text-align: center;
				line-height: 100rpx;

				.sing-ing {
					display: inline-block;

					.sing-tex {
						font-size: 14px;
						color: white;
					}
				}
			}
		}
	}
</style>

components/cellgroup/clientucell.vue

<template>
	<view>
		<view class="bg-box">
			<view class="bg-sing">
				<view class="sing-btn">
					<button type="warn" size="mini" class="btn" @click="Logout">退出</button>
				</view>
				<view class="sing-cen">
					<view class="sing-ing">
						<u--image radius="50px" :src="userinfo.avatarUrl" height="100px" width="100px"></u--image>
						<text class="sing-tex">{{userinfo.nickName}}</text>
					</view>
				</view>
			</view>
		</view>
		<u-cell-group v-for="(g,index) in grouplist" :key="index">
			<u-cell icon="map" :title="g.title" :name="g.name" :value="g.value" :isLink="true" @click="cellrow()">
			</u-cell>
		</u-cell-group>
	</view>
</template>
<script>
	import { mapMutations } from 'vuex'
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				login: false, 
				grouplist: [{
						title: "会员",
						name: "member",
						value: ""
					},
					{
						title: "我的订单",
						name: "orderforgoods",
						value: "订单"
					},
					{
						title: "充值",
						name: "Recharge",
						value: ""
					}, {
						title: "消费明细",
						name: "details",
						value: "记录"
					},
					{
						title: "预约记录",
						name: "reservation",
						value: ""
					},
				],
			}
		},
		computed: {
            //将storemoduls模块下的userinfo的内容映射到当前页面作为计算属性来使用
			...mapState('storemoduls', ['userinfo']),
		},
		methods: {
            //将storemoduls模块下的updateUserInfo,updateclcl方法映射到当前页面
			...mapMutations('storemoduls', ['updateUserInfo', 'updateclcl']),
			Logout() {
				wx.clearStorageSync();//一键清除所有本地数据缓存
				this.updateclcl('')	//更新updateclcl方法中的数据
			},
			cellrow(name) {
				console.log(name);
			},
		},
	}
</script>

<style lang="scss" scoped>
	.bg-box {
		background-image: linear-gradient(#e66465, #8081e5);
		height: 480rpx;

		.sing-out {
			padding-top: 50rpx;
			text-align: center;
			line-height: 120rpx;

			.sing-ing {
				display: inline-block;
			}
		}

		.bg-sing {
			padding: 10rpx;
			padding-top: 50rpx;

			.sing-btn {
				text-align: right;
			}

			.sing-cen {
				text-align: center;
				line-height: 100rpx;

				.sing-ing {
					display: inline-block;

					.sing-tex {
						font-size: 14px;
						color: white;
					}
				}
			}
		}
	}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/513248.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【历史上的今天】5 月 11 日:Dijkstra 算法开发者诞生;电子表格软件的开山鼻祖;机器狗 AIBO 问世

整理 | 王启隆 透过「历史上的今天」&#xff0c;从过去看未来&#xff0c;从现在亦可以改变未来。 今天是 2023 年 5 月 11 日&#xff0c;在 1995 年的今天&#xff0c;我国成为第六个研制成功磁悬浮列车的国家。磁悬浮列车利用“同性相斥&#xff0c;异性相吸”的原理&…

Linux基础学习---2、系统管理、帮助命令、文件目录类命令

1、系统管理 1.1 Linux中的进程和服务 计算机中&#xff0c;一个正在执行的程序或命令。被叫做“进程”&#xff08;Process&#xff09;。 启动之后一直存在、常驻内存的进程&#xff0c;一般称做“服务”&#xff08;Service&#xff09;。1.2 systemctl&#xff08;CentOS…

以playwright脚本为例,详解Python with as处理异常的原理

大家在做playwright自动化测试时&#xff0c;一定会遇到下面的写法 with sync_playwright() as p:自动化代码 很多同学可能只是按照这种写法来编写项目的自动化测试代码&#xff0c;对于具体细节可能并不了解&#xff0c;今天我来结合playwright讲解一下 Python中的 with ...…

大咖齐聚CCIG论坛——文档图像智能分析的产业前沿

目录 1 文档图像智能分析技术2 大咖齐聚CCIG20233 议题介绍3.1 从模式识别到类脑研究3.2 视觉-语言预训练模型演进及应用3.3 篡改文本图像的生成和检测3.4 智能文档处理在工业界的应用与挑战 4 观看入口&议程 1 文档图像智能分析技术 文档图像智能分析是指使用计算机视觉和…

推荐一款支持多种存储的程序AList(附上个人站点)

1、安装部署 1、一键脚本 仅适用于 Linux amd64/arm64 平台。 #安装 curl -fsSL "https://alist.nn.ci/v3.sh" | bash -s install#更新 curl -fsSL "https://alist.nn.ci/v3.sh" | bash -s update#卸载 curl -fsSL "https://alist.nn.ci/v3.sh&qu…

【react】特种兵之react.tsx列表页面搭建

目录 背景了解工程架构我可开始了啊新建一个页面怎么写&#xff1f;前端代码编写思路 感触 背景 方便系统运维、不用每次初始化数据都走数据订正 这是第一次毕业之后&#xff0c;用前端语言新建一个页面&#xff0c;过程自然是极其曲折。情绪也是跌宕起伏&#xff0c;从页面显…

无需代理客户端,轻松实现虚拟机备份!

21世纪的虚拟化技术为企业提供了极大的便利和成本节约。它允许企业通过管理程序创建虚拟机以在单个主机上运行多个不同的操作系统&#xff0c;从而充分利用计算资源并节省架构和管理成本。 随着组织将其业务迁移到虚拟化平台&#xff0c;执行数据保护的方法也随之改变。虚拟机…

重视企业缺少成本票问题!合理利用税收优惠政策降低企业所得税!

重视企业缺少成本票问题&#xff01;合理利用税收优惠政策降低企业所得税&#xff01; 业务是流程&#xff0c;财税是结果&#xff0c;税收问题千千万&#xff0c;关注《税算盘》来帮你找答案。 很多企业在成立的初期会出现对票据管理不严格的情况&#xff0c;前期影响也不是…

精选博客系列|VMware Tanzu Mission Control增强策略功能,让Kubernetes的安全性更加灵活

随着分布式系统日益复杂&#xff0c;定义和实施 Kubernetes 集群的策略以确保环境的安全性、可靠性和合规性 —— 当然也是为了构建可扩展性的支撑结构&#xff0c;变得至关重要。 因此&#xff0c;我们很高兴宣布 VMware Tanzu Mission Control 现在有了更多的策略相关的改进…

C# webservice 接收json数据 接口返回 远程服务器返回错误: (500) 内部服务器错误

C# post 调用webservice 服务端接口&#xff0c;会返回上面那个错误&#xff0c;8成是发送的数据和接口不符合造成的。有2种情况 第一种情况如下&#xff1a;如果类型是默认request.ContentType "application/x-www-form-urlencoded";这个类型 那么你发送数据和被…

基于趋动云部署B站大V秋葉aaaki的Stable Diffusion整合包v4--linux版

B站大V秋葉aaaki的Stable Diffusion整合V4版发布了&#xff0c;集成度比较高&#xff0c;在windows下解压缩直接就可以使用&#xff0c;整合的非常好。但是笔人没有RTX4090这样级别的显卡&#xff0c;又希望有个高速运行的效果。 所以索性到云GPU主机上来用秋叶aaaki的Stable …

windows机制初探

Windows内存管理 EPROCESS结构体&#xff1a;在内核中表示一个进程 VAD树 二叉树&#xff0c;存储进程在内核层申请的虚拟内存信息 (x86 EPROCESS0x11c) (x64 EPROCESS0x7d8)指向VadRoot(VAD树) 可以看到两种内存&#xff1a;Private(私有内存)、 Mapped(映射内存) 私有内…

Elasticsearch 入门

Elasticsearch 是一种开源搜索引擎&#xff0c;它基于 Apache Lucene 构建&#xff0c;提供了一个分布式、多租户、全文搜索和分析引擎。Elasticsearch 可以处理海量数据&#xff0c;能够快速、准确地搜索、分析和可视化数据。 Elasticsearch 最初是为了解决日志搜索和分析而开…

2018年下半年 软件设计师 答案及详解

2018年下半年 软件设计师 答案详解 主要记录刷题相关笔记&#xff0c;方便日后温习&#xff01;&#xff01;&#xff01; 一、选择题 CPU 在执行指令的过程中&#xff0c;会自动修改_____的内容&#xff0c;以使其保存的总是将要执行的下一条指令的地址。 A.指令寄存器B.程…

makefile 变量的扩展

文章目录 前言一、环境变量&#xff08;全局变量&#xff09;二、目标变量&#xff08;局部变量&#xff09;三、模式变量总结 前言 一、环境变量&#xff08;全局变量&#xff09; Makefile 中的环境变量&#xff0c;是指在执行 Makefile 时&#xff0c;从外部传入 Make 命令…

m4a音频怎么转换成mp3?

m4a音频怎么转换成mp3&#xff1f;如果你遇到了m4a音频格式的文件&#xff0c;但却无法在音频播放器中打开它&#xff0c;通常我们会将其转换成mp3格式。因为M4A属于苹果专用的音频格式&#xff0c;而MP3则是一种通用音频格式。M4A属于高品质压缩类型的音乐文件&#xff0c;而M…

Django框架之使用Session保持HTTP状态

HTTP协议是无状态的&#xff0c;每次请求都是新的请求&#xff1b; 客户端与服务器端的一次通信就是一次会话。 可使用cookie和session在客户端或者服务端存储有关会话的数据&#xff0c;来进行状态保持&#xff1b; cookie使用在上篇。本篇是使用session进行状态保持及使用Red…

外观数列----2023/5/11

外观数列----2023/5/11 给定一个正整数 n &#xff0c;输出外观数列的第 n 项。 「外观数列」是一个整数序列&#xff0c;从数字 1 开始&#xff0c;序列中的每一项都是对前一项的描述。 你可以将其视作是由递归公式定义的数字字符串序列&#xff1a; countAndSay(1) “1”…

Omniverse Replicator环境配置和使用说明

Omniverse Replicator使用说明 本教程将介绍Omniverse Replicator的环境配置和使用说明, 参加Sky Hackathon的同学可以参考本教程来合成训练数据集. 文章目录 Omniverse Replicator使用说明1. Omniverse环境配置1.a.安装Omniverse Launcher1.a.1.在下面的地址下载Omniverse La…

案例分享:ChatGPT写python脚本,轻松文本处理

大家好&#xff0c;我是可夫小子&#xff0c;关注AIGC、读书和自媒体。解锁更多ChatGPT、AI绘画玩法。加&#xff1a;keeepdance&#xff0c;备注&#xff1a;chatgpt&#xff0c;拉你进群。 在工作中&#xff0c;需要对数据进行筛选、分割和整理&#xff0c;当你接受到一个长长…