2 Star 0 Fork 0

积木云插件/开发示例

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
BaseController.php 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
/**
* 积木云渲染器
*
* @package XbCode
* @author 楚羽幽 <958416459@qq.com>
* @version 1.0
* @license Apache License 2.0
* @link http://www.xbcode.net
* @document http://doc.xbcode.net
*/
namespace plugin\xbDevExample\app\admin;
use plugin\xbCode\XbController;
use support\Cache;
/**
* 控制器基类
* @copyright 贵州小白基地网络科技有限公司
* @author 楚羽幽 cy958416459@qq.com
*/
class BaseController extends XbController
{
/**
* 获取普通列表数据
* @copyright 贵州积木云网络科技有限公司
* @author 楚羽幽 958416459@qq.com
*/
protected function getList()
{
$key = request()->path();
$key = ltrim($key, '/');
$key = rtrim($key,'/');
$key = str_replace('/', '_', $key);
$key = str_replace('.', '_', $key);
$key = str_replace('\\', '_', $key);
$data = Cache::get($key);
if (empty($data)) {
$data = $this->generate();
Cache::set($key, $data, 60 * 60 * 24);
}
return $data;
}
/**
* 获取树形列表数据
* @copyright 贵州积木云网络科技有限公司
* @author 楚羽幽 958416459@qq.com
*/
protected function getTreeList()
{
$data = $this->getList();
// 每10条数据作为一组,children作为子级
$groupedData = [];
foreach ($data as $item) {
$groupIndex = floor(($item['id'] - 1) / 10);
if (!isset($groupedData[$groupIndex])) {
$groupedData[$groupIndex] = array_merge($item, [
'id'=> $groupIndex + 1,
'children' => []
]);
}
$groupedData[$groupIndex]['children'][] = $item;
}
// 重置索引
$data = array_values($groupedData);
return $data;
}
/**
* 生成模拟数据
* @return array<array>
* @copyright 贵州积木云网络科技有限公司
* @author 楚羽幽 958416459@qq.com
*/
private function generate()
{
$faker = \Poppy\Faker\Factory::create('zh_CN');
$sexText = ['男生', '女生', '保密'];
for ($i = 0; $i < 50; $i++) {
$data[$i]['id'] = $i + 1;
$data[$i]['name'] = $faker->name;
$sex = $faker->numberBetween(0, 2);
$data[$i]['sex'] = $sex;
$data[$i]['sex_text'] = $sexText[$sex];
$age = $faker->numberBetween(16, 50);
$data[$i]['age'] = $age;
$data[$i]['age_text'] = $age. '岁';
$data[$i]['idnumber'] = $faker->idNumber;
$data[$i]['address'] = "{$faker->state} {$faker->address}";
$data[$i]['phone'] = $faker->phoneNumber;
$data[$i]['email'] = $faker->email;
$data[$i]['balance'] = $faker->randomFloat(2,0, 1000000);
$data[$i]['create_at'] = $faker->dateTimeBetween('-30 years', 'now')->format('Y-m-d H:i:s') ?? '';
}
return $data;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/xbcode-plugin/xbDevExample.git
git@gitee.com:xbcode-plugin/xbDevExample.git
xbcode-plugin
xbDevExample
开发示例
main

搜索帮助