Files
ntonpower/app/common.php
2026-10-14 14:00:29 +08:00

498 lines
14 KiB
PHP
Raw Permalink 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
// 应用公共文件
/*
*记录消息
* */
function addNewsLog($uid, $title, $detail, $type, $order_id)
{
$add_data['uid'] = $uid;
$add_data['title'] = $title;
$add_data['detail'] = $detail;
$add_data['type'] = $type;
$add_data['addtime'] = time();
$add_data['order_id'] = $order_id;
\think\facade\Db::table(config("database.connections.mysql.prefix").'news_log')->insert($add_data,false,true);
}
/**
* 组装绝对url
* @param $url
* @return string
*/
function build_image_fullurl($url, $qiniu=true, $type='public')
{
$rootPath = '';
if (empty($url) || stripos($url, 'http://') !== false || stripos($url, 'https://') !== false) {
return $url;
} else if (stripos($url, '.') == 0) {
$url = mb_substr($url, 1);
}
if (stripos($url, '/') == 0) {
if (false !== stripos($url, '/attachs')) {
$rootPath = 'https://www.f2b211.com';
}
if (false !== stripos($url, '/paycard')) {
$rootPath = 'https://api.f2b211.com';
}
if (false !== stripos($url, '/payimg')) {
$rootPath = 'https://api.f2b211.com';
}
if (false !== stripos($url, '/Public/goods')) {
$rootPath = 'https://www.f2b211.com';
}
} else {
// 七牛地址
if ($qiniu) {
$rootPath = \utils\QiniuUploader::$domain;
} else {
if ($type == 'public') {
$rootPath = request()->domain() . \think\facade\Filesystem::getConfig('disks.public.url');
} else {
$rootPath = request()->domain() . '/';
}
}
}
return $rootPath . $url;
}
/**
* 返回json格式的数组
* @param
* @param
* @param [array] $data [随带的数据]
* @return [string] [json字符串]
*/
function json_return($data)
{
header("Content-Type: application/json; charset=UTF-8");
exit(json_encode($data));
}
/**
* 将数值金额转换为中文大写金额
* @param $amount float 金额(支持到分)
* @param $type int 补整类型,0:到角补整;1:到元补整
* @return mixed 中文大写金额
*/
function convertAmountToCn($amount, $type = 1) {
// 判断输出的金额是否为数字或数字字符串
if(!is_numeric($amount)){
return "要转换的金额只能为数字!";
}
// 金额为0,则直接输出"零元整"
if($amount == 0) {
return "人民币零元整";
}
// 金额不能为负数
if($amount < 0) {
return "要转换的金额不能为负数!";
}
// 金额不能超过万亿,即12位
if(strlen($amount) > 12) {
return "要转换的金额不能为万亿及更高金额!";
}
// 预定义中文转换的数组
$digital = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
// 预定义单位转换的数组
$position = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '元');
// 将金额的数值字符串拆分成数组
$amountArr = explode('.', $amount);
// 将整数位的数值字符串拆分成数组
$integerArr = str_split($amountArr[0], 1);
// 将整数部分替换成大写汉字
$result = '人民币';
$integerArrLength = count($integerArr); // 整数位数组的长度
$positionLength = count($position); // 单位数组的长度
$zeroCount = 0; // 连续为0数量
for($i = 0; $i < $integerArrLength; $i++) {
// 如果数值不为0,则正常转换
if($integerArr[$i] != 0){
// 如果前面数字为0需要增加一个零
if($zeroCount >= 1){
$result .= $digital[0];
}
$result .= $digital[$integerArr[$i]] . $position[$positionLength - $integerArrLength + $i];
$zeroCount = 0;
}else{
$zeroCount += 1;
// 如果数值为0, 且单位是亿,万,元这三个的时候,则直接显示单位
if(($positionLength - $integerArrLength + $i + 1)%4 == 0){
$result = $result . $position[$positionLength - $integerArrLength + $i];
}
}
}
// 如果小数位也要转换
if($type == 0) {
// 将小数位的数值字符串拆分成数组
$decimalArr = str_split($amountArr[1], 1);
// 将角替换成大写汉字. 如果为0,则不替换
if($decimalArr[0] != 0){
$result = $result . $digital[$decimalArr[0]] . '角';
}
// 将分替换成大写汉字. 如果为0,则不替换
if($decimalArr[1] != 0){
$result = $result . $digital[$decimalArr[1]] . '分';
}
}else{
// $result = $result . '整';
$result = $result;
}
return $result;
}
/**
* 获取客户端ip
*/
function get_client_ip($type = 0) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if(isset($_SERVER['HTTP_X_REAL_IP'])){//nginx 代理模式下,获取客户端真实IP
$ip=$_SERVER['HTTP_X_REAL_IP'];
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$long = sprintf("%u",ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
/**
* 列表转换为树形结构
* @param array $list 需要处理的树形结构数据
* @param int $isOver 是否有限级别,比如2表示只输出2级分类结构,0表示有多少级全部输出
* @param string $pk 主键字段名
* @param string $pid_name 父类字段名
* @param string $child_name 子数据结构名
* @param string $level_name 层级结构名
* @param int $pid 父ID
* @param int $level 层级数量,1=一层分类,2=二层
* @return array
*/
function list_to_tree($list,$isOver = 0, $pk='id', $pid_name='pid', $child_name='children', $level_name='level', $pid=0, $level=1)
{
$tree = [];
foreach ($list as $key => $value)
{
if ($value[$pid_name] == $pid)
{
$value[$level_name] = $level;
if ($isOver == $level) {
continue;
} else {
$child = list_to_tree($list,$isOver, $pk, $pid_name, $child_name, $level_name, $value[$pk], $level+1);
}
if (!empty($child)) $value[$child_name] = $child;
$tree[] = $value;
}
}
return $tree;
}
/**
* 列表转换为树形结构
*/
function list_to_top_tree($data,$cid)
{
$arr = [];
foreach ($data as $item) {
if ($cid == $item['id']) {
$arr[] = $item;
$arr = array_merge(list_to_top_tree($data,$item['pid']),$arr);
}
}
return $arr;
}
/**
* 通过分类id查找所有父ID
*/
function category_to_all_pid($data,$cid)
{
$arr = [];
foreach ($data as $item) {
if ($cid == $item['id']) {
$arr[] = $item['id'];
$arr = array_merge(category_to_all_pid($data,$item['pid']),$arr);
}
}
return $arr;
}
/**
* 当前分类ID,获取所有子分类ID
*/
function category_to_all_children_id($data,$cid)
{
$arr = [];
foreach ($data as $item) {
if ($cid == $item['pid']) {
$arr[] = $item['id'];
$arr = array_merge(category_to_all_children_id($data,$item['id']),$arr);
}
$arr[] = $cid;
}
return $arr;
}
/**
* 获取唯一id
*/
function unique_id($length = 20, $prefix = '')
{
$id = $prefix;
$addLength = $length - 13;
$id .= uniqid();
if (function_exists('random_bytes')) {
$id .= substr(bin2hex(random_bytes(ceil(($addLength) / 2))),0,$addLength);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$id .= substr(bin2hex(openssl_random_pseudo_bytes(ceil($addLength / 2))),0,$addLength);
} else {
$id .= mt_rand(1*pow(10,($addLength)),9*pow(10,($addLength)));
}
return $id;
}
///**
// * 获取随机字符串
// */
//function rand_str($length)
//{
// $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
// $result = '';
// for($i=0;$i<$length;$i++)
// {
// $result .= $pattern{mt_rand(0,35)}; //生成php随机数
// }
// return $result;
//}
/**
* 格式化价格
* @param $price int 价格
* @param $divisor int 除数
* @param $bit int 保留位数
* @return string
*/
function format_price($price, $divisor=100, $bit=2)
{
$format = "%." . $bit . "f";
return sprintf($format, $price / $divisor);
}
/**
* 数据是否合法
*/
function is_valid_data($data)
{
return isset($data) && $data != '';
}
/**
* 隐藏电话号码
*/
function hide_mobile($mobile)
{
$IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$mobile); //固定电话
if($IsWhat == 1){
return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$mobile);
}else{
return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$mobile);
}
}
function curl_get($url){
$headerArray = array(
"Content-type: application/json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output, true);
return $output;
}
function curl_post($url, $data){
$data = json_encode($data);
$headerArray = array(
"Content-type: application/json"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output, true);
}
/** 根据物料编码查69码
* @param $fnumber 编码
*/
function get_FBARCODE($fnumber){
$formid = 'BD_MATERIAL';
$TopRowCount = 0;
$Limit = 0;
$StartRow = 0;
$FilterString = "FUseOrgId.fnumber=101 and FForbidStatus='A' and FNumber='$fnumber'";//dump($FilterString);die;
$OrderString = 'FMATERIALID';
$FieldKeys = 'FBARCODE';
$erp = new ERP();
$geterp = $erp->getproduct($formid, $FieldKeys, $FilterString, $OrderString, $StartRow, $TopRowCount, $Limit);
$fnumber = json_decode($geterp[0],true);
$CheckBox = $fnumber[0][0];
return $CheckBox;
}
/** 根据69码查物料型号
* @param $code 69码
*/
function FBARCODE_Getmodel($code){
$formid = 'BD_MATERIAL';
$TopRowCount = 0;
$Limit = 0;
$StartRow = 0;
$FilterString = "FUseOrgId.fnumber=101 and FForbidStatus='A' and FBARCODE='$code'";//dump($FilterString);die;
$OrderString = 'FMATERIALID';
$FieldKeys = 'FSpecification';
$erp = new ERP();
$geterp = $erp->getproduct($formid, $FieldKeys, $FilterString, $OrderString, $StartRow, $TopRowCount, $Limit);
$fnumber = json_decode($geterp[0],true);
if($fnumber==null){
$CheckBox=null;
}else{
$CheckBox = $fnumber[0][0];
}
return $CheckBox;
}
/**
* 处理图片绝对路径转相对路径算法
* @param $url
* @return mixed|string
*/
function doImageURLtoRelativePath($url)
{
$arr = explode(think\facade\Env::get('QINIU.cdn'),$url);
if (isset($arr[1])) {
return $arr[1];
} else {
//可以拆分域名朋问题
if (false === strrpos(think\facade\Env::get('QINIU.cdn'),$url)) {
return $url;
} else {
$domain = explode('/',$url)[2];
$arr = explode($domain[2],$url);
return ltrim('/',$arr[1]);
}
}
}
if (!function_exists('is_image')) {
function is_image($file) {
return file_exists($file) && @getimagesize($file) ? true : false;
}
}
/**
* 处理二维数组站点与语言ID互换的问题
*/
if (!function_exists('array_copy_column')) {
function array_copy_column($array) {
foreach ($array as $key => $item) {
if (isset($item['language_id'])) {
$array[$key]['site_id'] = $item['language_id'];
}
}
return $array;
}
}
if (function_exists('show')) {
/**
* 返回json格式的数组
* @param [int] $status [代表返回时的状态:0失败,1成功]
* @param [string] $message [提示信息]
* @param [array] $data [随带的数据]
* @return [string] [json字符串]
*/
function show($status, $message, $data = [])
{
header("Content-Type: application/json; charset=UTF-8");
$result = [
'status' => $status,
'message' => $message,
'data' => $data
];
exit(json_encode($result));
}
}
/**
* 递归
* @param $data array 需要递归的数据
* @param $pid integer 上级ID
* @param $pf string 上级的字段名
* @param $unset bool 为true则去除pid字段
* @return array
*/
if (!function_exists('recursion_func')) {
function recursion_func($data, $pid = 0, $pf = 'pid', $unset = false) {
$result = [];
foreach ($data as $key => $val) {
if ($val[$pf] == $pid) {
if ($unset) unset($val[$pf]);
$result[$key] = $val;
if ($recur = recursion_func($data, $val['id'], $pf, $unset)) {
$result[$key]['children'] = array_values($recur);
}
}
}
return $result;
}
}