Files
fdesk/app/functions.php
T
2025-08-04 14:03:57 +08:00

197 lines
5.5 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.
*/
use Webman\RedisQueue\Redis as RedisQueueRedis;
/**
* 标准化账号参数
*
* @param string $account 用户输入的账号(可能是手机号或邮箱)
* @return array 返回 ['phone' => $account] 或 ['email' => $account]
*/
if (!function_exists('normalizeAccount')) {
function normalizeAccount(string $account): array
{
// 使用FILTER_VALIDATE_EMAIL验证邮箱格式
if (filter_var($account, FILTER_VALIDATE_EMAIL)) {
return ['email' => $account];
}
// 清理手机号中的非数字字符
$cleanPhone = preg_replace('/[^0-9]/', '', $account);
// 验证手机号格式(简单版:11位数字)
if (preg_match('/^1[3-9]\d{9}$/', $cleanPhone)) {
return ['phone' => $cleanPhone];
}
// 如果既不是邮箱也不是手机号,根据业务需求处理
// 这里默认按手机号处理(可根据实际需求调整)
return ['phone' => $cleanPhone];
}
}
/**
* 公共html页面
*/
if (!function_exists('htmlpage')) {
function htmlpage()
{
static $html;
if (!$html) {
$html = file_get_contents(public_path('mzl.html'));
}
return $html;
}
}
/**
* 获取配置
*/
if (!function_exists('getConfig')) {
function getConfig($filePath = null)
{
// 构建文件路径
$config_file = app_path($filePath);
// 检查文件是否存在
if (!file_exists($config_file)) {
return [];
}
// 包含文件并获取返回的数组
$config = include $config_file;
// 验证是否为数组
if (!is_array($config)) {
return [];
}
return $config;
}
}
/*
* 询单号生成
*/
if (!function_exists('orderNum')) {
function orderNum()
{
return "SN".date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}
}
/*
* 调试用(console)
*/
if (!function_exists('dpr')) {
function dpr(\Throwable $e)
{
$contents = "=== === 报错打印 === ===\n";
$contents .= "文件:".$e->getFile()."\n";
$contents .= "行数:".$e->getLine()."\n";
$contents .= "详情:".$e->getMessage()."\n";
$contents .= "=== === 报错打印 === ===\n";
var_dump($contents);
return $contents;
}
}
/**
* 调试用(file)
*/
if (!function_exists('flg')) {
function flg($eventData = '', $isFs = true, $fileName = 'flg', $fileCatalog = 'flg')
{
if ($eventData == '') {
return false;
}
if (is_array($eventData) || is_object($eventData)) {
$eventData = json_encode($eventData);
}
$fileCatalog = substr(__FILE__, 0, strpos(__FILE__, 'app/')) . "runtime/" . $fileCatalog;
if (!is_dir($fileCatalog)) {
mkdir($fileCatalog, 0777, true);
exec('chown -R nginx:nginx ' . $fileCatalog);
}
$fileName = $fileName . '-' . date('Ymd') . '.log';
if ($isFs) {
$data = [
'content' => $eventData,
'time' => date('Y-m-d H:i:s', time())
];
RedisQueueRedis::send("server", $data);
}
return file_put_contents($fileCatalog . "/" . $fileName, date("Y-m-d H:i:s", time()) . "::" . $eventData . "\n", FILE_APPEND);
}
}
/***
* 发送飞书文本消息
*/
if (!function_exists('sendFeishuTextMessage')) {
function sendFeishuTextMessage($webhookUrl, $message) {
$data = [
'msg_type' => 'text',
'content' => [
'text' => $message
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $webhookUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
/**
* 替换模板
*/
if (!function_exists('replaceTpl')) {
function replaceTpl(array $data = [], string $templatePath = 'common/utils/scaffold/templates/Email.stub')
{
if (!$data || !$templatePath) return '';
$templatePath = app_path($templatePath);
$content = file_get_contents($templatePath);
foreach ($data as $key => $value) {
$content = str_replace('{{'.$key.'}}', $value, $content);
}
return $content;
}
}
/**
* 替换模板
*/
if (!function_exists('InquiryFormat')) {
function InquiryFormat(array $data = [])
{
if (!$data) return '';
$content = '';
// foreach ($data as $key => $value) {
// $content .= $key . ':' . $value . "\r\n";
// }
$content .= '编号:'.$data['order_no'] . "\r\n";
$content .= '询单质量:'.$data['quality'] . "\r\n";
$content .= '渠道:'.$data['channel'] . "\r\n";
$content .= '邮箱:'.$data['email'] . "\r\n";
$content .= '电话:'.$data['tel'] . "\r\n";
$content .= 'WhatsApp:'.$data['whatsapp'] . "\r\n";
$content .= '国家:'.$data['country'] . "\r\n";
$content .= '联系人:'.$data['contacts'] . "\r\n";
$content .= '录单人:'.$data['enter_user_id'] . "\r\n";
$content .= '录单时间:'.$data['r_time'] . "\r\n";
$content .= '内容:'.$data['content'];
return $content;
}
}