DiverseYouthNightSchool/application/common/library/Virtual.php

66 lines
1.7 KiB
PHP

<?php
namespace app\common\library;
use app\common\model\school\classes\VirtualHead;
use app\common\model\school\classes\VirtualUser;
/**
* 虚拟数据工具类
*/
class Virtual
{
/** 获得虚拟昵称
* @return mixed|string
*/
public function getNickName(){
$str = config("site.virtual_nickname");
$arr = explode("##",$str);
$r = rand(0,sizeof($arr));
return $arr[$r];
}
public function getVirtualUser($num=10,$classes_lib_id = 0,$time=null,$set=false){
$users = [];
if(!$time)$time = time();
//mysql随机查询$num个数据
$virtualHead = VirtualHead::orderRaw("rand()")->limit($num)->select();
//随机获得$num个虚拟用户昵称和头像
foreach($virtualHead as $v) {
$nickname = $this->getNickName();
$head = $v->head_image;
$r = rand(($time - 86400*7),$time);
$users[] = [
"nickname"=>$nickname,
"head_image"=>$head,
"time"=>$r,
"jointype" => "1",
"havetype" => "".($r % 2)
];
}
if(!$set){
return $users;
}else{
$user_data=[];
foreach($users as $user) {
$r = rand(($time - 86400*7),$time);
$user_data[] = [
"nickname"=>$user["nickname"],
"head_image"=>$user["head_image"],
"classes_lib_id"=>$classes_lib_id,
"time"=>$r,
"jointype" => "1",
"havetype" => "".($r % 2)
];
}
(new VirtualUser)->saveAll($user_data);
return $user_data;
}
}
}