Files
CompanyProject/芳林公司项目/www.zhiyuanhelp.com/application/common.php
2024-09-27 01:32:49 +08:00

526 lines
13 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
use app\admin\service\AuthService;
use think\facade\Cache;
/**
* 返回随机字符串
* @param $length 长度
* @return string
*/
function getRandChar($length)
{
$str = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol) - 1;
for ($i = 0;
$i < $length;
$i++) {
$str .= $strPol[rand(0, $max)];
}
return $str;
}
/*
* 科目代号转换
* */
function changeSubString($num){
$result = preg_split('//', $num, -1, PREG_SPLIT_NO_EMPTY);
$str = "";
$conSub = config('setsub.newTestSub');
foreach($result as $val){
$str .= $conSub[$val-1]."/";
}
$arr = substr($str,0,strlen($str)-1);
return $arr;
}
/*
* 新高考科目英文查询赋值数组
* */
function getSubEng($num){
$mySub = preg_split('//', $num, -1, PREG_SPLIT_NO_EMPTY);
$mySub[0] = config('setSub.subEng')[$mySub[0]];
$mySub[1] = config('setSub.subEng')[$mySub[1]];
$mySub[2] = config('setSub.subEng')[$mySub[2]];
return $mySub;
}
/*
* 省份转换real_code转code
* */
function changeCode($str){
$result = explode(',',$str);
$province = config('province.province');
foreach($result as $key=>$val){
$res[$key] = $province[$val]['code'];
}
$result = implode(',',$res);
return $result;
}
/*
* 字符串排序
* */
function sortStr($str){
$arr = str_split($str);
asort($arr);
$str = implode('',$arr);
return $str;
}
/**
* 十进制数转换成62进制
*
* @param integer $num
* @return string
*/
function from10_to62($num) {
$to = 62;
$dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
do {
$ret = $dict[bcmod($num, $to)] . $ret;
$num = bcdiv($num, $to);
} while ($num > 0);
return $ret;
}
if (!function_exists('api_return')) {
/**
* 成功时返回的信息
* @param $msg 消息
* @return \think\response\Json
*/
function api_return($msg, $data = '')
{
return json(['status' => 1, 'msg' => $msg, 'data' => $data]);
}
}
if (!function_exists('api_error')) {
/**
* 失败时返回的信息
* @param $msg 消息
* @return \think\response\Json
*/
function api_error($msg, $data = '')
{
return json(['status' => 0, 'msg' => $msg, 'data' => $data]);
}
}
function curl_post($url, array $params = array())
{
$data_string = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt(
$ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json'
)
);
$data = curl_exec($ch);
curl_close($ch);
return ($data);
}
function curl_post_raw($url, $rawData)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
curl_setopt(
$ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: text'
)
);
$data = curl_exec($ch);
curl_close($ch);
return ($data);
}
/**
* @param string $url get请求地址
* @param int $httpCode 返回状态码
* @return mixed
*/
function curl_get($url, &$httpCode = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//不做证书校验,部署在linux环境下请改为true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $file_contents;
}
function fromArrayToModel($m , $array)
{
foreach ($array as $key => $value)
{
$m[$key] = $value;
}
return $m;
}
if (!function_exists('sysconf')) {
/**
* 获取系统配置
* @param $group 分组
* @param $name 变量名
* @return mixed
*/
function sysconf($group, $name)
{
$value = \app\admin\model\Config::where(['group' => $group, 'name' => $name])->value('value');
return $value;
}
}
if (!function_exists('auth')) {
/**
* 权限节点判断
* @param $node 节点
* @return bool true有权限false无权限
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
function auth($node)
{
return AuthService::checkNode($node);
}
}
if (!function_exists('parseNodeStr')) {
/**
* 驼峰转下划线规则
* @param string $node
* @return string
*/
function parseNodeStr($node)
{
$tmp = [];
foreach (explode('/', $node) as $name) {
$tmp[] = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
return str_replace('._', '.', trim(join('/', $tmp), '/'));
}
}
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('__buildData')) {
/**
* 构建数据
* @param $data 模型数据
* @param $method 模型方法
*/
function __buildData(&$data, $method)
{
foreach ($data as &$vo) {
$vo->$method;
}
}
}
if (!function_exists('alert')) {
/**
* 弹出层提示
* @param string $msg 提示信息
* @param string $url 跳转链接
* @param int $time 停留时间 默认2秒
* @param int $icon 提示图标
* @return string
*/
function alert($msg = '', $url = '', $time = 3, $icon = 6)
{
$success = '<meta name="renderer" content="webkit">';
$success .= '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">';
$success .= '<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">';
$success .= '<script type="text/javascript" src="/static/plugs/jquery/jquery-2.2.4.min.js"></script>';
$success .= '<script type="text/javascript" src="/static/plugs/layui-layer/layer.js"></script>';
if (empty($url)) {
$success .= '<script>$(function(){layer.msg("' . $msg . '", {icon: ' . $icon . ', time: ' . ($time * 1000) . '});})</script>';
} else {
$success .= '<script>$(function(){layer.msg("' . $msg . '",{icon:' . $icon . ',time:' . ($time * 1000) . '});setTimeout(function(){self.location.href="' . $url . '"},2000)});</script>';
}
return $success;
}
}
if (!function_exists('msg_success')) {
/**
* 成功时弹出层提示信息
* @param string $msg 提示信息
* @param string $url 跳转链接
* @param int $time 停留时间 默认2秒
* @param int $icon 提示图标
* @return string
*/
function msg_success($msg = '', $url = '', $time = 3, $icon = 1)
{
return alert($msg, $url, $time, $icon);
}
}
if (!function_exists('msg_error')) {
/**
* 失败时弹出层提示信息
* @param string $msg 提示信息
* @param string $url 跳转链接
* @param int $time 停留时间 默认2秒
* @param int $icon 提示图标
* @return string
*/
function msg_error($msg = '', $url = '', $time = 3, $icon = 2)
{
return alert($msg, $url, $time, $icon);
}
}
if (!function_exists('clear_menu')) {
/**
* 清空菜单缓存
*/
function clear_menu()
{
Cache::clear('menu');
}
}
if (!function_exists('clear_basic')) {
/**
* 清空菜单缓存
*/
function clear_basic()
{
Cache::clear('basic');
}
}
if (!function_exists('get_ip')) {
/**
* 获取用户ip地址
* @return array|false|string
*/
function get_ip()
{
$ip = false;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) {
array_unshift($ips, $ip);
$ip = false;
}
for ($i = 0; $i < count($ips); $i++) {
if (!eregi("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
}
if (!function_exists('get_location')) {
/**
* 根据ip获取地理位置
* @param string $ip
* @return mixed
*/
function get_location($ip = '')
{
empty($ip) && $ip = get_ip();
//接口有问题
// ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3559.6 Safari/537.36');
// $url = "http://ip.taobao.com/service/getIpInfo.php?ip={$ip}";
// $ret = file_get_contents($url);
// $arr = json_decode($ret, true);
// $data =$arr['data'];
$data = [
'ip' => $ip,
'country' => '',
'region' => '',
'city' => '',
'isp' => '',
];
return $data;
}
}
if (!function_exists('__success')) {
/**
* 成功时返回的信息
* @param $msg 消息
* @return \think\response\Json
*/
function __success($msg, $data = '')
{
return json(['code' => 0, 'msg' => $msg, 'data' => $data]);
}
}
if (!function_exists('__error')) {
/**
* 错误时返回的信息
* @param $msg 消息
* @return \think\response\Json
*/
function __error($msg, $data = '')
{
return json(['code' => 1, 'msg' => $msg, 'data' => $data]);
}
}
if (!function_exists('is_mobile')) {
/**
* 判断客户端是否为手机
* @return bool
*/
function is_mobile()
{
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
$is_pc = (strpos($agent, 'windows nt')) ? true : false;
$is_mac = (strpos($agent, 'mac os')) ? true : false;
$is_iphone = (strpos($agent, 'iphone')) ? true : false;
$is_android = (strpos($agent, 'android')) ? true : false;
$is_ipad = (strpos($agent, 'ipad')) ? true : false;
if ($is_pc) return false;
if ($is_mac) return true;
if ($is_iphone) return true;
if ($is_android) return true;
if ($is_ipad) return true;
}
}
if (!function_exists('get_time')) {
/**
* 获取当前时间
* @return false|string
*/
function get_time()
{
return date('Y-m-d H:i:s');
}
}
if (!function_exists('code')) {
/**
* 生成随机数验证码
* @param string $num
* @return int
*/
function code($num = '6')
{
$max = pow(10, $num) - 1;
$min = pow(10, $num - 1);
return rand($min, $max);
}
}
if (!function_exists('get_config')) {
/**
* 获取配置信息
* @param $group
* @param $name
*/
function get_config($group, $name)
{
$value = Db::name('SystemConfig')->where([
'group' => $group,
'name' => $name,
])->value('value');
return $value;
}
}
if (!function_exists('P')) {
/**
* 打印日志
* @param $data
*/
function P($data)
{
\think\facade\Log::record($data, 'record');
}
}