225 lines
6.0 KiB
PHP
225 lines
6.0 KiB
PHP
<?php
|
||
// 应用公共文件
|
||
use think\facade\Cache;
|
||
use think\facade\Config;
|
||
use think\facade\Request;
|
||
//取系统配置
|
||
function get_config($key)
|
||
{
|
||
return Config::get($key);
|
||
}
|
||
|
||
//系统信息
|
||
function get_system_info($key)
|
||
{
|
||
$system = [
|
||
'os' => PHP_OS,
|
||
'php' => PHP_VERSION,
|
||
'upload_max_filesize' => get_cfg_var("upload_max_filesize") ? get_cfg_var("upload_max_filesize") : "不允许上传附件",
|
||
'max_execution_time' => get_cfg_var("max_execution_time") . "秒 ",
|
||
];
|
||
if (empty($key)) {
|
||
return $system;
|
||
} else {
|
||
return $system[$key];
|
||
}
|
||
}
|
||
|
||
//获取url参数
|
||
function get_params($key = "")
|
||
{
|
||
return Request::instance()->param($key);
|
||
}
|
||
|
||
//生成一个不会重复的字符串
|
||
function make_token()
|
||
{
|
||
$str = md5(uniqid(md5(microtime(true)), true));
|
||
$str = sha1($str); //加密
|
||
return $str;
|
||
}
|
||
|
||
//随机字符串,默认长度10
|
||
function set_salt($num = 10)
|
||
{
|
||
$str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
|
||
$salt = substr(str_shuffle($str), 10, $num);
|
||
return $salt;
|
||
}
|
||
//密码加密
|
||
function set_password($pwd, $salt)
|
||
{
|
||
return md5(md5($pwd . $salt) . $salt);
|
||
}
|
||
|
||
//设置缓存
|
||
function set_cache($key, $value, $date = 86400)
|
||
{
|
||
Cache::set($key, $value, $date);
|
||
}
|
||
|
||
//读取缓存
|
||
function get_cache($key)
|
||
{
|
||
Cache::get($key);
|
||
}
|
||
|
||
//情况缓存
|
||
function clear_cache($key)
|
||
{
|
||
Cache::clear($key);
|
||
}
|
||
|
||
//判断cms是否完成安装
|
||
function is_installed()
|
||
{
|
||
static $isInstalled;
|
||
if (empty($isInstalled)) {
|
||
$isInstalled = file_exists(CMS_ROOT . 'config/install.lock');
|
||
}
|
||
return $isInstalled;
|
||
}
|
||
|
||
/**
|
||
* 返回json数据,用于接口
|
||
* @param integer $code
|
||
* @param string $msg
|
||
* @param array $data
|
||
* @param string $url
|
||
* @param integer $httpCode
|
||
* @param array $header
|
||
* @param array $options
|
||
* @return json
|
||
*/
|
||
function to_assign($code = 1, $msg = "操作成功", $data = [], $url = '', $httpCode = 200, $header = [], $options = [])
|
||
{
|
||
$res = ['code' => $code];
|
||
$res['msg'] = $msg;
|
||
$res['url'] = $url;
|
||
if (is_object($data)) {
|
||
$data = $data->toArray();
|
||
}
|
||
$res['data'] = $data;
|
||
$response = \think\Response::create($res, "json", $httpCode, $header, $options);
|
||
throw new \think\exception\HttpResponseException($response);
|
||
}
|
||
|
||
/**
|
||
* 适配layui数据列表的返回数据方法,用于接口
|
||
* @param integer $code
|
||
* @param string $msg
|
||
* @param array $data
|
||
* @param integer $httpCode
|
||
* @param array $header
|
||
* @param array $options
|
||
* @return json
|
||
*/
|
||
function table_assign($code = 0, $msg = '请求成功', $data = [], $httpCode = 200, $header = [], $options = [])
|
||
{
|
||
$res['code'] = $code;
|
||
$res['msg'] = $msg;
|
||
if (is_object($data)) {
|
||
$data = $data->toArray();
|
||
}
|
||
if (!empty($data['total'])) {
|
||
$res['count'] = $data['total'];
|
||
} else {
|
||
$res['count'] = 0;
|
||
}
|
||
$res['data'] = $data['data'];
|
||
$response = \think\Response::create($res, "json", $httpCode, $header, $options);
|
||
throw new \think\exception\HttpResponseException($response);
|
||
}
|
||
|
||
/**
|
||
* 间隔时间段格式化
|
||
* @param int $time 时间戳
|
||
* @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】
|
||
* @return string
|
||
*/
|
||
function time_trans($time, $format = 'd')
|
||
{
|
||
$now = time();
|
||
$diff = $now - $time;
|
||
if ($diff < 60) {
|
||
return '1分钟前';
|
||
} else if ($diff < 3600) {
|
||
return floor($diff / 60) . '分钟前';
|
||
} else if ($diff < 86400) {
|
||
return floor($diff / 3600) . '小时前';
|
||
}
|
||
$yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间
|
||
$yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间
|
||
$two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间
|
||
$three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间
|
||
$four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间
|
||
$five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间
|
||
$six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间
|
||
$seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间
|
||
|
||
if ($time > $yes_start_time && $time < $yes_end_time) {
|
||
return '昨天';
|
||
}
|
||
|
||
if ($time > $yes_start_time && $time < $two_end_time) {
|
||
return '1天前';
|
||
}
|
||
|
||
if ($time > $yes_start_time && $time < $three_end_time) {
|
||
return '2天前';
|
||
}
|
||
|
||
if ($time > $yes_start_time && $time < $four_end_time) {
|
||
return '3天前';
|
||
}
|
||
|
||
if ($time > $yes_start_time && $time < $five_end_time) {
|
||
return '4天前';
|
||
}
|
||
|
||
if ($time > $yes_start_time && $time < $six_end_time) {
|
||
return '5天前';
|
||
}
|
||
|
||
if ($time > $yes_start_time && $time < $seven_end_time) {
|
||
return '6天前';
|
||
}
|
||
|
||
switch ($format) {
|
||
case 'd':
|
||
$show_time = date('Y-m-d', $time);
|
||
break;
|
||
case 'i':
|
||
$show_time = date('Y-m-d H:i', $time);
|
||
break;
|
||
case 's':
|
||
$show_time = date('Y-m-d H:i:s', $time);
|
||
break;
|
||
}
|
||
return $show_time;
|
||
}
|
||
|
||
/**
|
||
* 根据附件表的id返回url地址
|
||
* @param [type] $id [description]
|
||
*/
|
||
function get_file($id)
|
||
{
|
||
if ($id) {
|
||
$geturl = \think\facade\Db::name("file")->where(['id' => $id])->find();
|
||
if ($geturl['status'] == 1) {
|
||
//审核通过
|
||
//获取签名的URL
|
||
$url = $geturl['filepath'];
|
||
return $url;
|
||
} elseif ($geturl['status'] == 0) {
|
||
//待审核
|
||
return '/themes/admin_themes/lib/static/images/none_pic.jpg';
|
||
} else {
|
||
//不通过
|
||
return '/themes/admin_themes/lib/static/images/none_pic.jpg';
|
||
}
|
||
}
|
||
return false;
|
||
}
|