进一步完成

This commit is contained in:
2025-10-15 19:30:38 +08:00
parent c957a1ba2b
commit 348a990f88
13 changed files with 547 additions and 53 deletions

View File

@@ -95,4 +95,82 @@ if(!function_exists('http_post')) {
}
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'])) {
Log::write('耗材预警提交json' . $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);
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)
{
$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;
}
$newJson = json_encode($newData, JSON_UNESCAPED_UNICODE);
return $newJson;
}
}