baji/app/common.php
2025-03-09 22:43:15 +08:00

618 lines
18 KiB
PHP
Executable File
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
// 应用公共文件
use think\facade\Request;
use think\facade\Db;
if (!function_exists('getRandStr')) {
/**
* @param int $len 长度
* @param bool $special 是否加入特殊字符
* @return string 字符串
*/
function getRandStr($len = 30, $special = false)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
if ($special) {
$chars = array_merge($chars, array(
"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
"}", "<", ">", "~", "+", "=", ",", "."
));
}
$charsLen = count($chars) - 1;
shuffle($chars); //打乱数组顺序
$str = '';
for ($i = 0; $i < $len; $i++) {
$str .= $chars[mt_rand(0, $charsLen)]; //随机取出一位
}
return $str;
}
}
if (!function_exists('setConfig')) {
/**
* 设置系统配置
* @param string $type 配置关键词
* @param array $data 需要保存的数组
* @return void
*/
function setConfig($type, $data)
{
$data = empty($data) ? null : $data;
$content = DB::name('config')->where(['key' => $type])->find();
$json_str = json_encode($data, JSON_UNESCAPED_UNICODE);
if (!empty($content)) {
if ($data) {
$res = DB::name('config')->where(['key' => $type])->update(['value' => $json_str]);
} else {
$res = DB::name('config')->where(['key' => $type])->delete();
}
} else {
$res = DB::name('config')->insert(array('value' => $json_str, 'key' => $type));
}
return $res;
}
}
if (!function_exists('getConfig')) {
/**
* 获取系统配置
* @param string $type 配置关键词
* @return void
*/
function getConfig($type)
{
$content = Db::name('config')->where(['key' => $type])->value('value');
$config = json_decode($content, true);
return $config;
}
}
if (!function_exists('imageUrl')) {
/**
* 图片补全路径
* @param string $type 配置关键词
* @return void
*/
function imageUrl($url = '')
{
if ($url == '') {
return '';
}
$pre = request()->domain();
if (strpos($url, 'http') !== false) {
$path = $url;
}
// else if (strpos($pre, '192.168') !== false) {#测试环境
// $path = $pre . "ceshi/public/" . $url;
// }
else if ($url === '0') {
$path = $pre;
} else {
$path = $pre . $url;
}
$path = str_replace('\\', '/', $path);
return $path;
}
}
if (!function_exists('contentUrl')) {
/**
* 补充富文本内容图片路径
* @param content 富文本内容
* @return 返回处理后的富文本内容
*/
function contentUrl($content)
{
$url = request()->domain() . '/';
if (strpos($url, '192.168') !== false) {#测试环境
$url = $url . "ceshi/public/";
}
$pregRule = "/<[img|IMG].*?src=[\|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\|\"].*?[\/]?>/";
$content = preg_replace($pregRule, '<img src="' . $url . '${1}" style="max-width:100%">', htmlspecialchars_decode($content));
return $content;
}
}
if (!function_exists('shuffle_assoc')) {
/**
* 二维数组打乱顺序
* @param string $list 列表数组
* @return void
*/
function shuffle_assoc($list)
{
if (!is_array($list)) {
return $list;
}
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $k => $key) {
$random[$k] = $list[$key];
}
return $random;
}
}
if (!function_exists('arraySequence')) {
/**
* 二位数组指定字段排序
* @param $array
* @param $field
* @param string $sort
* @return array
*/
function arraySequence($array, $field, $sort = 'SORT_DESC')
{
$else_data = [];
$arrSort = array();
$new_arrSort = array();
$len = 0;
foreach ($array as $uniqid => $row) {
$new_arrSort[] = $row;
foreach ($row as $key => $value) {
$arrSort[$key][$len] = $value;
}
$len++;
}
array_multisort($arrSort[$field], constant($sort), $new_arrSort);
$new_array = array_merge($else_data, $new_arrSort);
return $new_array;
}
}
if (!function_exists('create_order_no')) {
/**
* 生成订单号
* @return string
*/
function create_order_no($pre = 'NO_', $tale = 'order', $field = 'order_num')
{
$order_no = $pre . date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8) . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
$r = Db::name($tale)->field('id')->where([$field => $order_no])->find();
if ($r) {
$order_no = create_order_no($pre);
}
return $order_no;
}
}
if (!function_exists('getPrizeCode')) {
/**
* 获取盒子奖品编号
* @param int $len 推荐码长度
*/
function getPrizeCode($len = 10, $special = false)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); //打乱数组顺序
$str = '';
for ($i = 0; $i < $len; $i++) {
$str .= $chars[mt_rand(0, $charsLen)]; //随机取出一位
}
$i = Db::name('goods_list')->where(['prize_code' => $str])->find();
if ($i) {
$str = getPrizeCode();
}
return $str;
}
}
if (!function_exists('curlGet')) {
/**
* curl请求指定url (get)
* @param $url
* @param array $data
* @return mixed
*/
function curlGet($url)
{
$headerArray = array("Content-type:application/json;", "Accept: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;
}
}
if (!function_exists('curlPost')) {
/**
* curl请求指定url (post)
* @param $url
* @param array $data
* @return mixed
*/
function curlPost($url, $data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
if (!function_exists('admin_md5')) {
/**
* @param $msg 需要加密的参数
* @return bool
*/
function admin_md5($msg)
{
return sha1(md5(md5('admin_md5_password' . $msg)));
}
}
if (!function_exists('user_md5')) {
/**
* @param $msg 需要加密的参数
* @return bool
*/
function user_md5($msg)
{
return sha1('md5' . md5(md5('user_md5' . $msg)));
}
}
if (!function_exists('RegMoney')) {
/**
* @param $money 需要验证的金额
* @return bool
*/
function RegMoney($money = -1)
{
$preg_money = '/^(0|[1-9]\d{0,10})(\.\d{1,2})?$/';
if (!preg_match($preg_money, $money)) {
return true;
}
return false;
}
}
if (!function_exists('RegInt')) {
/**
* @param $num 需要验证的整数
* @return bool
*/
function RegInt($num = -1)
{
$preg_int = '/^[1-9]\d{0,10}$/';
if (!preg_match($preg_int, $num)) {
return true;
}
return false;
}
}
if (!function_exists('RegZero')) {
/**
* @param $num 需要验证的整数
* @return bool
*/
function RegZero($num = -1)
{
$preg_int = '/^[0-9]\d{0,10}$/';
if (!preg_match($preg_int, $num)) {
return true;
}
return false;
}
}
if (!function_exists('expressWuliu')) {
/**
* 查物流
* @param $no 订单号
* @param $type 快递公司编号
* @return array
*/
function expressWuliu($no = '', $type = '')
{
$host = "https://wuliu.market.alicloudapi.com";//api访问链接
$path = "/kdi";//API访问后缀
$method = "GET";
$appcode = getConfig('base')['logistics_code'];//开通服务后 买家中心-查看AppCode
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "no=" . $no . "&type=" . $type; //参数写在这里
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$" . $host, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$out_put = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
list($header, $body) = explode("\r\n\r\n", $out_put, 2);
if ($httpCode == 200) {
// print("正常请求计费(其他均不计费)<br>");
// print($body);
$body = json_decode($body, true);
if ($body['status'] == 0 && $body['msg'] == 'ok') {
return ['code' => 1, 'msg' => $body['result']];
} else {
return ['code' => 0, 'msg' => $body['msg']];
}
} else {
if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
// print("参数错误");
return ['code' => 0, 'msg' => '参数错误'];
} elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
// print("AppCode错误");
return ['code' => 0, 'msg' => 'AppCode错误'];
} elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
// print("请求的 Method、Path 或者环境错误");
return ['code' => 0, 'msg' => '请求的 Method、Path 或者环境错误'];
} elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
// print("服务未被授权或URL和Path不正确");
return ['code' => 0, 'msg' => '服务未被授权或URL和Path不正确'];
} elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
// print("套餐包次数用完");
return ['code' => 0, 'msg' => '套餐包次数用完'];
} elseif ($httpCode == 500) {
// print("API网关错误");
return ['code' => 0, 'msg' => 'API网关错误'];
} elseif ($httpCode == 0) {
// print("URL错误");
return ['code' => 0, 'msg' => 'URL错误'];
} else {
// print("参数名错误 或 其他错误");
// print($httpCode);
return ['code' => 0, 'msg' => '参数名错误 或 其他错误'];
$headers = explode("\r\n", $header);
$headList = array();
foreach ($headers as $head) {
$value = explode(':', $head);
$headList[$value[0]] = $value[1];
}
print($headList['x-ca-error-message']);
}
}
}
}
if (!function_exists('resCheck')) {
/**
* @param $rs 数据库结果集的判断
* @return bool
*/
function resCheck($data)
{
foreach ($data as $value) {
if (is_array($value)) {
foreach ($value as $val) {
if (is_array($val)) {
foreach ($val as $va) {
if (is_array($va)) {
foreach ($va as $v) {
if (!$v) {
return false;
}
}
} else {
if (!$va) {
return false;
}
}
}
} else {
if (!$val) {
return false;
}
}
}
} else {
if (!$value) {
return false;
}
}
}
return true;
}
}
function p($msg = '', $type = 0)
{
echo '<pre>';
if ($type == 0) {
print_r($msg);
} else {
var_dump($msg);
}
die;
}
if (!function_exists('thumbImage')) {
/**
* 缩放图片
* @param $url 图片路径
* @return int
*/
function thumbImage($path = '', $width = 0, $height = 0, $radius = 0, $bg = 0)
{
require_once('../vendor/topthink/think-image/src/Image.php');
$image = \think\Image::open($path);
// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
$image->thumb($width, $height)->radius($radius, $bg)->save($path);
return true;
}
}
if (!function_exists('time_jian')) {
/**
* 缩放图片
* @param $url 图片路径
* @return int
*/
function time_jian($star_time,$end_time)
{
// 计算两个时间戳之间的差值(单位:秒)
$timeDiff = $end_time - $star_time;
// 根据差值计算经过了多少秒、分钟、小时和天
$seconds = $timeDiff % 60;
$minutes = floor(($timeDiff / 60) % 60);
$hours = floor(($timeDiff / (60 * 60)) % 24);
$days = floor($timeDiff / (60 * 60 * 24));
$str = '';
if ($days){
$str .= "{$days}";
}
if ($hours){
$str .= "{$hours}小时";
}
if ($minutes){
$str .= "{$minutes}分钟";
}
if ($seconds){
$str .= "{$seconds}";
}
return $str;
}
}
// 订单支付日志
function writelog($filename,$content){
$date = date('Y-m-d');
$file = "./pay_log/".$date;
if(!is_dir($file)){
mkdir($file);
}
$file = $file."/".$filename.".txt";
$content = "【收到信息".date("Y-m-d H:i:s",time())."".$content."\r\n\r\n";
$open=fopen($file,"a" );
fwrite($open,$content);
fclose($open);
}
//广告
function tencent_ad_attribution($click_id,$money,$account_id,$access_token,$user_action_set_id){
// $access_token = '80a2138abe0227598ea9ae8a2a55ba69';
// $user_action_set_id = 1200995945;
$interface = 'user_actions/add';
$url = 'https://api.e.qq.com/v1.1/' . $interface;
$common_parameters = array (
'access_token' => $access_token,
'timestamp' => time(),
'nonce' => md5(uniqid('', true))
);
$parameters = array (
'account_id' => $account_id,
'user_action_set_id' => $user_action_set_id,
'actions' =>
array (
0 =>
array (
'url' => 'ruiou.ruiouyifanshang.com',
'external_action_id' => strtolower(md5(rand(100000,999999))) . '_complete_order_1492991000_30569088_1200995945',
'action_time' => time(),
'action_type' => 'COMPLETE_ORDER',
// 'custom_action' => 'my_type',
'trace' => [
'click_id' => $click_id,
],
'action_param' =>
[
'value' => $money
]
),
),
);
// dd($parameters);
// dump($common_parameters);
$parameters = json_encode($parameters);
$request_url = $url . '?' . http_build_query($common_parameters);
// var_dump($parameters);
// dd($request_url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type:application/json"
));
$response = curl_exec($curl);
if (curl_error($curl)) {
$error_msg = curl_error($curl);
$error_no = curl_errno($curl);
curl_close($curl);
throw new \Exception($error_msg, $error_no);
}
curl_close($curl);
\think\facade\Log::write($response,'warning');
return $response;
}
function getData($url,$header){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}