nk-lihaink-cn/app/common.php

265 lines
7.3 KiB
PHP
Raw Normal View History

2021-01-30 20:59:12 +08:00
<?php
// 应用公共文件
use think\facade\Cache;
use think\facade\Config;
2021-02-19 19:07:41 +08:00
use think\facade\Db;
2021-01-30 20:59:12 +08:00
use think\facade\Request;
2021-02-19 19:07:41 +08:00
//设置缓存
function set_cache($key, $value, $date = 86400)
{
Cache::set($key, $value, $date);
}
//读取缓存
function get_cache($key)
{
return Cache::get($key);
}
//清空缓存
function clear_cache($key)
{
Cache::clear($key);
}
//读取文件配置
2021-01-30 20:59:12 +08:00
function get_config($key)
{
return Config::get($key);
}
2021-02-19 19:07:41 +08:00
//读取系统配置
function get_system_config($name,$key)
{
$config=[];
if (get_cache('system_config' . $name)) {
$config = get_cache('system_config' . $name);
} else {
$conf = Db::name('config')->where('name',$name)->find();
if($conf['content']){
$config = unserialize($conf['content']);
}
set_cache('system_config' . $name, $config);
}
if($config[$key]){
return $config[$key];
}
}
2021-01-30 20:59:12 +08:00
//系统信息
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);
}
//判断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) {
//待审核
2021-02-19 19:07:41 +08:00
return '/static/admin/images/none_pic.jpg';
2021-01-30 20:59:12 +08:00
} else {
//不通过
2021-02-19 19:07:41 +08:00
return '/static/admin/images/none_pic.jpg';
2021-01-30 20:59:12 +08:00
}
}
return false;
}
2021-02-23 00:13:40 +08:00
/**
* 判断是否是手机浏览器
*/
function isMobile()
{
if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
return true;
} elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
return true;
} elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
return true;
} elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
return true;
} else {
return false;
}
}