php管理和接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

215 lines
6.2 KiB

<?php
// 应用公共文件
use app\common\service\AuthService;
use cores\exception\BaseException;
use think\facade\Cache;
if (!function_exists('__url')) {
/**
* 构建URL地址
* @param string $url
* @param array $vars
* @param bool $suffix
* @param bool $domain
* @return string
*/
function __url(string $url = '', array $vars = [], $suffix = true, $domain = false)
{
return url($url, $vars, $suffix, $domain)->build();
}
}
/**
* 随机盐值
* @param int $len
* @return string
*/
function makeSalt(int $len){
$ss = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$salt = '';
for ($i = 0; $i < $len; $i++) {
$salt .= $ss[mt_rand(0, strlen($ss) - 1)];
}
return $salt;
}
if (!function_exists('password')) {
/**
* 密码加密算法
* @param $value 需要加密的值
* @param $type 加密类型,默认为md5 (md5, hash)
* @return mixed
*/
function password($value)
{
$value = sha1('blog_') . md5($value) . md5('_encrypt') . sha1($value);
return sha1($value);
}
}
if (!function_exists('xdebug')) {
/**
* debug调试
* @deprecated 不建议使用,建议直接使用框架自带的log组件
* @param string|array $data 打印信息
* @param string $type 类型
* @param string $suffix 文件后缀名
* @param bool $force
* @param null $file
*/
function xdebug($data, $type = 'xdebug', $suffix = null, $force = false, $file = null)
{
!is_dir(runtime_path() . 'xdebug/') && mkdir(runtime_path() . 'xdebug/');
if (is_null($file)) {
$file = is_null($suffix) ? runtime_path() . 'xdebug/' . date('Ymd') . '.txt' : runtime_path() . 'xdebug/' . date('Ymd') . "_{$suffix}" . '.txt';
}
file_put_contents($file, "[" . date('Y-m-d H:i:s') . "] " . "========================= {$type} ===========================" . PHP_EOL, FILE_APPEND);
$str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . PHP_EOL;
$force ? file_put_contents($file, $str) : file_put_contents($file, $str, FILE_APPEND);
}
}
if (!function_exists('sysconfig')) {
/**
* 获取系统配置信息
* @param $group
* @param null $name
* @return array|mixed
*/
function sysconfig($group, $name = null)
{
$where = ['group' => $group];
$value = empty($name) ? Cache::get("sysconfig_{$group}") : Cache::get("sysconfig_{$group}_{$name}");
if (empty($value)) {
if (!empty($name)) {
$where['name'] = $name;
$value = \app\admin\model\SystemConfig::where($where)->value('value');
Cache::tag('sysconfig')->set("sysconfig_{$group}_{$name}", $value, 3600);
} else {
$value = \app\admin\model\SystemConfig::where($where)->column('value', 'name');
Cache::tag('sysconfig')->set("sysconfig_{$group}", $value, 3600);
}
}
return $value;
}
}
if (!function_exists('array_format_key')) {
/**
* 二位数组重新组合数据
* @param $array
* @param $key
* @return array
*/
function array_format_key($array, $key)
{
$newArray = [];
foreach ($array as $vo) {
$newArray[$vo[$key]] = $vo;
}
return $newArray;
}
}
if (!function_exists('auth')) {
/**
* auth权限验证
* @param $node
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
function auth($node = null)
{
$authService = new AuthService(session('admin.id'));
$check = $authService->checkNode($node);
return $check;
}
}
/**
* 输出错误信息
* @param string $message 报错信息
* @param int|null $status 状态码,默认为配置文件status.error
* @param array $data 附加数据
* @throws BaseException
*/
function throwError(string $message, ?int $status = null, array $data = [])
{
is_null($status) && $status = config('status.error');
throw new BaseException(['status' => $status, 'message' => $message, 'data' => $data]);
}
/**
* 获取全局唯一标识符
* @param bool $trim
* @return string
*/
function get_guid_v4(bool $trim = true): string
{
// Windows
if (function_exists('com_create_guid') === true) {
$charid = com_create_guid();
return $trim ? trim($charid, '{}') : $charid;
}
// OSX/Linux
if (function_exists('openssl_random_pseudo_bytes') === true) {
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
// Fallback (PHP 4.2+)
mt_srand(intval((double)microtime() * 10000));
$charid = strtolower(md5(uniqid((string)rand(), true)));
$hyphen = chr(45); // "-"
$lbrace = $trim ? "" : chr(123); // "{"
$rbrace = $trim ? "" : chr(125); // "}"
return $lbrace .
substr($charid, 0, 8) . $hyphen .
substr($charid, 8, 4) . $hyphen .
substr($charid, 12, 4) . $hyphen .
substr($charid, 16, 4) . $hyphen .
substr($charid, 20, 12) .
$rbrace;
}
if (!function_exists('array_del_key')) {
function array_del_key(&$data, array $del_key) {
foreach ($data as $key => $value) {
foreach ($value as $field => $v) {
if (!in_array($field, $del_key)) {
unset($data[$key][$field]);
}
}
}
}
}
if (!function_exists('get_datetime')) {
function get_datetime($time, $type = 'datetime'): string {
if (!$time || !is_numeric($time)) return '';
if ($type === 'date') {
$str = 'Y-m-d';
} else if ($type === 'dateHi') {
$str = 'Y-m-d H:i';
} else {
$str = 'Y-m-d H:i:s';
}
return date($str, $time);
}
}