zoo-frontend/src/utils/common.js
2024-06-10 12:32:27 +08:00

347 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//生成用户数据
import store from "@/store/index.js";
import request from "@/utils/request.js";
// 生成用户数据
export function generateUsers() {
const users = []
// 随机生成20-40条用户数据
const n = Math.floor(Math.random() * 20) + 20;
for (let i = 0; i < n; i++) {
const a = generateAuth()
let user = {
id: i + 2,
username: 'user' + (i + 2),
password: '123456',
auth: a,
}
users.push(user)
request.addUserRequest(user)
if (a === 1) {
keeper.push(user)
} else if (a === 2) {
veterinary.push(user)
}
}
console.log('用户', users, keeper, veterinary)
return users
}
//生成用户的比例为管理员2饲养员6兽医2
function generateAuth() {
const n = Math.floor(Math.random() * 100);
if (n < 20) {
return 0
}
if (n < 80) {
return 1
}
if (n < 100) {
return 2
}
}
//生成动物数据
export function generateAnimals() {
const animals = []
//随机生成40-60只动物
const n = Math.floor(Math.random() * 20) + 40;
for (let i = 0; i < n; i++) {
let animal = {
id: i + 3,
name: 'animal' + (i + 1),
sex: Math.floor(Math.random() * 2) === 0 ? '雌性' : '雄性',
species: '种类' + Math.ceil(Math.random() * 20).toString(),//种类
weight: Number((Math.random() * 2000).toFixed(2)),
height: Number((Math.random() * 10).toFixed(2)),
state: generateState(),//状态0正常 1异常
roleId: generateKeeperId(),//饲养员id
color: generateNewColor(),//颜色
features: 'test animal' + (i + 1) + ' features',//特征
phase: generatePhase()
}
request.addAnimalRequest(animal)
animals.push(animal)
}
console.log('动物', animals)
store.state.animals = animals
return animals
}
//生成动物状态正常9异常1
function generateState() {
const n = Math.floor(Math.random() * 100);
if (n < 10) {
return 1
}
if (n < 100) {
return 0
}
}
//生成已有的饲养员id
//region
const keeper = []
function generateKeeperId() {
const n = Math.floor(Math.random() * keeper.length);
return keeper[n].id
}
//endregion
//生成已有的兽医id
//region
const veterinary = []
function generateVeterinaryId() {
const n = Math.floor(Math.random() * veterinary.length);
return veterinary[n].id
}
//endregion
//生成颜色
//region
const hexCharacters = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"]
function getCharacter(index) {
return hexCharacters[index]
}
function generateNewColor() {
let hexColorRep = "#"
for (let index = 0; index < 6; index++) {
const randomPosition = Math.floor(Math.random() * hexCharacters.length)
hexColorRep += getCharacter(randomPosition)
}
return hexColorRep
}
//endregion
// id: '',//int 饲养计划id
// name: '',//字符 饲养计划名称
// species: '',//字符 种类
// sex: '雄性',//字符 饲养动物性别
// phase: '',//字符 饲养动物阶段:幼年期、成长期、成年期、老年期
// state: 0,//int 状态0正常 1异常
// describe: ''//字符 饲养计划的描述
//生成饲养计划
export function generateBreedingPlans() {
const breedingPlans = []
//随机生成5种类动物的饲养计划
const n = 5;
for (let i = 0; i < n; i++) {
//分别生成2种性别的
for (let j = 0; j < 2; j++) {
//分别生成4种生命阶段的
for (let k = 0; k < 4; k++) {
//分别生成2种状态的
for (let l = 0; l < 2; l++) {
let breedingPlan = {
// id: i + '' + j + '' + k + '' + l,
name: 'breeding' + (i + 1),
species: '种类' + i.toString(),
sex: j === 0 ? '雌性' : '雄性',
phase: k === 0 ? '幼年期' : k === 1 ? '成长期' : k === 2 ? '成年期' : '老年期',
state: l,
roleId: generateKeeperId(),
description: 'test breeding' + i + '' + j + '' + k + '' + l + ' description',
}
request.addBreedingPlanRequest(breedingPlan)
breedingPlans.push(breedingPlan)
}
}
}
}
console.log('饲养计划', breedingPlans)
return breedingPlans
}
//生成动物生命阶段1幼年2成长6成年1老年
function generatePhase() {
const phase = Math.floor(Math.random() * 100)
if (phase < 10)
return '幼年期'
if (phase < 30)
return '成长期'
if (phase < 90)
return '成年期'
if (phase < 100)
return '老年期'
}
//生成档案
export async function generateArchives() {
const archives = []
let aid = 0
for (const e of store.state.animals) {
//分为四个周期的档案
for (let i = 0; i < 4; i++) {
let n;
if (i === 0) {
//幼年期5条档案
n = 5
} else if (i === 1) {
//成长期10条档案
n = 10
} else if (i === 2) {
//成年期30条档案
n = 30
} else {
//老年期5条档案
n = 5
}
for (let j = 0; j < n; j++) {
let archive = {
// id: aid,//档案号
animalId: e.id,//该档案记录的动物id
animalName: e.name,//该档案记录的动物名称
phase: i === 0 ? '幼年期' : i === 1 ? '成长期' : i === 2 ? '成年期' : '老年期',
state: generateState(),
type: generateType(),
date: '202' + i + '-' + Math.ceil(Math.random() * 11).toString().padStart(2, '0') + '-' + Math.ceil(Math.random() * 30).toString().padStart(2, '0'),//字符 档案记录日期
time: Math.floor(Math.random() * 24).toString().padStart(2, '0') + ':' + Math.floor(Math.random() * 60).toString().padStart(2, '0') + ':' + Math.floor(Math.random() * 60).toString().padStart(2, '0'),//记录时间
roleId: generateVeterinaryId(),//负责录入档案的人的id身份不限
description: 'test archive' + i + '' + j + ' description',
}
request.addArchiveRequest(archive)
archives.push(archive)
//同时获取一条健康记录
let health = {
// id: aid,//int 健康检测数据号
animalId: archive.animalId,//int 记录的动物id
animalName: archive.animalName,//字符 记录的动物名称
state: archive.state,//int 动物状态 0正常1异常
temperature: Number((35 + Math.random() * 7).toFixed(1)),//double 体温
breathRate: Math.floor(Math.random() * 90) + 10,//int 呼吸频率
heartRate: Math.floor(Math.random() * 130) + 10,//int 心跳频率
bloodPressure: Math.floor(Math.random() * 110) + 70,//int // 血压
date: archive.date,//字符 记录日期
time: archive.time,//字符,记录时间
description: 'test health' + i + '' + j + ' description',//字符 检测的描述,比如动物的症状
}
//异常的记录要修改一下监测数据
if (health.state === 1) {
health.temperature += Math.random() * 2 > 1 ?
Number((Math.random() * health.temperature * 0.5).toFixed(1)) :
-Number((Math.random() * health.temperature * 0.5).toFixed(1))
health.temperature = Number(health.temperature.toFixed(1))
health.breathRate += Math.random() * 2 > 1 ?
Math.floor(Math.random() * health.breathRate * 0.5) : -Math.floor(Math.random() * health.breathRate * 0.5)
health.heartRate += health.heartRate += Math.random() * 2 > 1 ?
Math.floor(Math.random() * health.heartRate * 0.5) : -Math.floor(Math.random() * health.heartRate * 0.5)
health.bloodPressure += health.bloodPressure += Math.random() * 2 > 1 ?
Math.floor(Math.random() * health.bloodPressure * 0.5) : -Math.floor(Math.random() * health.bloodPressure * 0.5)
}
request.addHealthRequest(health)
healths.push(health)
aid++
await sleep(50)
}
}
}
console.log('档案', archives)
return archives
}
function sleep(time){
return new Promise((resolve) => setTimeout(resolve, time));
}
const healths = []
export function generateHealths() {
console.log('健康记录', healths)
return healths
}
function generateType() {
//疫苗接种1疾病治疗1日常饲养8
const type = Math.floor(Math.random() * 100)
if (type < 10) {
return '疫苗接种'
}
if (type < 20) {
return '疾病治疗'
}
if (type < 100) {
return '日常饲养'
}
}
//深拷贝任何值
export function copy(value) {
let result = JSON.stringify(value)
return JSON.parse(result)
}
//鼠标悬浮显示密码
//region
export function showPassword(event, password) {
event.target.innerText = password
}
export function hidePassword(event) {
event.target.innerText = '••••••••'
}
//endregion
//模糊查询
//region
//分割字符串
export function splitKeyWords(keyword) {
let keyset = keyword.split(' ')//提取关键字
keyset = Array.from(new Set(keyset))//关键字去重
// console.log('切割去重好的关键字', keyset)
return keyset;
}
//模糊匹配传入一个包含多个关键字的数组、一个对象若匹配则true
export function fuzzyMatching(object, keyset) {
//一个对象的所有属性能匹配所有关键字就返回trueevery():遇到false就返回false所有true就返回true
return keyset.every(key => {
return keywordMatching(object, key)
})
}
//传入一个关键字一个对象只有关键字能匹配对象的某个属性则true
function keywordMatching(object, keyword) {
//该关键字为空,肯定能匹配上
if (keyword === '') {
// console.log('keywordMatching关键字为空', object, keyword, true)
return true
}
//只要对象的任意属性能匹配一次关键字就返回truesome():遇到true就返回true所有false就返回false
return Object.keys(object).some(key => {
return (object[key] + '').includes(keyword)
})
}
//endregion
//日期时间降序排序
export function sortByDateTime(archives) {
archives.sort((a, b) => {
return Number(b.date.split('-').join('')) - Number(a.date.split('-').join(''))
})
archives.sort((a, b) => {
//日期相同再按时间排序
if (a.date === b.date) {
return Number(b.time.split(':').join('')) - Number(a.time.split(':').join(''))
}
})
return archives
}