239 lines
7.6 KiB
PHP
239 lines
7.6 KiB
PHP
<?php
|
||
/**
|
||
* Here is your custom functions.
|
||
*/
|
||
if (!function_exists('p')) {
|
||
|
||
/**
|
||
* 将数组 key 的命名方式转换为小写驼峰
|
||
* @param array $array 被转换的数组
|
||
* @param array $keys 要转换的 key,默认所有
|
||
* @return array
|
||
*/
|
||
function p(...$p)
|
||
{
|
||
if(count($p) > 1) {
|
||
foreach ($p as $k => $v) {
|
||
print_r($v);
|
||
print_r('---');
|
||
}
|
||
}else {
|
||
print_r($p[0]);
|
||
}
|
||
die;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 多维数组去重并重新索引
|
||
* @param array $array 待处理的多维数组
|
||
* @return array 去重后并重新索引的数组
|
||
*/
|
||
function uniqueMultidimensionalArray($array, $k = 'user_id') {
|
||
$unique = [];
|
||
$seenIds = []; // 用于记录已出现的user_id
|
||
|
||
foreach ($array as $item) {
|
||
// 确保子数组包含user_id字段
|
||
if (!isset($item[$k])) {
|
||
continue; // 跳过不包含user_id的子数组(可选:也可抛出异常)
|
||
}
|
||
|
||
$userId = $item[$k];
|
||
// 如果user_id未出现过,则保留该记录
|
||
if (!in_array($userId, $seenIds)) {
|
||
$seenIds[] = $userId;
|
||
$unique[] = $item;
|
||
}
|
||
// 已出现的user_id会被自动跳过(去重)
|
||
}
|
||
|
||
// 重新索引数组(从0开始的连续数字键)
|
||
return array_values($unique);
|
||
}
|
||
if(!function_exists('http_post')) {
|
||
function http_post($url, $data, $headers = [], $timeout = 30, $contentType = 'application/json')
|
||
{
|
||
// 初始化cURL
|
||
$ch = curl_init();
|
||
|
||
// 处理请求数据
|
||
if ($contentType === 'application/json') {
|
||
$data = json_encode($data);
|
||
} elseif (is_array($data)) {
|
||
$data = http_build_query($data);
|
||
}
|
||
|
||
// 设置请求头
|
||
$defaultHeaders = [
|
||
"Content-Type: $contentType",
|
||
"Content-Length: " . strlen($data)
|
||
];
|
||
$headers = array_merge($defaultHeaders, $headers);
|
||
|
||
// 设置cURL选项
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_POST, true);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||
|
||
// 执行请求
|
||
$response = curl_exec($ch);
|
||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
$error = curl_error($ch);
|
||
|
||
// 关闭cURL
|
||
curl_close($ch);
|
||
|
||
// 处理错误
|
||
if ($response === false) {
|
||
return $error;
|
||
}
|
||
return $response;
|
||
}
|
||
}
|
||
|
||
if (!function_exists('curl_post')) {
|
||
function curl_post($url, $params, $op)
|
||
{
|
||
$postData = json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||
if(!empty($params['shopId'])) {
|
||
\support\Log::info('curl_post--->' . $postData);
|
||
}
|
||
$opts = array(
|
||
CURLOPT_TIMEOUT => 30,
|
||
CURLOPT_RETURNTRANSFER => 1,
|
||
CURLOPT_SSL_VERIFYPEER => false,
|
||
CURLOPT_SSL_VERIFYHOST => false,
|
||
CURLOPT_ENCODING => 'gzip',
|
||
);
|
||
$opts[CURLOPT_URL] = $url;
|
||
$opts[CURLOPT_POST] = 1;
|
||
$opts[CURLOPT_POSTFIELDS] = $postData;
|
||
$opts[CURLOPT_HTTPHEADER] = $op;
|
||
$ch = curl_init();
|
||
curl_setopt_array($ch, $opts);
|
||
$responsedata = curl_exec($ch);
|
||
$error = curl_error($ch);
|
||
curl_close($ch);
|
||
\support\Log::info('curl_post_结果--->' . $responsedata);
|
||
return $responsedata;
|
||
}
|
||
}
|
||
|
||
// 处理${}字符串
|
||
if (!function_exists('handldollerstr')) {
|
||
function handldollerstr($str)
|
||
{
|
||
$pattern = '/\$\{(.*?)\}/';
|
||
preg_match_all($pattern, $str, $matches);
|
||
$result = $matches[1];
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
|
||
// 处理${}字符串
|
||
if (!function_exists('saveJson_arr')) {
|
||
function saveJson_arr($th_json, $cont_json, $user)
|
||
{
|
||
$th_json_arr = json_decode($th_json, true);
|
||
$cont_json_arr = json_decode($cont_json, true);
|
||
$new_arr = [];
|
||
foreach ($th_json_arr as $k => $v) {
|
||
foreach ($cont_json_arr as $k1 => $v1) {
|
||
if($k == $v1) {
|
||
if($k1 == 'username') {
|
||
$new_arr[$k1] = removeEmojiAndSpecialChars($user['nick_name'], false);
|
||
}else {
|
||
$new_arr[$k1] = $v;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return json_encode($new_arr);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
if (!function_exists('replace_placeholder_keys')) {
|
||
function replace_placeholder_keys($str, $replaceArray)
|
||
{
|
||
preg_match_all('/\$\{.*?\}/', $str, $matches);
|
||
$originalPlaceholders = $matches[0]; // 结果: ['${用户昵称}', '${店铺名称}', ...]
|
||
$arrayKeys = array_keys($replaceArray); // 结果: ['username', 'shopname', 'time', 'item1', 'item2']
|
||
$replacePairs = [];
|
||
foreach ($originalPlaceholders as $index => $placeholder) {
|
||
if (isset($arrayKeys[$index])) {
|
||
// 关键:明确拼接 ${键} 结构
|
||
$replacePairs[$placeholder] = '${' . $arrayKeys[$index] . '}';
|
||
}
|
||
}
|
||
$newStr = strtr($str, $replacePairs);
|
||
return $newStr;
|
||
}
|
||
}
|
||
|
||
|
||
if (!function_exists('replace_json_keys')) {
|
||
function replace_json_keys($originalJson, $mapJson, $user)
|
||
{
|
||
$originalData = json_decode($originalJson, true);
|
||
$mapData = json_decode($mapJson, true);
|
||
$reverseMap = array_flip($mapData);
|
||
$newData = [];
|
||
foreach ($originalData as $oldKey => $value) {
|
||
// 如果原始键在映射中存在,则替换为对应的新键;否则保留原键
|
||
$newKey = $reverseMap[$oldKey] ?? $oldKey;
|
||
$newData[$newKey] = $value;
|
||
if($newKey == 'username') {
|
||
$newData[$newKey] = $user['nick_name'];
|
||
}
|
||
}
|
||
$newJson = json_encode($newData, JSON_UNESCAPED_UNICODE);
|
||
return $newJson;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 移除字符串中的表情和特殊字符
|
||
* @param string $str 原始字符串
|
||
* @param bool $keepBasicPunct 是否保留基本标点(默认保留)
|
||
* @return string 处理后的字符串
|
||
*/
|
||
if (!function_exists('removeEmojiAndSpecialChars')) {
|
||
function removeEmojiAndSpecialChars($str, $keepBasicPunct = true)
|
||
{
|
||
// 1. 移除 Emoji 表情(匹配常见 Emoji 的 Unicode 范围)
|
||
$emojiPattern = '/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F1E0}-\x{1F1FF}]|[\x{2600}-\x{26FF}]/u';
|
||
$str = preg_replace($emojiPattern, '', $str);
|
||
|
||
// 2. 移除特殊字符(控制字符、非预期字符)
|
||
if ($keepBasicPunct) {
|
||
// 保留:字母、数字、中文、空格,以及基本标点(!@#$%^&*()_+-= etc.)
|
||
// 正则含义:匹配所有不在以下范围内的字符并移除
|
||
// \p{L}:所有语言的字母(包括中文、英文、日文等)
|
||
// \p{N}:所有数字
|
||
// \s:空白字符(空格、换行等)
|
||
// !-~:ASCII 可见标点(33-126 范围内的字符)
|
||
$specialPattern = '/[^\p{L}\p{N}\s!-~]/u';
|
||
} else {
|
||
// 不保留标点:只保留字母、数字、中文、空格
|
||
$specialPattern = '/[^\p{L}\p{N}\s]/u';
|
||
}
|
||
$str = preg_replace($specialPattern, '', $str);
|
||
|
||
// 3. 移除连续的空白字符(可选,根据需求)
|
||
$str = preg_replace('/\s+/', ' ', $str);
|
||
|
||
return trim($str);
|
||
}
|
||
}
|