vr-shopxo-source/application/common.php

2189 lines
60 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
// 应用公共文件
/**
* 获取参数数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-23
* @desc description
* @param [string] $key [参数key]
* @param [string] $default [默认值]
*/
function MyInput($key = null, $default = null)
{
static $data = null;
if($data === null)
{
// raw
$raw_data = empty($HTTP_RAW_POST_DATA) ? [] : (!is_array($HTTP_RAW_POST_DATA) ? json_decode($HTTP_RAW_POST_DATA, true) : []);
// request
$request_data = empty($_REQUEST) ? [] : $_REQUEST;
// input
$input_data = file_get_contents("php://input");
$input_data = empty($input_data) ? [] : (!is_array($input_data) ? json_decode($input_data, true) : []);
// 数据合并
$data = array_merge($raw_data, $request_data, $input_data);
}
// 是否指定key
if(!empty($key))
{
if(array_key_exists($key, $data))
{
return is_array($data[$key]) ? $data[$key] : htmlspecialchars(trim($data[$key]));
}
return $default;
}
// 未指定key则返回所有数据
return $data;
}
/**
* 当前应用平台
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-11
* @desc description
*/
function ApplicationClientType()
{
// 平台
$platform = APPLICATION_CLIENT_TYPE;
// web端手机访问
if($platform == 'pc' && IsMobile())
{
$platform = 'h5';
}
return $platform;
}
/**
* 是否微信环境
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-26
* @desc description
* @return [boolean] [否false, 是true]
*/
function IsWeixinEnv()
{
return (!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false);
}
/**
* 笛卡尔积生成规格
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-15
* @desc description
* @param [array] $arr1 [要进行笛卡尔积的二维数组]
* @param [array] $arr2 [最终实现的笛卡尔积组合,可不传]
*/
function SpecCartesian($arr1, $arr2 = [])
{
$result = [];
if(!empty($arr1))
{
// 去除第一个元素
$first = array_splice($arr1, 0, 1);
// 判断是否是第一次进行拼接
if(count($arr2) > 0)
{
foreach($arr2 as $v)
{
foreach($first[0] as $vs)
{
$result[] = $v.','.$vs;
}
}
} else {
foreach($first[0] as $vs)
{
$result[] = $vs;
}
}
// 递归进行拼接
if(count($arr1) > 0)
{
$result = SpecCartesian($arr1, $result);
}
}
return $result;
}
/**
* 后台管理权限校验方法
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-12
* @desc description
* @param [string] $controller [控制器(默认读取当前)]
* @param [string] $action [方法(默认读取当前)]
* @param [array] $unwanted_power [不校验权限的方法(默认空)]
*/
function AdminIsPower($controller = null, $action = null, $unwanted_power = [])
{
// 控制器/方法
$controller = strtolower(empty($controller) ? request()->controller() : $controller);
$action = strtolower(empty($action) ? request()->action() : $action);
// 管理员
$admin = session('admin');
if(!empty($admin))
{
// 不需要校验权限的方法
if(!empty($unwanted_power) && in_array($action, $unwanted_power))
{
return true;
}
// 权限
// 角色组权限列表校验
$power = \app\service\AdminPowerService::PowerData();
if(!empty($power) && is_array($power) && in_array($controller.'_'.$action, $power))
{
return true;
}
}
return false;
}
/**
* 获取数组字段名称
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-12
* @desc description
* @param [array] $data [数组(一维或二维数组)]
*/
function ArrayKeys($data)
{
if(is_array($data))
{
// 是否二维数组
if(isset($data[0]) && is_array($data[0]))
{
return array_keys($data[0]);
}
// 一维数组
return array_keys($data);
}
return [];
}
/**
* 商品销售模式
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-02
* @desc description
* @param [int] $site_type [商品类型]
* @return [int] [销售模式]
*/
function GoodsSalesModelType($site_type)
{
return ($site_type == -1) ? MyC('common_site_type', 0, true) : $site_type;
}
/**
* 商品类型是否与站点类型一致
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-02
* @desc description
* @param [int] $site_type [商品类型]
* @return [int] [一致 1 | 不一致 0]
*/
function IsGoodsSiteTypeConsistent($site_type)
{
// 是否已设置
if($site_type == -1)
{
return 1;
}
// 系统站点类型
$common_site_type = MyC('common_site_type', 0, true);
// 是否一致
if($common_site_type == $site_type)
{
return 1;
}
// 系统是否为 销售+自提,包含其中
if($common_site_type == 4 && in_array($site_type, [0, 2]))
{
return 1;
}
// 不一致
return 0;
}
/**
* 缓存安全验证次数处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-06-03
* @desc description
* @param [string] $key [缓存 key]
* @param [int] $type [操作类型0清除, 1验证]
* @param [int] $expire_time [过期时间默认30秒]
*/
function SecurityPreventViolence($key, $type = 1, $expire_time = 30)
{
// 安全缓存 key
$mkey = md5($key.'_security_prevent_violence');
// 清除缓存返
if($type == 0)
{
cache($mkey, null);
return true;
}
// 验证并增加次数
$count = intval(cache($mkey))+1;
$max = config('shopxo.security_prevent_violence_max');
$status = false;
if($count <= $max)
{
cache($mkey, $count, $expire_time);
$status = true;
}
// 验证达到次数限制则清除验证信息
if($count > $max)
{
cache($key, null);
cache($mkey, null);
}
return $status;
}
/**
* 获取动态表格 form 路径
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-06-05
* @desc description
* @param [array] $params [输入参数]
*/
function FormModulePath($params = [])
{
// 参数变量
$path = '';
$controller = request()->controller();
$action = request()->action();
// 是否插件调用
if($controller == 'Plugins')
{
if(!empty($params['pluginsname']) && !empty($params['pluginscontrol']))
{
$path = '\app\plugins\\'.$params['pluginsname'].'\form\\'.ucfirst($params['pluginscontrol']);
}
} else {
$path = '\app\\'.request()->module().'\form\\'.$controller;
}
return [
'module' => $path,
'action' => $action,
];
}
/**
* 模块视图动态加载方法
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-05-25
* @desc description
* @param [string] $template [视图路径]
* @param [mixed] $data [参数数据]
* @param [mixed] $params [额外参数]
*/
function ModuleInclude($template, $data = [], $params = [])
{
// 应用控制器
$module = '\app\module\ViewIncludeModule';
if(!class_exists($module))
{
return '模块视图控制器未定义['.$module.']';
}
// 调用方法
$action = 'Run';
$obj = new $module();
if(!method_exists($obj, $action))
{
return '模块视图方法未定义['.$module.'->'.$action.'()]';
}
return $obj->Run($template, $data, $params);
}
/**
* 钩子返回数据处理,是否存在错误
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-12-02
* @desc description
* @param [array] $data [钩子返回的数据]
*/
function HookReturnHandle($data)
{
if(!empty($data) && is_array($data))
{
foreach($data as $v)
{
if(is_array($v) && isset($v['code']) && $v['code'] != 0)
{
return $v;
}
}
}
return DataReturn('无钩子信息', 0);
}
/**
* 附件地址处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-10-16
* @desc 用于页面展示处理非绝对路径的情况下自动加上http
* @param [string] $value [附件地址]
*/
function AttachmentPathViewHandle($value)
{
return app\service\ResourcesService::AttachmentPathViewHandle($value);
}
/**
* 路径解析指定参数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-08-06
* @desc description
* @param [string] $key [指定key]
* @param [mixed] $default [默认值]
* @param [string] $path [参数字符串 格式如: a/aa/b/bb/c/cc ]
*/
function PathToParams($key = null, $default = null, $path = '')
{
$data = $_REQUEST;
if(empty($path) && isset($_REQUEST['s']))
{
$path = $_REQUEST['s'];
}
if(!empty($path) && !array_key_exists($key, $data))
{
if(substr($path, 0, 1) == '/')
{
$path = mb_substr($path, 1, mb_strlen($path, 'utf-8')-1, 'utf-8');
}
$position = strrpos($path, '.');
if($position !== false)
{
$path = mb_substr($path, 0, $position, 'utf-8');
}
$arr = explode('/', $path);
$index = 0;
foreach($arr as $k=>$v)
{
if($index != $k)
{
$data[$arr[$index]] = $v;
$index = $k;
}
}
}
if($key !== null)
{
return array_key_exists($key, $data) ? $data[$key] : $default;
}
return $data;
}
/**
* 调用插件服务层方法 - 获取插件配置信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-10-16T22:03:48+0800
* @param [string] $plugins [插件名称]
* @param [array] $attachment_field [自定义附件字段]
* @param [string] $service_name [附件定义的服务层类名]
* @param [string] $attachment_property [附件属性名称]
*/
function CallPluginsData($plugins, $attachment_field = [], $service_name = '', $attachment_property = 'base_config_attachment_field')
{
// 插件是否启用
if(app\service\PluginsService::PluginsStatus($plugins) != 1)
{
return DataReturn('插件状态异常['.$plugins.']', -1);
}
// 查看是否存在基础服务层并且定义获取基础配置方法
$plugins_class = 'app\plugins\\'.$plugins.'\service\BaseService';
if(class_exists($plugins_class) && method_exists($plugins_class, 'BaseConfig'))
{
return $plugins_class::BaseConfig();
}
// 未指定附件字段则自动去获取
$attachment = $attachment_field;
if(empty($attachment_field) && !empty($attachment_property))
{
// 类自定义或者默认两个类
$service_all = empty($service_name) ? ['BaseService', 'Service'] : [$service_name];
foreach($service_all as $service)
{
// 服务层获取附件属性
$plugins_class = 'app\plugins\\'.$plugins.'\service\\'.$service;
if(class_exists($plugins_class) && property_exists($plugins_class, $attachment_property))
{
$attachment = $plugins_class::${$attachment_property};