employeeControl.vue 4.9 KB
<template>
	<view class="employee_control">
		<!-- 员工列表 -->
		<view class="employeeList">
			<view class="header">
				<view class="list_card">
					<text>姓名</text>
					<text>手机号码</text>
					<text>设置</text>
				</view>
			</view>
			<view class="list">
				<view v-for="(item, index) in employeeList" :key="index" class="list_card">
					<text>{{ item.name }}</text>
					<text>{{ item.phone }}</text>
					<u-icon name="setting" size="16" color="#333" @click="editEmployee(item)"></u-icon>
				</view>
			</view>
		</view>
		<!-- 添加员工 -->
		<view class="add_employee" @click="addEmployee">
			<u-icon name="plus-circle"></u-icon>
			<text class="m-l-20">添加员工</text>
		</view>
		<!-- 添加与修改员工弹出框 -->
		<u-popup :show="show" mode="center" :round="12" @close="show = false">
			<view class="employeeForm">
				<u--form :model="form" ref="uForm" labelWidth="60">
					<u-form-item label="姓名" prop="name">
						<u-input v-model="form.name" border="bottom" placeholder="请输入姓名" />
					</u-form-item>
					<u-form-item label="手机号" prop="phone">
						<u-input v-model="form.phone" border="bottom" placeholder="请输入手机号" />
					</u-form-item>
					<u-form-item label="密码" prop="password">
						<u-input v-model="form.password" type="password" border="bottom" placeholder="请输入密码" />
					</u-form-item>
					<u-form-item>
						<text class="col-9 f-28 l-h-38">密码必须是8-6位英文字母、数字、字符组合(不能是纯数字)</text>
					</u-form-item>
					<u-form-item>
						<view class="btn">
							<u-button text="提交" color="#333" shape="circle" @click="show = false"></u-button>
							<u-button text="取消" color="#DDD" shape="circle" @click="show = false"></u-button>
						</view>
					</u-form-item>
				</u--form>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isEdit: false, // 是否为编辑
				show: false, // 是否显示弹出框
				form: { name: '', phone: '', password: '' },
				employeeList: [
					{ name: '张三', phone: '15812341234', password: '123456'},
					{ name: '李四', phone: '13612341234', password: '123456'},
					{ name: '王五', phone: '19117263465', password: '123456'},
					{ name: '赵六六', phone: '13712341234', password: '123456'}
				],
				rules: {
					name: [
						{ required: true, message: '请输入员工姓名', trigger: ['blur', 'change'] }
					],
					phone: [
						{ required: true, message: '请输入手机号', trigger: ['change'] },
						{
							validator: (rule, value, callback) => {
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change','blur'],
						}
					],
					password: [
						{ required: true, message: '请输入密码', trigger: ['change'] },
						{ pattern: /^[0-9a-zA-Z]*$/g,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '只能包含字母或数字',
							trigger: ['change']
						},
						{ min: 6, max: 8, message: '长度在6-8个字符之间', trigger: ['change'] }
					]
				}
			}
		},
		onReady() {
		//如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			// 添加员工
			addEmployee(){
				this.form = { name: '', phone: '', password: '' }
				this.isEdit = false
				this.show = true
			},
			// 编辑员工
			editEmployee(item){
				this.isEdit = true
				this.form = item
				this.show = true
			},
			// 提交
			submit() {
				this.$refs.uForm.validate().then(valid => {
					if(this.isEdit) {
						uni.$u.toast('编辑')
					} else {
						uni.$u.toast('添加')
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.employee_control{
	padding: 20rpx 30rpx;
	.employeeList{
		border-radius: 12rpx;
		overflow: hidden;
		.header{
			padding: 0 20rpx;
			background: #333;
			color: #fff;
			font-weight: 500;
			font-size: 28rpx;
			line-height: 38rpx;
		}
		.list{
			background-color: #fff;
			padding: 0 20rpx;
			color: #333;
			font-size: 28rpx;
			line-height: 38rpx;
			.list_card{
				border-bottom: 2rpx solid #eee;
				&:last-child{
					border-bottom: 0;
				}
			}
		}
		.list_card{
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 120rpx;
			padding: 0 36rpx;
		}
	}
	.add_employee{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		line-height: 38rpx;
		color: #999;
		height: 120rpx;
		background: #FFFFFF;
		margin-top: 20rpx;
		border-radius: 12rpx;
	}
}

// 添加员工
.employeeForm{
	width: 680rpx;
	padding: 20rpx 30rpx;
	background: #FFFFFF;
	border-radius: 12rpx;
	box-sizing: border-box;
	.btn {
		display: flex;
		align-items: center;
		justify-content: space-around;
		gap: 50rpx;
	}
}
</style>