p_ysk/app/functions.php

178 lines
5.4 KiB
PHP
Raw 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
/**
* 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('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;
}
}