You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

923 lines
24 KiB
PHP

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
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
/**
* 后台公共文件
* 主要定义后台公共函数库
*/
/* 解析列表定义规则*/
function get_list_field($data, $grid)
{
// 获取当前字段数据
foreach ($grid['field'] as $field) {
$array = explode('|', $field);
$temp = $data[$array[0]];
$twotemp = $data[$array[2]];//新加
// 函数支持
if (isset($array[1])) {
if (isset($twotemp)) {//新加
$param = explode('*', $array[1]);
$temp = isset($param[1]) ? call_user_func($param[0], $temp, $param[1]) : call_user_func($array[1], $twotemp, $temp);
} else {
$param = explode('*', $array[1]);
$temp = isset($param[1]) ? call_user_func($param[0], $temp, $param[1]) : call_user_func($array[1], $temp);
}
}
$data2[$array[0]] = $temp;
}
if (!empty($grid['format'])) {
$value = preg_replace_callback('/\[([a-z_]+)\]/', function ($match) use ($data2) {
return $data2[$match[1]];
}, $grid['format']);
} else {
$value = implode(' ', $data2);
}
// 链接支持
if ('title' == $grid['field'][0] && '目录' == $data['type']) {
// 目录类型自动设置子文档列表链接
$grid['href'] = '[LIST]';
}
if (!empty($grid['href'])) {
$links = explode(',', $grid['href']);
foreach ($links as $link) {
$array = explode('|', $link);
$href = $array[0];
if (preg_match('/^\[([a-z_]+)\]$/', $href, $matches)) {
$val[] = $data2[$matches[1]];
} else {
$show = isset($array[1]) ? $array[1] : $value;
// 替换系统特殊字符串
$href = str_replace(
array('[DELETE]', '[EDIT]', '[LIST]'),
array('setstatus?status=-1&ids=[id]',
'edit?id=[id]&model=[model_id]&cate_id=[category_id]',
'index?pid=[id]&model=[model_id]&cate_id=[category_id]'),
$href);
// 替换数据变量
$href = preg_replace_callback('/\[([a-z_]+)\]/', function ($match) use ($data) {
return $data[$match[1]];
}, $href);
switch ($show) {
case '删除':
$val[] = '<a href="' . U($href) . '" class="ajax-get confirm" target-form="ids" >' . $show . '</a>';
break;
default:
$val[] = '<a href="' . U($href) . '">' . $show . '</a>';
break;
}
}
}
$value = implode(' ', $val);
}
return $value;
}
/**
* [every_day 获取日期]
* @param integer $m [description]
* @return [type] [array]
*/
function every_day($m = 7)
{
$time = array();
for ($i = $m - 1; $i >= 0; $i--) {
$time[] = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') - $i, date('Y')));
}
return $time;
}
// 两个日期之间的所有日期
function prDates($start, $end)
{
$dt_start = strtotime($start);
$dt_end = strtotime($end);
while ($dt_start <= $dt_end) {
$tt[] = date('Y-m-d', $dt_start);
$dt_start = strtotime('+1 day', $dt_start);
}
return $tt;
}
/* 解析插件数据列表定义规则*/
function get_addonlist_field($data, $grid, $addon)
{
// 获取当前字段数据
foreach ($grid['field'] as $field) {
$array = explode('|', $field);
$temp = $data[$array[0]];
// 函数支持
if (isset($array[1])) {
$temp = call_user_func($array[1], $temp);
}
$data2[$array[0]] = $temp;
}
if (!empty($grid['format'])) {
$value = preg_replace_callback('/\[([a-z_]+)\]/', function ($match) use ($data2) {
return $data2[$match[1]];
}, $grid['format']);
} else {
$value = implode(' ', $data2);
}
// 链接支持
if (!empty($grid['href'])) {
$links = explode(',', $grid['href']);
foreach ($links as $link) {
$array = explode('|', $link);
$href = $array[0];
if (preg_match('/^\[([a-z_]+)\]$/', $href, $matches)) {
$val[] = $data2[$matches[1]];
} else {
$show = isset($array[1]) ? $array[1] : $value;
// 替换系统特殊字符串
$href = str_replace(
array('[DELETE]', '[EDIT]', '[ADDON]'),
array('del?ids=[id]&name=[ADDON]', 'edit?id=[id]&name=[ADDON]', $addon),
$href);
// 替换数据变量
$href = preg_replace_callback('/\[([a-z_]+)\]/', function ($match) use ($data) {
return $data[$match[1]];
}, $href);
$val[] = '<a href="' . U($href) . '">' . $show . '</a>';
}
}
$value = implode(' ', $val);
}
return $value;
}
// 获取模型名称
function get_model_by_id($id)
{
return $model = M('Model')->getFieldById($id, 'title');
}
// 获取属性类型信息
function get_attribute_type($type = '')
{
// TODO 可以加入系统配置
static $_type = array(
'num' => array('数字', 'int(10) UNSIGNED NOT NULL'),
'string' => array('字符串', 'varchar(255) NOT NULL'),
'textarea' => array('文本框', 'text NOT NULL'),
'date' => array('日期', 'int(10) NOT NULL'),
'datetime' => array('时间', 'int(10) NOT NULL'),
'bool' => array('布尔', 'tinyint(2) NOT NULL'),
'select' => array('枚举', 'char(50) NOT NULL'),
'radio' => array('单选', 'char(10) NOT NULL'),
'checkbox' => array('多选', 'varchar(100) NOT NULL'),
'editor' => array('编辑器', 'text NOT NULL'),
'picture' => array('上传图片', 'int(10) UNSIGNED NOT NULL'),
'file' => array('上传附件', 'int(10) UNSIGNED NOT NULL'),
);
return $type ? $_type[$type][0] : $_type;
}
/**
* 获取对应状态的文字信息
* @param int $status
* @return string 状态文字 false 未获取到
* @author huajie <banhuajie@163.com>
*/
function get_status_title($status = null)
{
if (!isset($status)) {
return false;
}
switch ($status) {
case -1 :
return "已删除";
break;
case 0 :
return "禁用";
break;
case 1 :
return "正常";
break;
case 2 :
return "待审核";
break;
default :
return false;
break;
}
}
// 获取数据的状态操作
function show_status_op($status)
{
switch ($status) {
case 0 :
return "正常";
break;
case 1 :
return "禁用";
break;
case 2 :
return "待审核";
break;
default :
return false;
break;
}
}
/**
* 获取文档的类型文字
* @param string $type
* @return string 状态文字 false 未获取到
* @author huajie <banhuajie@163.com>
*/
function get_document_type($type = null)
{
if (!isset($type)) {
return false;
}
switch ($type) {
case 1 :
return '目录';
break;
case 2 :
return '主题';
break;
case 3 :
return '段落';
break;
default :
return false;
break;
}
}
/**
* 获取配置的类型
* @param string $type 配置类型
* @return string
*/
function get_config_type($type = 0)
{
$list = C('CONFIG_TYPE_LIST');
return $list[$type];
}
/**
* 获取配置的分组
* @param string $group 配置分组
* @return string
*/
function get_config_group($group = 0)
{
$list = C('CONFIG_GROUP_LIST');
return $group ? $list[$group] : '';
}
/**
* select返回的数组进行整数映射转换
*
* @param array $map 映射关系二维数组 array(
* '字段名1'=>array(映射关系数组),
* '字段名2'=>array(映射关系数组),
* ......
* )
* @return array
*
* array(
* array('id'=>1,'title'=>'标题','status'=>'1','status_text'=>'正常')
* ....
* )
*
* @author 朱亚杰 <zhuyajie@topthink.net>
*/
function int_to_string(&$data, $map = array('status' => array(1 => '正常', -1 => '删除', 0 => '锁定', 2 => '未审核', 3 => '草稿')))
{
if ($data === false || $data === null) {
return $data;
}
$data = (array)$data;
foreach ($data as $key => $row) {
foreach ($map as $col => $pair) {
if (isset($row[$col]) && isset($pair[$row[$col]])) {
$data[$key][$col . '_text'] = $pair[$row[$col]];
}
}
}
return $data;
}
/**
* 动态扩展左侧菜单,base.html里用到
* @author 朱亚杰 <zhuyajie@topthink.net>
*/
function extra_menu($extra_menu, &$base_menu)
{
foreach ($extra_menu as $key => $group) {
if (isset($base_menu['child'][$key])) {
$base_menu['child'][$key] = array_merge($base_menu['child'][$key], $group);
} else {
$base_menu['child'][$key] = $group;
}
}
}
/**
* 获取参数的所有父级分类
* @param int $cid 分类id
* @return array 参数分类和父类的信息集合
* @author huajie <banhuajie@163.com>
*/
function get_parent_category($cid)
{
if (empty($cid)) {
return false;
}
$cates = M('Category')->where(array('status' => 1))->field('id,title,pid')->order('sort')->select();
$child = get_category($cid); //获取参数分类的信息
$pid = $child['pid'];
$temp = array();
$res[] = $child;
while (true) {
foreach ($cates as $key => $cate) {
if ($cate['id'] == $pid) {
$pid = $cate['pid'];
array_unshift($res, $cate); //将父分类插入到数组第一个元素前
}
}
if ($pid == 0) {
break;
}
}
return $res;
}
/**
* 检测验证码
* @param integer $id 验证码ID
* @return boolean 检测结果
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function check_verify($code, $id = 1)
{
$verify = new \Think\Verify();
return $verify->check($code, $id);
}
/**
* 获取当前分类的文档类型
* @param int $id
* @return array 文档类型数组
* @author huajie <banhuajie@163.com>
*/
function get_type_bycate($id = null)
{
if (empty($id)) {
return false;
}
$type_list = C('DOCUMENT_MODEL_TYPE');
$model_type = M('Category')->getFieldById($id, 'type');
$model_type = explode(',', $model_type);
foreach ($type_list as $key => $value) {
if (!in_array($key, $model_type)) {
unset($type_list[$key]);
}
}
return $type_list;
}
/**
* 获取当前文档的分类
* @param int $id
* @return array 文档类型数组
* @author huajie <banhuajie@163.com>
*/
function get_cate($cate_id = null)
{
if (empty($cate_id)) {
return false;
}
$cate = M('Category')->where('id=' . $cate_id)->getField('title');
return $cate;
}
// 分析枚举类型配置值 格式 a:名称1,b:名称2
function parse_config_attr($string)
{
$array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
if (strpos($string, ':')) {
$value = array();
foreach ($array as $val) {
list($k, $v) = explode(':', $val);
$value[$k] = $v;
}
} else {
$value = $array;
}
return $value;
}
// 获取子文档数目
function get_subdocument_count($id = 0)
{
return M('Document')->where('pid=' . $id)->count();
}
// 分析枚举类型字段值 格式 a:名称1,b:名称2
// 暂时和 parse_config_attr功能相同
// 但请不要互相使用,后期会调整
function parse_field_attr($string)
{
if (0 === strpos($string, ':')) {
// 采用函数定义
return eval('return ' . substr($string, 1) . ';');
} elseif (0 === strpos($string, '[')) {
// 支持读取配置参数(必须是数组类型)
return C(substr($string, 1, -1));
}
$array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
if (strpos($string, ':')) {
$value = array();
foreach ($array as $val) {
list($k, $v) = explode(':', $val);
$value[$k] = $v;
}
} else {
$value = $array;
}
return $value;
}
/**
* 获取行为数据
* @param string $id 行为id
* @param string $field 需要获取的字段
* @author huajie <banhuajie@163.com>
*/
function get_action($id = null, $field = null)
{
if (empty($id) && !is_numeric($id)) {
return false;
}
$list = S('action_list');
if (empty($list[$id])) {
$map = array('status' => array('gt', -1), 'id' => $id);
$list[$id] = M('Action')->where($map)->field(true)->find();
}
return empty($field) ? $list[$id] : $list[$id][$field];
}
/**
* 根据条件字段获取数据
* @param mixed $value 条件,可用常量或者数组
* @param string $condition 条件字段
* @param string $field 需要返回的字段,不传则返回整个数据
* @author huajie <banhuajie@163.com>
*/
function get_document_field($value = null, $condition = 'id', $field = null)
{
if (empty($value)) {
return false;
}
//拼接参数
$map[$condition] = $value;
$info = M('Model')->where($map);
if (empty($field)) {
$info = $info->field(true)->find();
} else {
$info = $info->getField($field);
}
return $info;
}
/**
* 获取行为类型
* @param intger $type 类型
* @param bool $all 是否返回全部类型
* @author huajie <banhuajie@163.com>
*/
function get_action_type($type, $all = false)
{
$list = array(
1 => '系统',
2 => '用户',
);
if ($all) {
return $list;
}
return $list[$type];
}
//获取服务器类型
function getServerType($serverType)
{
return (($serverType == 1) ? '专服' : '混服');
}
//获取合作方
function getPartnerList($id = 0)
{
if ($id > 0) {
return M('Partner', 'tab_')->field('id,partner')->find($id);
} else {
return M('Partner', 'tab_')->field('id,partner')->where(array('status' => 1))->select();
}
}
function getPartnerName($id = 0)
{
return M('Partner', 'tab_')->where(array('id' => intval($id)))->getField('partner');
}
function getGameByName($game_name=null, $sdk_version=null)
{
$map = [];
if ($game_name) {
$map['relation_game_name'] = $game_name;
}
if ($sdk_version) {
$map['sdk_version'] = $sdk_version;
}
$result = D("Game")->field('id')->where($map)->select();
if(empty($result)) {
return [['id' => -1]];
}else
{
return $result;
}
}
//根据游戏公司,游戏名称,游戏类型
function getGameidByPartnerNameType($partner_id=null,$game_name=null, $sdk_version=null)
{
$map = [];
if($partner_id){
$map['partner_id'] = $partner_id;
}
if ($game_name) {
$map['relation_game_name'] = $game_name;
}
if ($sdk_version) {
$map['sdk_version'] = $sdk_version;
}
$result = D("Game")->field('id')->where($map)->select();
if(empty($result)) {
return [['id' => -1]];
}else
{
return $result;
}
}
function getTopPromote($promote_id)
{
$promoter = M('promote', 'tab_')->where(['id' => $promote_id])->find();
if (!$promoter) {
return [];
}
$chain = trim($promoter['chain'], '/');
if ($chain == '') {
return $promoter;
} else {
$topPromoteId = explode('/', $chain)[0];
return M('promote', 'tab_')->where(['id' => $topPromoteId])->find();
}
}
function arrayPromoteWithdrawStatus($status, $param, $array = array()) {
foreach ($array as $key => $value) {
if($value[$status] == -1){
unset($array[$key]);
}
}
return $array;
}
function getAllGame()
{
$list = M("game", 'tab_')->field('relation_game_name as game_name')->group('relation_game_name')->select();
return $list;
}
/**
* 获取合作公司
* @author chenzhi
*/
function getPromoteCompany()
{
$list = M("PromoteCompany", 'tab_')
->field('id,company_name')
->where("status = 1")
->select();
array_unshift($list,array("id"=>0,"company_name"=>C(DEFAULT_COMPANY)));//默认0
return $list;
}
function getAllIosGame()
{
$list = M("game", 'tab_')->field('relation_game_name as game_name,id')->where(['sdk_version' => 2])->group('relation_game_name')->select();
return $list;
}
/**
* 中间加密 替换字符串的子串
*/
function encryptStr($str) {
$length = strlen($str);
$stars_str = "";
for ($x=0; $x<=$length-6; $x++) {
$stars_str = $stars_str."*";
}
return substr_replace($str, $stars_str, 3, $length-6);
}
/**
* 身份证加密
*
* @param $str
* @return mixed
*/
function encryptIdCard($str) {
$length = strlen($str);
$stars_str = "";
if($length>=4){
$stars_str = "****";
}
return substr_replace($str, $stars_str, $length-4, 4);
}
/**
* 真实名字加密
*
* @param $str
* @return mixed
*/
function encryptRealName($str) {
$length = strlen($str);
$stars_str = "";
if($length>=4){
$stars_str = "****";
}
return substr_replace($str, "**", 3, $length);
}
//获取推广员资质审核状态 $type 1-获取数组 2-获取文本
function getPromoteVerStatus($status, $type = 1)
{
$statusList = [
0 => '未认证',
1 => '审核成功',
2 => '审核失败',
3 => '审核中',
4 => '修改审核中',
];
$records = null;
switch ($type) {
case 1:
$records = $status;
break;
case 2:
$records = $statusList[$status] ?? '未知';
break;
default:
$records = false;
break;
}
return $records;
}
//获取推广员账号
function getPromoteAccount($promoteId)
{
$map['id'] = intval($promoteId);
return M('promote', 'tab_')->where($map)->getField('account');
}
//获取推广员列表 $level 0-全部 $companyId 推广公司 0-全部
function getPromoteByLevel($level = 0, $companyId = 0)
{
$field = 'id, account, real_name';
$map['_string'] = '1 = 1';
if ($level > 0) {
$map['level'] = $level;
}
if ($companyId > 0) {
$map['company_id'] = $companyId;
}
$promotes = M('promote', 'tab_')->field($field)->where($map)->select();
return $promotes;
}
//获取游戏列表
function getAllGameList($groupByRelation = false)
{
$field = 'id, game_name, relation_game_id, relation_game_name';
if ($groupByRelation) {
$games = M('game', 'tab_')->field($field)->where('id = relation_game_id')->group('relation_game_id')->select();
} else {
$games = M('game', 'tab_')->field($field)->select();
}
return $games;
}
//获取管理员权限列表
function getAdminRules($adminId)
{
$rules = [];
if ($adminId) {
$groupId = M('auth_group_access')->where(array('uid' => intval($adminId)))->getField('group_id');
if ($groupId) {
$rules = M('auth_group')->where(array('id' => $groupId))->getField('rules');
$rules = explode(',', $rules);
}
}
return $rules;
}
//获取权限id
function getRule($name, $module)
{
$ruleId = 0;
if ($name) {
if ($module) {
$map['module'] = $module;
}
$map['name'] = trim($name);
$ruleId = M('auth_rule')->where($map)->getField('id');
}
return $ruleId;
}
//下划线转驼峰 $littleHump 是否转换成小驼峰
function camelize($str, $separator = '_', $littleHump = false)
{
if ($littleHump) {
$str = $separator . str_replace($separator, " ", strtolower($str));
} else {
$str = str_replace($separator, " ", strtolower($str));
}
return ltrim(str_replace(" ", "", ucwords($str)), $separator );
}
//驼峰转下划线
function unCamelize($str, $separator = '_')
{
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $str));
}
function convertAmountToCn($num) {
//判断$num是否存在
if(!$num) return '零圆';
//保留小数点后两位
$num = round($num, 2);
//将浮点转换为整数
$tem_num = $num * 100;
//判断数字长度
$tem_num_len = strlen($tem_num);
if($tem_num_len > 14) {
return '数值过大';
}
//大写数字
$dint = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
//大写金额单位
$danwei = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '圆');
$danwei1 = array('角', '分');
//空的变量用来保存转换字符串
$daxie = '';
//分割数字,区分圆角分
list($left_num, $right_num) = explode('.', $num);
//计算单位长度
$danwei_len = count($danwei);
//计算分割后的字符串长度
$left_num_len = strlen($left_num);
$right_num_len = strlen($right_num);
//循环计算亿万元等
for($i = 0; $i < $left_num_len; $i++) {
//循环单个文字
$key_ = substr($left_num, $i, 1);
//判断数字不等于0或数字等于0与金额单位为亿、万、圆就返回完整单位的字符串
if($key_ !== '0' || ($key_ == '0' && ($danwei[$danwei_len - $left_num_len + $i] == '亿' || $danwei[$danwei_len - $left_num_len + $i] == '万' || $danwei[$danwei_len - $left_num_len + $i] == '圆'))) {
$daxie = $daxie . $dint[$key_] . $danwei[$danwei_len - $left_num_len + $i];
} else {
//否则就不含单位
$daxie = $daxie . $dint[$key_];
}
}
//循环计算角分
for($i = 0; $i < $right_num_len; $i++) {
$key_ = substr($right_num, $i, 1);
if($key_ > 0) {
$daxie = $daxie . $dint[$key_] . $danwei1[$i];
}
}
//计算转换后的长度
$daxie_len = strlen($daxie);
//设置文字切片从0开始utf-8汉字占3个字符
$j = 0;
while($daxie_len > 0) {
//每次切片两个汉字
$str = substr($daxie, $j, 6);
//判断切片后的文字不等于零万、零圆、零亿、零零
if($str == '零万' || $str == '零圆' || $str == '零亿' || $str == '零零') {
//重新切片
$left = substr($daxie, 0, $j);
$right = substr($daxie, $j + 3);
$daxie = $left . $right;
}
$j += 3;
$daxie_len -= 3;
}
return $daxie . '整';
}
/**
* @param $phone
* @return false|int
*/
function checkPhone($phone) {
return preg_match("/^1[3456789]\d{9}$/",$phone);
}
//获取sdk类型名称
function getSDKTypeName($sdkType, $chinese = false)
{
$android = 'Android';
if ($chinese) {
$android = '安卓';
}
switch ($sdkType) {
case 0:
$sdkName = $android . '+ios';
break;
case 1:
$sdkName = $android;
break;
case 2:
$sdkName = 'ios';
break;
}
return $sdkName;
}
//游戏名称取消 (安卓版),(苹果版)
function clearGameNameType($game_name)
{
return preg_replace("/\(.*\)/","",$game_name);
}
//设备名称词典
function getGameTypeName($id,$chinese=true)
{
if(empty($id)){
return '无';
}
$android = 'Android';
$ios = 'ios';
if($chinese){
$android = '安卓';
$ios = '苹果';
}
$data = array(
["id"=>0,'name'=>"{$android}+{$ios}"],
["id"=>1,'name'=>$android],
["id"=>2,'name'=> $ios]
);
if($id > -1){
foreach($data as $k=>$v){
if($v['id'] == $id){
return $v['name'];
break;
}
}
}else{
return $data;
}
}