优化数据获取方式
This commit is contained in:
parent
53af23e703
commit
d91a550769
@ -3,13 +3,13 @@ import {copy, fuzzyMatching, splitKeyWords} from "@/utils/common.js";
|
|||||||
import {Delete, DocumentAdd, Edit, Search} from "@element-plus/icons-vue";
|
import {Delete, DocumentAdd, Edit, Search} from "@element-plus/icons-vue";
|
||||||
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
||||||
import ZooAnimalFormDialog from "@/components/ZooAnimalFormDialog.vue";
|
import ZooAnimalFormDialog from "@/components/ZooAnimalFormDialog.vue";
|
||||||
|
import {mapActions,mapState} from "vuex";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "ZooAnimal",
|
name: "ZooAnimal",
|
||||||
components: {ZooAnimalFormDialog, ZooUserFormDialog},
|
components: {ZooAnimalFormDialog, ZooUserFormDialog},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
animals: [],
|
|
||||||
tableData: [],
|
tableData: [],
|
||||||
searchInput: {
|
searchInput: {
|
||||||
state: '',//状态(0正常 1异常)
|
state: '',//状态(0正常 1异常)
|
||||||
@ -38,18 +38,7 @@ export default {
|
|||||||
return Delete
|
return Delete
|
||||||
},
|
},
|
||||||
//endregion
|
//endregion
|
||||||
// dataFilters() {
|
...mapState(["animals"])
|
||||||
// // 计算格式化后的数组
|
|
||||||
// let data = copy(this.tableData)
|
|
||||||
// data.forEach(e => {
|
|
||||||
// if (e.state === 0) {
|
|
||||||
// e.state = '正常'
|
|
||||||
// } else if (e.state === 1) {
|
|
||||||
// e.state = '异常'
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// return data
|
|
||||||
// },
|
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
searchInput: {
|
searchInput: {
|
||||||
@ -60,6 +49,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
...mapActions(["getAnimals"]),
|
||||||
showEditDialog(animal) {
|
showEditDialog(animal) {
|
||||||
//这里使用animal的id查询后端的特定动物再赋值,这里用假数据代替一下
|
//这里使用animal的id查询后端的特定动物再赋值,这里用假数据代替一下
|
||||||
this.dialog.editData = this.animals.find(e => e.id === animal.id)
|
this.dialog.editData = this.animals.find(e => e.id === animal.id)
|
||||||
@ -177,23 +167,7 @@ export default {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
//数据生成器
|
this.getAnimals()
|
||||||
for (let i = 0; i < 100; i++) {
|
|
||||||
let animal = {
|
|
||||||
id: i,
|
|
||||||
name: 'animal' + (i + 3),
|
|
||||||
sex: Math.floor(Math.random() * 2) === 0 ? '雌性' : '雄性',
|
|
||||||
species: Math.floor(Math.random() * 10).toString(),//种类
|
|
||||||
weight: Number((Math.random() * 2000).toFixed(2)),
|
|
||||||
height: Number((Math.random() * 10).toFixed(2)),
|
|
||||||
state: Math.floor(Math.random() * 2),//状态(0正常 1异常)
|
|
||||||
roleId: Math.floor(Math.random() * 100),//饲养员id
|
|
||||||
color: '',//Math.floor(Math.random() * 255),//颜色
|
|
||||||
features: Math.floor(Math.random() * 40).toString(),//特征
|
|
||||||
habit: Math.floor(Math.random() * 500).toString()//生活习性
|
|
||||||
}
|
|
||||||
this.animals.push(animal)
|
|
||||||
}
|
|
||||||
this.tableData = copy(this.animals);//复制一份给表格展示
|
this.tableData = copy(this.animals);//复制一份给表格展示
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
import {mapMutations} from "vuex";
|
import {mapMutations,mapState,mapActions} from "vuex";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "ZooContainer",
|
name: "ZooContainer",
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
user: {
|
user: {
|
||||||
username:'',
|
username: '',
|
||||||
password:'123456',
|
password: '123456',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
computed: {
|
||||||
|
...mapState(['loginUser'])
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapMutations(['updateUserName']),
|
...mapActions(['getLoginUser']),
|
||||||
|
...mapMutations(['updateLoginUser']),
|
||||||
onSubmit() {
|
onSubmit() {
|
||||||
console.log('点击了登录按钮')
|
console.log('点击了登录按钮')
|
||||||
if (!this.user.username || !this.user.password) {
|
if (!this.user.username || !this.user.password) {
|
||||||
@ -22,32 +26,22 @@ export default {
|
|||||||
message: '用户名和密码不能为空',
|
message: '用户名和密码不能为空',
|
||||||
type: 'warning',
|
type: 'warning',
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
//此处接入后端登录接口验证登录用户名和密码,验证通过方可通行
|
|
||||||
//密码错误
|
|
||||||
if (false) {
|
|
||||||
return ElMessage({
|
|
||||||
message: '用户名不存在或密码错误',
|
|
||||||
type: 'error',
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
//登录成功
|
|
||||||
sessionStorage.setItem('isLogin', 'isLogin');//登录状态
|
this.getLoginUser(this.user)//通过表单获取登录的用户
|
||||||
localStorage.setItem('userName', this.user.username);//本地存储登录用户名
|
//能获取到登录后的数据,说明登录成功
|
||||||
this.updateUserName(this.user.username);//存储登录用户名
|
if (this.loginUser) {
|
||||||
|
localStorage.setItem('username', this.loginUser.username);//本地存储登录用户名
|
||||||
this.$router.push('/panel/home')
|
this.$router.push('/panel/home')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
reset() {
|
reset() {
|
||||||
this.user = {}
|
this.user = {}
|
||||||
},
|
},
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
|
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.updateUserName('');//清空退出登录的用户名
|
this.updateLoginUser('');//清空退出登录的用户
|
||||||
this.user.username = localStorage.getItem('userName');//自动填充历史登录的用户
|
this.user.username = localStorage.getItem('username');//自动填充历史登录的用户
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
<script>
|
<script>
|
||||||
import {Delete, Edit, Search, User} from "@element-plus/icons-vue";
|
import {Delete, Edit, Search, User} from "@element-plus/icons-vue";
|
||||||
|
import {mapState,mapActions} from "vuex";
|
||||||
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
||||||
import {copy, fuzzyMatching, splitKeyWords} from "@/utils/common.js";
|
import {copy, fuzzyMatching, generateUsers, splitKeyWords} from "@/utils/common.js";
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -25,6 +26,7 @@ export default {
|
|||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
...mapState(["users"])
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
searchInput: {
|
searchInput: {
|
||||||
@ -36,7 +38,6 @@ export default {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
users: [],//原始用户数据
|
|
||||||
tableData: [],//用来显示在界面的数据
|
tableData: [],//用来显示在界面的数据
|
||||||
searchInput: {
|
searchInput: {
|
||||||
auth: '',
|
auth: '',
|
||||||
@ -50,6 +51,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
...mapActions(['getUsers']),
|
||||||
showEditDialog(user) {
|
showEditDialog(user) {
|
||||||
// console.log('点击了编辑',user)
|
// console.log('点击了编辑',user)
|
||||||
//这里使用user的用户名查询后端的特定用户再赋值,这里用假数据代替一下
|
//这里使用user的用户名查询后端的特定用户再赋值,这里用假数据代替一下
|
||||||
@ -169,6 +171,7 @@ export default {
|
|||||||
this.tableData = searchResult
|
this.tableData = searchResult
|
||||||
},
|
},
|
||||||
//鼠标悬浮显示密码
|
//鼠标悬浮显示密码
|
||||||
|
//region
|
||||||
showPassword(event,password){
|
showPassword(event,password){
|
||||||
console.log('showPassword')
|
console.log('showPassword')
|
||||||
event.target.innerText = password
|
event.target.innerText = password
|
||||||
@ -176,18 +179,10 @@ export default {
|
|||||||
hidePassword(event) {
|
hidePassword(event) {
|
||||||
event.target.innerText = '••••••••'
|
event.target.innerText = '••••••••'
|
||||||
}
|
}
|
||||||
|
//endregion
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
//数据生成器
|
this.getUsers()//获取用户数据
|
||||||
for (let i = 0; i < 100; i++) {
|
|
||||||
let user = {
|
|
||||||
userKey: i,
|
|
||||||
username: 'user' + (i + 3),
|
|
||||||
password: '123456',
|
|
||||||
auth: Math.floor(Math.random() * 3),
|
|
||||||
}
|
|
||||||
this.users.push(user)
|
|
||||||
}
|
|
||||||
this.tableData = copy(this.users);//复制一份给表格展示
|
this.tableData = copy(this.users);//复制一份给表格展示
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import ZooPanel from "@/pages/ZooPanel.vue";
|
|||||||
import ZooHome from '../pages/ZooHome.vue'
|
import ZooHome from '../pages/ZooHome.vue'
|
||||||
import ZooUser from "@/pages/ZooUser.vue";
|
import ZooUser from "@/pages/ZooUser.vue";
|
||||||
import ZooAnimal from "@/pages/ZooAnimal.vue";
|
import ZooAnimal from "@/pages/ZooAnimal.vue";
|
||||||
|
import store from "@/store/index.js";
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHashHistory(),
|
history: createWebHashHistory(),
|
||||||
@ -52,7 +53,7 @@ const router = createRouter({
|
|||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
//判断页面是否需要登录权限才可进入
|
//判断页面是否需要登录权限才可进入
|
||||||
if (to.meta.isAuth) {
|
if (to.meta.isAuth) {
|
||||||
if (!sessionStorage.getItem('isLogin')) {
|
if (!store.state.loginUser.username) {
|
||||||
router.replace({
|
router.replace({
|
||||||
path: '/login',
|
path: '/login',
|
||||||
})
|
})
|
||||||
@ -64,4 +65,6 @@ router.beforeEach((to, from, next) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default router
|
export default router
|
@ -1,21 +1,57 @@
|
|||||||
import {createStore} from "vuex";
|
import {createStore} from "vuex";
|
||||||
|
import {generateAnimals, generateUsers} from "@/utils/common.js";
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
|
//更新登录用户
|
||||||
|
getLoginUser(context, user) {
|
||||||
|
//此处接入后端登录接口验证登录用户名和密码,验证通过方可通行
|
||||||
|
//密码错误
|
||||||
|
if (false) {
|
||||||
|
return ElMessage({
|
||||||
|
message: '用户名不存在或密码错误',
|
||||||
|
type: 'error',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
//登录成功
|
||||||
|
context.commit('updateLoginUser', user);//存储登录用户,这里拉取远程登录的用户作为参数
|
||||||
|
},
|
||||||
|
//获取用户数据
|
||||||
|
getUsers(context) {
|
||||||
|
context.commit('updateUsers', generateUsers())
|
||||||
|
},
|
||||||
|
//获取动物数据
|
||||||
|
getAnimals(context) {
|
||||||
|
context.commit('updateAnimals', generateAnimals())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mutations = {
|
const mutations = {
|
||||||
//更新已登录用户名
|
//更新已登录的用户
|
||||||
updateUserName(state, value) {
|
updateLoginUser(state, value) {
|
||||||
state.user.username = value
|
state.loginUser = value
|
||||||
|
},
|
||||||
|
//更新用户集合
|
||||||
|
updateUsers(state, value) {
|
||||||
|
state.users = value
|
||||||
|
},
|
||||||
|
//更新动物集合
|
||||||
|
updateAnimals(state, value) {
|
||||||
|
state.animals = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = {
|
const state = {
|
||||||
//登录用户数据
|
//登录用户数据
|
||||||
user: {
|
loginUser: {
|
||||||
username: 'user1',
|
username: 'user1',
|
||||||
}
|
auth: null
|
||||||
|
},
|
||||||
|
//用户数据
|
||||||
|
users: [],
|
||||||
|
//动物数据
|
||||||
|
animals: [],
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default createStore({
|
export default createStore({
|
||||||
|
@ -1,3 +1,40 @@
|
|||||||
|
export function generateUsers() {
|
||||||
|
const users = []
|
||||||
|
//数据生成器
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
let user = {
|
||||||
|
userKey: i,
|
||||||
|
username: 'user' + (i + 3),
|
||||||
|
password: '123456',
|
||||||
|
auth: Math.floor(Math.random() * 3),
|
||||||
|
}
|
||||||
|
users.push(user)
|
||||||
|
}
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateAnimals() {
|
||||||
|
const animals = []
|
||||||
|
//数据生成器
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
let animal = {
|
||||||
|
id: i,
|
||||||
|
name: 'animal' + (i + 3),
|
||||||
|
sex: Math.floor(Math.random() * 2) === 0 ? '雌性' : '雄性',
|
||||||
|
species: Math.floor(Math.random() * 10).toString(),//种类
|
||||||
|
weight: Number((Math.random() * 2000).toFixed(2)),
|
||||||
|
height: Number((Math.random() * 10).toFixed(2)),
|
||||||
|
state: Math.floor(Math.random() * 2),//状态(0正常 1异常)
|
||||||
|
roleId: Math.floor(Math.random() * 100),//饲养员id
|
||||||
|
color: '',//Math.floor(Math.random() * 255),//颜色
|
||||||
|
features: Math.floor(Math.random() * 40).toString(),//特征
|
||||||
|
habit: Math.floor(Math.random() * 500).toString()//生活习性
|
||||||
|
}
|
||||||
|
animals.push(animal)
|
||||||
|
}
|
||||||
|
return animals
|
||||||
|
}
|
||||||
|
|
||||||
//深拷贝任何值
|
//深拷贝任何值
|
||||||
export function copy(value) {
|
export function copy(value) {
|
||||||
let result = JSON.stringify(value)
|
let result = JSON.stringify(value)
|
||||||
|
Loading…
Reference in New Issue
Block a user