feat: init project codebase

This commit is contained in:
sinde21
2026-09-14 16:32:07 +08:00
parent c4b2a15757
commit da20fde7fb
140 changed files with 27855 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
<?php
class Captcha
{
private $width;
private $height;
private $codeNum;
private $code;
private $im;
function __construct($width = 80, $height = 35, $codeNum = 4)
{
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
}
function showImg()
{
//创建图片
$this->createImg();
//设置干扰元素
$this->setDisturb();
//设置验证码
$this->setCaptcha();
//输出图片
$this->outputImg();
}
function getCaptcha()
{
$this->createCode();
return $this->code;
}
private function createImg()
{
$this->im = imagecreatetruecolor($this->width, $this->height);
// $bgColor = imagecolorallocate($this->im, 0, 0, 0);
$bgColor = imagecolorallocate($this->im, 255, 255, 255);
imagefill($this->im, 0, 0, $bgColor);
}
private function setDisturb()
{
$area = ($this->width * $this->height) / 20;
$disturbNum = ($area > 250) ? 250 : $area;
//加入点干扰
for ($i = 0; $i < $disturbNum; $i++) {
$color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
}
//加入弧线
for ($i = 0; $i <= 5; $i++) {
$color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
}
}
private function createCode()
{
// $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$str = "0123456789";
for ($i = 0; $i < $this->codeNum; $i++) {
$this->code .= $str{rand(0, strlen($str) - 1)};
}
}
private function setCaptcha()
{
for ($i = 0; $i < $this->codeNum; $i++) {
// $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
$color = imagecolorallocate($this->im, rand(0, 50), rand(0, 100), rand(0, 128));
$size = rand(floor($this->height / 5), floor($this->height / 3));
$x = floor($this->width / $this->codeNum) * $i + 5;
$y = rand(0, $this->height - 20);
imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
}
}
private function outputImg()
{
if (imagetypes() & IMG_JPG) {
header('Content-type:image/jpeg');
imagejpeg($this->im);
} elseif (imagetypes() & IMG_GIF) {
header('Content-type: image/gif');
imagegif($this->im);
} elseif (imagetype() & IMG_PNG) {
header('Content-type: image/png');
imagepng($this->im);
} else {
die("Don't support image type!");
}
}
}
+51
View File
@@ -0,0 +1,51 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 压缩整个目录
*/
class MakeZip
{
/**
* description:主方法:生成压缩包
* @author: MY
* @param $dir_path 想要压缩的目录:如 './demo/'
* @param $zipName 压缩后的文件名:如 './folder/demo.zip'
* @return string
*/
function zip($dir_path, $zipName)
{
// Get real path for our folder
$rootPath = realpath($dir_path);
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
return true;
}
}
+340
View File
@@ -0,0 +1,340 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Permission Class
*
* 基础权限类,及生成树类
*
*/
class Permission
{
private $idKey = 'id'; //主键的键名
private $fidKey = 'fid'; //父ID的键名
private $root = 0; //最顶层fid
private $pId = 0; //父fid
private $data = array(); //源数据
private $treeArray = array(); //属性数组
private $state = 'closed'; //默认关闭
/**
* 获得一个带children的树形数组
* @return multitype:
*/
public function getTreeArray($data, $idKey, $fidKey, $root, $closed = '0')
{
if ($idKey) $this->idKey = $idKey;
if ($fidKey) $this->fidKey = $fidKey;
if ($root) $this->root = $root;
if ($data) {
//var_dump($data);
$this->data = $data;
$this->getChildren($this->root, $closed);
}
//去掉键名
//var_dump($this->treeArray);
return array_values($this->treeArray);
}
/**
* @param int $root 父id值
* @return null or array
*/
private function getChildren($root, $closed)
{
$children = '';
foreach ($this->data as &$node) {
if ($root == $node[$this->fidKey]) {
$node['children'] = $this->getChildren($node[$this->idKey], $closed);
$children[] = $node;
}
//只要一级节点
if ($this->root == $node[$this->fidKey]) {
//$s=array('state'=>'close');
//array_push($node,'close');
if ($closed) {
$node['state'] = $this->state;
}
$this->treeArray[$node[$this->idKey]] = $node;
}
}
return $children;
}
/**
* 根据 $token 权限类型 获取该 token->userid->role 对应的所有权限
* @parms$type 根据$perm_type = 'menu'时,判断是否菜单带有功能控件
* return array
*/
function getPermission($token, $perm_type, $menuCtrl = true)
{
$CI = &get_instance();
$CI->load->model('Base_model');
$BasetblArr = $CI->Base_model->getBaseTable($perm_type);
if (empty($BasetblArr)) {
var_dump($this->uri->uri_string . '$this->Base_model->getBaseTable 获取基础表失败...');
return;
}
$tblname = $BasetblArr[0]['r_table'];
$PermArr = $CI->Base_model->getPerm($tblname, $token, $perm_type, $menuCtrl);
return $PermArr;
}
/**
* @parms, 根据$token获取拥有的菜单功能按钮控件权限 与vue前台perm.js 配合判断按钮隐藏与否
* return array
*/
function getMenuCtrlPerm($token)
{
$CI = &get_instance();
$CI->load->model('Base_model');
$PermArr = $CI->Base_model->getCtrlPerm($token);
// 返回样例
// array(2) {
// [0]=>
// array(1) {
// ["path"]=>
// string(13) "/sys/menu/add"
// }
// [1]=>
// array(1) {
// ["path"]=>
// string(14) "/sys/menu/edit"
// }
// }
return $PermArr;
}
/**
* 后端根据 $token,$uri 判断是否该用户是否过期及拥有的功能按钮控件操作权限
* 增/删/改/查 控制器引用
* @parms $token,$uri
* return Array
* return ['code' => 50008, 'message' => "非法的token"];
* return ['code' => 50014, 'message' => "Token 过期了"];
* return ['code' => 50016, 'message' => "无操作权限"];
* return ['code' => 50000, 'message' => "有操作权限"];
*/
function HasPermit($token, $uri)
{
$CI = &get_instance();
$CI->load->model('Base_model');
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
$tokenArr = $CI->Base_model->TokenExpired($token);
if ($tokenArr['code'] != 20000) {
return $tokenArr;
}
$PermArr = $CI->Base_model->getCtrlPerm($token);
// getCtrlPerm 返回样例
// array(2) {
// [0]=>
// array(1) {
// ["path"]=>
// string(13) "/sys/menu/add"
// }
// [1]=>
// array(1) {
// ["path"]=>
// string(14) "/sys/menu/edit"
// }
// }
if (empty($PermArr)) {
return ['code' => 50016, 'message' => "无操作权限 " . $uri, 'data' => $PermArr];
}
// var_dump($this->uri->uri_string); // string(19) "api/v2/sys/menu/add"
foreach ($PermArr as $k => $v) {
if (strpos($uri, $v['path'])) {
return ['code' => 50000, 'message' => "有操作权限 " . $uri, 'data' => $PermArr];
}
}
return ['code' => 50016, 'message' => "无操作权限 " . $uri, 'data' => $PermArr];
}
/**
* 将数据格式化成树形结构路由菜单
*/
function genVueRouter($data, $idKey, $fidKey, $pId)
{
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
if ($v[$fidKey] == $pId) {
// 数据库取出为string类型,强制类型转换成整形,方便前端使用
isset($v['id']) ? $v['id'] = intval($v['id']) : '';
isset($v['pid']) ? $v['pid'] = intval($v['pid']) : '';
isset($v['type']) ? $v['type'] = intval($v['type']) : '';
isset($v['hidden']) ? $v['hidden'] = intval($v['hidden']) : '';
isset($v['listorder']) ? $v['listorder'] = intval($v['listorder']) : '';
// 构造 vue-admin 路由结构 meta
$v['meta'] = [
'title' => $v['title'],
'icon' => $v['icon']
];
unset($v['title']);
unset($v['icon']);
$v['children'] = $this->genVueRouter($data, $idKey, $fidKey, $v[$idKey]);
$tree[] = $v; // 循环数组添加元素 属于同一层级
}
}
// print_r($tree);
return $tree;
}
// 菜单管理 -> 菜单列表
function genVueMenuTree($data, $idKey, $fidKey, $pId)
{
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
if ($v[$fidKey] == $pId) {
// 数据库取出为string类型,强制类型转换成整形,方便前端使用
isset($v['id']) ? $v['id'] = intval($v['id']) : '';
isset($v['pid']) ? $v['pid'] = intval($v['pid']) : '';
isset($v['type']) ? $v['type'] = intval($v['type']) : '';
isset($v['hidden']) ? $v['hidden'] = intval($v['hidden']) : '';
isset($v['listorder']) ? $v['listorder'] = intval($v['listorder']) : '';
$v['children'] = $this->genVueMenuTree($data, $idKey, $fidKey, $v[$idKey]);
// vue treeselect 组件子节点为空时会列出,将空的子节点删除 children key.
if (empty($v['children'])) {
unset($v['children']);
}
$tree[] = $v; // 循环数组添加元素 属于同一层级
// print_r($tree);
}
}
return $tree;
}
/**
* 指定格式两个二维数组比较差集
* @param $array1
* @param $array2
* @return array
*/
// $arr1 = [
// ['role_id'=>1,'perm_id'=>1],
// ['role_id'=>1,'perm_id'=>2]
// ];
function array_diff_assoc2($array1, $array2)
{
$ret = array();
foreach ($array1 as $k => $v) {
# var_dump($v);
$isExist = false;
foreach ($array2 as $k2 => $v2) {
if (empty(array_diff_assoc($v, $v2))) {
$isExist = true;
break;
}
}
if (!$isExist) array_push($ret, $v);
}
return $ret;
}
/**
* 将数据格式化成树形结构
*/
function genTree($data, $idKey, $fidKey, $pId)
{
// $tree = '';
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
// 同时将子节点赋值至该节点的'children'元素,同时判断是否叶子节点
if ($v[$fidKey] == $pId) {
$v['children'] = $this->genTree($data, $idKey, $fidKey, $v[$idKey]);
// print_r($pId);
// $v['isLeaf']=$v['children']?0:1;
// $v['state']=$v['children']?'closed':'open';
$tree[] = $v; // 循环数组添加元素 属于同一层级
// print_r($v);
// print_r($tree);
}
}
return $tree;
}
/**
* 将数据格式化成树形结构 ___非递归方式,使用了数组指针,与前面json方式一样,array 必须以1开头___
* @author Xuefen.Tong
* @param array $items
* @return array
*/
function genTree9($items)
{
$tree = array(); //格式化好的树
foreach ($items as $item)
if (isset($items[$item['pid']]))
$items[$item['pid']]['son'][] = &$items[$item['id']];
else
$tree[] = &$items[$item['id']];
return $tree;
}
/**
* 获取全部机构
* TODO:根据当前USERID 获取用户最高级机构所有下属机构
*/
function getDept()
{
$CI = &get_instance();
$CI->load->model('Dept_model', 'Dept');
$array = $CI->Dept->getDeptids();
// var_dump($array);
$max = 10000;
$j = 0;
for ($i = 0; $i < count($array); $i++) {
// var_dump($array[$i]->DeptId);
$arr = $CI->Dept->getDeptFatherId($array[$i]->DeptId);
// var_dump($arr[0]->FatherLst);
// var_dump(explode(",",$arr[0]->FatherLst));
// var_dump(count(explode(",",$arr[0]->FatherLst)));
$fid_length = count(explode(",", $arr[0]->FatherLst));
if ($fid_length < $max) {
$max = $fid_length;
$j = $i; // 指针父机构最小长度
}
}
$fdeptid = $array[$j]->DeptId; // 最高机构ID
$data = $CI->Dept->getOptDept($fdeptid); //获取下属所有机构树
$ret = $CI->Dept->getFahterId($fdeptid); //获取最高机构直属父ID作为终止节点
$root = $ret[0]->FatherId; // treelib 需要终止节点
// $root 最顶层fid
// $b=$this->treelib->getTreeArray($data,'Id','FatherId',$root,1);
// $b=$this->treelib->getTreeArray($data,'Id','FatherId',$root,1);
// $b=str_replace(',"state":"closed","children":""',',"state":"open","children":""',json_encode($b));
// echo json_encode($b);
// $b=$this->genTree($data,'Id','FatherId',$root);
$b = $this->genTree($data, 'id', 'FatherId', $root);
echo json_encode($b);
}
}
+368
View File
@@ -0,0 +1,368 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Permission Class
*
* 基础权限类,及生成树类
*
*/
class Permission
{
private $idKey = 'id'; //主键的键名
private $fidKey = 'fid'; //父ID的键名
private $root = 0; //最顶层fid
private $pId = 0; //父fid
private $data = array(); //源数据
private $treeArray = array(); //属性数组
private $state = 'closed'; //默认关闭
/**
* 获得一个带children的树形数组
* @return multitype:
*/
public function getTreeArray($data, $idKey, $fidKey, $root, $closed = '0')
{
if ($idKey) $this->idKey = $idKey;
if ($fidKey) $this->fidKey = $fidKey;
if ($root) $this->root = $root;
if ($data) {
//var_dump($data);
$this->data = $data;
$this->getChildren($this->root, $closed);
}
//去掉键名
//var_dump($this->treeArray);
return array_values($this->treeArray);
}
/**
* @param int $root 父id值
* @return null or array
*/
private function getChildren($root, $closed)
{
$children = '';
foreach ($this->data as &$node) {
if ($root == $node[$this->fidKey]) {
$node['children'] = $this->getChildren($node[$this->idKey], $closed);
$children[] = $node;
}
//只要一级节点
if ($this->root == $node[$this->fidKey]) {
//$s=array('state'=>'close');
//array_push($node,'close');
if ($closed) {
$node['state'] = $this->state;
}
$this->treeArray[$node[$this->idKey]] = $node;
}
}
return $children;
}
/**
* 根据 $token 权限类型 获取该 token->userid->role 对应的所有权限
* @parms$type 根据$perm_type = 'menu'时,判断是否菜单带有功能控件
* return array
*/
function getPermission($token, $perm_type, $menuCtrl = true)
{
$CI = &get_instance();
$CI->load->model('Base_model');
$BasetblArr = $CI->Base_model->getBaseTable($perm_type);
if (empty($BasetblArr)) {
var_dump($this->uri->uri_string . '$this->Base_model->getBaseTable 获取基础表失败...');
return;
}
$tblname = $BasetblArr[0]['r_table'];
$PermArr = $CI->Base_model->getPerm($tblname, $token, $perm_type, $menuCtrl);
return $PermArr;
}
/**
* @parms, 根据$token获取拥有的菜单功能按钮控件权限 与vue前台perm.js 配合判断按钮隐藏与否
* return array
*/
function getMenuCtrlPerm($token)
{
$CI = &get_instance();
$CI->load->model('Base_model');
$PermArr = $CI->Base_model->getCtrlPerm($token);
// 返回样例
// array(2) {
// [0]=>
// array(1) {
// ["path"]=>
// string(13) "/sys/menu/add"
// }
// [1]=>
// array(1) {
// ["path"]=>
// string(14) "/sys/menu/edit"
// }
// }
return $PermArr;
}
/**
* 后端根据 $token,$uri 判断是否该用户是否过期及拥有的功能按钮控件操作权限
* 增/删/改/查 控制器引用
* @parms $token,$uri
* return Array
* return ['code' => 50008, 'message' => "非法的token"];
* return ['code' => 50014, 'message' => "Token 过期了"];
* return ['code' => 50016, 'message' => "无操作权限"];
* return ['code' => 50000, 'message' => "有操作权限"];
*/
function HasPermit($token, $uri)
{
$CI = &get_instance();
$CI->load->model('Base_model');
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
$tokenArr = $CI->Base_model->TokenExpired($token);
if ($tokenArr['code'] != 20000) {
return $tokenArr;
}
$PermArr = $CI->Base_model->getCtrlPerm($token);
// getCtrlPerm 返回样例
// array(2) {
// [0]=>
// array(1) {
// ["path"]=>
// string(13) "/sys/menu/add"
// }
// [1]=>
// array(1) {
// ["path"]=>
// string(14) "/sys/menu/edit"
// }
// }
//暂不细分后台权限
return ['code' => 50000, 'message' => "有操作权限 " . $uri, 'data' => $PermArr];
if (empty($PermArr)) {
return ['code' => 50016, 'message' => "无操作权限 " . $uri, 'data' => $PermArr];
}
// var_dump($this->uri->uri_string); // string(19) "api/v2/sys/menu/add"
foreach ($PermArr as $k => $v) {
if (strpos($uri, $v['path'])) {
return ['code' => 50000, 'message' => "有操作权限 " . $uri, 'data' => $PermArr];
}
}
return ['code' => 50016, 'message' => "无操作权限 " . $uri, 'data' => $PermArr];
}
/**
* 将数据格式化成树形结构路由菜单
*/
function genVueRouter($data, $idKey, $fidKey, $pId)
{
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
if ($v[$fidKey] == $pId) {
// 数据库取出为string类型,强制类型转换成整形,方便前端使用
isset($v['id']) ? $v['id'] = intval($v['id']) : '';
isset($v['pid']) ? $v['pid'] = intval($v['pid']) : '';
isset($v['type']) ? $v['type'] = intval($v['type']) : '';
isset($v['hidden']) ? $v['hidden'] = intval($v['hidden']) : '';
isset($v['listorder']) ? $v['listorder'] = intval($v['listorder']) : '';
// 构造 vue-admin 路由结构 meta
$v['meta'] = [
'title' => $v['title'],
'icon' => $v['icon']
];
unset($v['title']);
unset($v['icon']);
$v['children'] = $this->genVueRouter($data, $idKey, $fidKey, $v[$idKey]);
$tree[] = $v; // 循环数组添加元素 属于同一层级
}
}
// print_r($tree);
return $tree;
}
// 菜单管理 -> 菜单列表
function genVueMenuTree($data, $idKey, $fidKey, $pId)
{
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
if ($v[$fidKey] == $pId) {
// 数据库取出为string类型,强制类型转换成整形,方便前端使用
isset($v['id']) ? $v['id'] = intval($v['id']) : '';
isset($v['pid']) ? $v['pid'] = intval($v['pid']) : '';
isset($v['type']) ? $v['type'] = intval($v['type']) : '';
isset($v['hidden']) ? $v['hidden'] = intval($v['hidden']) : '';
isset($v['listorder']) ? $v['listorder'] = intval($v['listorder']) : '';
$v['children'] = $this->genVueMenuTree($data, $idKey, $fidKey, $v[$idKey]);
// vue treeselect 组件子节点为空时会列出,将空的子节点删除 children key.
if (empty($v['children'])) {
unset($v['children']);
}
$tree[] = $v; // 循环数组添加元素 属于同一层级
// print_r($tree);
}
}
return $tree;
}
// 生成部门机构树
function genDeptTree($data, $idKey, $fidKey, $pId)
{
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
if ($v[$fidKey] == $pId) {
// 数据库取出为string类型,强制类型转换成整形,方便前端使用
isset($v['id']) ? $v['id'] = intval($v['id']) : '';
isset($v['pid']) ? $v['pid'] = intval($v['pid']) : '';
isset($v['listorder']) ? $v['listorder'] = intval($v['listorder']) : '';
$v['children'] = $this->genDeptTree($data, $idKey, $fidKey, $v[$idKey]);
// vue treeselect 组件子节点为空时会列出,将空的子节点删除 children key.
if (empty($v['children'])) {
unset($v['children']);
}
$tree[] = $v; // 循环数组添加元素 属于同一层级
}
}
return $tree;
}
/**
* 指定格式两个二维数组比较差集
* @param $array1
* @param $array2
* @return array
*/
// $arr1 = [
// ['role_id'=>1,'perm_id'=>1],
// ['role_id'=>1,'perm_id'=>2]
// ];
function array_diff_assoc2($array1, $array2)
{
$ret = array();
foreach ($array1 as $k => $v) {
# var_dump($v);
$isExist = false;
foreach ($array2 as $k2 => $v2) {
if (empty(array_diff_assoc($v, $v2))) {
$isExist = true;
break;
}
}
if (!$isExist) array_push($ret, $v);
}
return $ret;
}
/**
* 将数据格式化成树形结构
*/
function genTree($data, $idKey, $fidKey, $pId)
{
// $tree = '';
$tree = array();
foreach ($data as $k => $v) {
// 找到父节点为$pId的节点,然后进行递归查找其子节点,
// 同时将子节点赋值至该节点的'children'元素,同时判断是否叶子节点
if ($v[$fidKey] == $pId) {
$v['children'] = $this->genTree($data, $idKey, $fidKey, $v[$idKey]);
// print_r($pId);
// $v['isLeaf']=$v['children']?0:1;
// $v['state']=$v['children']?'closed':'open';
$tree[] = $v; // 循环数组添加元素 属于同一层级
// print_r($v);
// print_r($tree);
}
}
return $tree;
}
/**
* 将数据格式化成树形结构 ___非递归方式,使用了数组指针,与前面json方式一样,array 索引必须以1开头___
* @author Xuefen.Tong
* @param array $items
* @return array
*/
function genTree9($items)
{
$tree = array(); //格式化好的树
foreach ($items as $item)
if (isset($items[$item['pid']]))
$items[$item['pid']]['son'][] = &$items[$item['id']];
else
$tree[] = &$items[$item['id']];
return $tree;
}
/**
* 获取全部机构
* TODO:根据当前USERID 获取用户最高级机构所有下属机构
*/
function getDept()
{
$CI = &get_instance();
$CI->load->model('Dept_model', 'Dept');
$array = $CI->Dept->getDeptids();
// var_dump($array);
$max = 10000;
$j = 0;
for ($i = 0; $i < count($array); $i++) {
// var_dump($array[$i]->DeptId);
$arr = $CI->Dept->getDeptFatherId($array[$i]->DeptId);
// var_dump($arr[0]->FatherLst);
// var_dump(explode(",",$arr[0]->FatherLst));
// var_dump(count(explode(",",$arr[0]->FatherLst)));
$fid_length = count(explode(",", $arr[0]->FatherLst));
if ($fid_length < $max) {
$max = $fid_length;
$j = $i; // 指针父机构最小长度
}
}
$fdeptid = $array[$j]->DeptId; // 最高机构ID
$data = $CI->Dept->getOptDept($fdeptid); //获取下属所有机构树
$ret = $CI->Dept->getFahterId($fdeptid); //获取最高机构直属父ID作为终止节点
$root = $ret[0]->FatherId; // treelib 需要终止节点
// $root 最顶层fid
// $b=$this->treelib->getTreeArray($data,'Id','FatherId',$root,1);
// $b=$this->treelib->getTreeArray($data,'Id','FatherId',$root,1);
// $b=str_replace(',"state":"closed","children":""',',"state":"open","children":""',json_encode($b));
// echo json_encode($b);
// $b=$this->genTree($data,'Id','FatherId',$root);
$b = $this->genTree($data, 'id', 'FatherId', $root);
echo json_encode($b);
}
}
+192
View File
@@ -0,0 +1,192 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 上传及图像处理
*/
class Uploads
{
const DIR = 'uploads';
const DIR_PET_IMG = 'pet_img';
const DIR_PER_CERT_IMG = 'pet_cert_img';
const DIR_PER_CERT_ZIP = 'pet_cert_zip';
public function get_upload_path(){
return FCPATH.self::DIR.DIRECTORY_SEPARATOR;
}
/**
* 上传宠物证书base64
*/
public function save_cert_img_base64($content,$device_id,$filename)
{
$device_id = strtoupper($device_id);
$upload_path = $this->get_upload_path();
$pet_img_path = Uploads::DIR_PER_CERT_IMG.DIRECTORY_SEPARATOR.$device_id.DIRECTORY_SEPARATOR;
$type = $this->base64_image_save(
$content,
$upload_path.$pet_img_path,
$filename
);
if(!$type){
return false;
}
//png转为jpg,降低文件大小
$img = imagecreatefrompng($upload_path.$pet_img_path.$filename.'.'.$type);
if($img){
imagejpeg($img, $upload_path.$pet_img_path.$filename.'.jpg',75);
imagedestroy($img);
unlink($upload_path.$pet_img_path.$filename.'.'.$type);
}
return $filename.'.jpg';
}
/**
* 上传宠物头像
*/
public function save_pet_img_base64($content,$device_id)
{
$device_id = strtoupper($device_id);
$upload_path = $this->get_upload_path();
$pet_img_path = Uploads::DIR_PET_IMG.DIRECTORY_SEPARATOR;
$type = $this->base64_image_save(
$content,
$upload_path.$pet_img_path,
$device_id.'_ori'
);
if(!$type){
return false;
}
$source = $pet_img_path.$device_id.'_ori.'.$type;
$target = $pet_img_path.$device_id.'_300.'.$type;
$ret = $this->image_center_crop($upload_path.$source, 300, 300, $upload_path.$target);
if(!$ret){
return $source;
}
return $target;
}
/**
* 上传图片
*/
public function upload_img($arr)
{
$list = array();
foreach ($arr as $k => $v) {
$name = $v['name'];
$type = strtolower(substr($name, strrpos($name, '.') + 1)); // 得到文件类型,并且都转化成小写
$allow_type = array(
'jpg',
'jpeg',
'gif',
'png',
'mp4',
); // 定义允许上传的类型
// 判断文件类型是否被允许上传
if (! in_array($type, $allow_type)) {
// 如果不被允许,则直接停止程序运行
return;
}
// 判断是否是通过HTTP POST上传的
if (! is_uploaded_file($v['tmp_name'])) {
// 如果不是通过HTTP POST上传的
return;
}
$upload_path = FCPATH.self::DIR.DIRECTORY_SEPARATOR."users_info_img/";
if (! file_exists($upload_path)) {
mkdir($upload_path);
}
$img_id = time() . rand(1, 100) . "." . $type;
$list[$k] ='users_info_img/'.$img_id;
if (move_uploaded_file($v['tmp_name'], $upload_path . $img_id)) {
} else {
return;
}
}
return $list[0];
}
/* base64格式编码转换为图片并保存对应文件夹 */
function base64_image_save($base64_image_content,$path,$name)
{
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
$type = $result[2] === 'jpeg'?'jpg':$result[2];
if (!file_exists($path)) {
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($path, 0755);
}
$new_file = $name . ".{$type}";
if (file_put_contents($path . $new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
return $type;
} else {
return false;
}
} else {
return false;
}
}
/**
* 居中裁剪图片
* @param string $source [原图路径]
* @param int $width [设置宽度]
* @param int $height [设置高度]
* @param string $target [目标路径]
* @return bool [裁剪结果]
*/
public function image_center_crop($source, $width, $height, $target)
{
if (!file_exists($source)) return false;
/* 根据类型载入图像 */
switch (exif_imagetype($source)) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($source);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($source);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($source);
break;
}
if (!isset($image)) return false;
/* 获取图像尺寸信息 */
$target_w = $width;
$target_h = $height;
$source_w = imagesx($image);
$source_h = imagesy($image);
/* 计算裁剪宽度和高度 */
$judge = (($source_w / $source_h) > ($target_w / $target_h));
$resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w;
$resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h;
$start_x = $judge ? ($resize_w - $target_w) / 2 : 0;
$start_y = !$judge ? ($resize_h - $target_h) / 2 : 0;
/* 绘制居中缩放图像 */
$resize_img = imagecreatetruecolor($resize_w, $resize_h);
imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h);
$target_img = imagecreatetruecolor($target_w, $target_h);
imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h);
/* 将图片保存至文件 */
if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true);
switch (exif_imagetype($source)) {
case IMAGETYPE_JPEG:
imagejpeg($target_img, $target);
break;
case IMAGETYPE_PNG:
imagepng($target_img, $target);
break;
case IMAGETYPE_GIF:
imagegif($target_img, $target);
break;
}
return boolval(file_exists($target));
}
}
+11
View File
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>