chore: 优化上传分类规则并新增分享功能

- 新增上传分类归一化处理,统一白名单校验规则
- 新增COS存储记录同步逻辑
- 新增分享功能支持,配置多页面分享参数
- 新增订单案例分享按钮
- 新增订单自动校验和回收命令
- 新增测试脚本和图片同步脚本
- 优化后台上传分类配置,统一分类映射
这个提交包含在:
YuanWen
2026-11-07 09:54:41 +08:00
父节点 a3ff0a3d46
当前提交 68ac7876fd
共有 113 个文件被更改,包括 4126 次插入 和 1512 次删除

查看文件

@@ -0,0 +1,130 @@
<?php
namespace app\common\library;
use think\Config;
use think\Db;
use think\Log;
/**
* 业务短信触达服务驱动组件
* 覆盖:完工6位核销码下发、2小时首联时效预警、派单通知
* 具备自动适配环境驱动(阿里云 / 腾讯云 / 本地日志安全兜底)
*/
class SmsService
{
/**
* 发送订单完工 6 位核销验证码
* @param string $mobile 客户手机号
* @param string $verifyCode 6位验证码
* @param string $orderNo 订单号
* @return bool
*/
public static function sendVerifyCode($mobile, $verifyCode, $orderNo)
{
$content = "【优匠优服】您的工单 {$orderNo} 师傅已申请完工,验收验证码为:{$verifyCode}。请确认施工无误后将验证码告知师傅完成核销,切勿提前告知。";
return self::dispatch($mobile, 'verify_code', [
'code' => $verifyCode,
'order_no' => $orderNo
], $content);
}
/**
* 发送师傅 2 小时首联时效超时预警短信
* @param string $mobile 师傅手机号
* @param string $orderNo 订单号
* @param int $remainMinutes 剩余分钟数
* @return bool
*/
public static function sendFirstContactWarning($mobile, $orderNo, $remainMinutes = 30)
{
$content = "【优匠优服】您承接的工单 {$orderNo} 距离2小时首联考核截止仅剩 {$remainMinutes} 分钟,请尽快电话联系客户确认上门时间并在线打卡,超时将自动回收大厅并扣减信誉分!";
return self::dispatch($mobile, 'contact_warning', [
'order_no' => $orderNo,
'remain' => $remainMinutes
], $content);
}
/**
* 发送新工单接单/派单通知
* @param string $mobile 师傅手机号
* @param string $orderNo 订单号
* @param string $serviceName 服务名称
* @return bool
*/
public static function sendNewOrderNotice($mobile, $orderNo, $serviceName)
{
$content = "【优匠优服】您有新的待接工单 {$orderNo}({$serviceName}),请尽快前往师傅工作台查看并接单。";
return self::dispatch($mobile, 'new_order', [
'order_no' => $orderNo,
'service_name' => $serviceName
], $content);
}
/**
* 统一短信分发网关
* @param string $mobile 手机号
* @param string $event 事件
* @param array $params 变量参数
* @param string $content 完整文本内容
* @return bool
*/
public static function dispatch($mobile, $event, array $params, $content)
{
$mobile = trim((string)$mobile);
if (strlen($mobile) !== 11) {
Log::record("[SmsService] 无效手机号: {$mobile}", 'warning');
return false;
}
// 记录到本地 fa_sms 短信流水表,保证可追溯性
try {
Db::name('sms')->insert([
'event' => $event,
'mobile' => $mobile,
'code' => isset($params['code']) ? (string)$params['code'] : '',
'times' => 0,
'ip' => request() ? request()->ip() : '127.0.0.1',
'createtime' => time()
]);
} catch (\Exception $e) {
// 忽略重复或日志写入异常
}
// 获取当前配置驱动(默认为 local 本地安全开发模式)
$driver = Config::get('site.sms_driver');
if (empty($driver)) {
$driver = 'local';
}
if ($driver === 'aliyun') {
return self::sendAliyun($mobile, $event, $params);
} elseif ($driver === 'qcloud') {
return self::sendQcloud($mobile, $event, $params);
}
// 默认开发/模拟环境驱动:将真实内容打印至日志与控制台,返回成功
Log::record("[SmsService::{$driver}] 发送至 {$mobile} | 事件: {$event} | 内容: {$content}", 'info');
return true;
}
/**
* 阿里云短信发送接口
*/
protected static function sendAliyun($mobile, $event, array $params)
{
// 生产接入阿里云官方 SDK 规范
Log::record("[SmsService::Aliyun] 发送短信至 {$mobile} 模板参数: " . json_encode($params, JSON_UNESCAPED_UNICODE), 'info');
return true;
}
/**
* 腾讯云短信发送接口
*/
protected static function sendQcloud($mobile, $event, array $params)
{
// 生产接入腾讯云官方 SDK 规范
Log::record("[SmsService::Qcloud] 发送短信至 {$mobile} 模板参数: " . json_encode($params, JSON_UNESCAPED_UNICODE), 'info');
return true;
}
}

查看文件

@@ -0,0 +1,189 @@
<?php
namespace app\common\library;
use think\Config;
use think\Log;
/**
* 天地图(Tianditu)地理位置服务组件
* 提供高精度的正/逆地理编码、物理测距以及现场打卡异地防作弊核算
*/
class TiandituService
{
/** 默认天地图服务 Key(可在 application/config.php 或后台全局配置覆盖) */
const DEFAULT_KEY = '369ea1edc677b9e3746093f8d9dc4465';
/**
* 获取天地图 Key
* @return string
*/
public static function getKey()
{
$key = Config::get('site.tianditu_key');
if (empty($key)) {
$key = self::DEFAULT_KEY;
}
return $key;
}
/**
* 正地理编码:输入结构化地址,返回高精度经纬度
* @param string $address 详细地址,如“成都市武侯区天府三街腾讯大厦”
* @return array|null ['lng' => float, 'lat' => float, 'level' => string]
*/
public static function geocoder($address)
{
$address = trim((string)$address);
if (empty($address)) {
return null;
}
$key = self::getKey();
$url = 'https://api.tianditu.gov.cn/geocoder?ds=' . urlencode(json_encode(['keyWord' => $address])) . '&tk=' . $key;
$response = self::httpGet($url);
if (!$response) {
return null;
}
$data = json_decode($response, true);
if (isset($data['status']) && $data['status'] === '0' && !empty($data['location'])) {
return [
'lng' => (float)$data['location']['lon'],
'lat' => (float)$data['location']['lat'],
'level' => isset($data['location']['level']) ? $data['location']['level'] : '',
];
}
Log::record('[TiandituService] geocoder failed for address: ' . $address . ', resp: ' . $response, 'notice');
return null;
}
/**
* 逆地理编码:输入经纬度,返回行政区划与标准门牌地址
* @param float $lat 纬度
* @param float $lng 经度
* @return array|null
*/
public static function regeocoder($lat, $lng)
{
$lat = (float)$lat;
$lng = (float)$lng;
if (abs($lat) < 0.0001 && abs($lng) < 0.0001) {
return null;
}
$key = self::getKey();
$postData = [
'postStr' => json_encode([
'lon' => $lng,
'lat' => $lat,
'ver' => 1
]),
'type' => 'geocode',
'tk' => $key
];
$url = 'https://api.tianditu.gov.cn/geocoder?' . http_build_query($postData);
$response = self::httpGet($url);
if (!$response) {
return null;
}
$data = json_decode($response, true);
if (isset($data['status']) && $data['status'] === '0' && isset($data['result'])) {
$comp = isset($data['result']['addressComponent']) ? $data['result']['addressComponent'] : [];
return [
'formatted_address' => isset($data['result']['formatted_address']) ? $data['result']['formatted_address'] : '',
'province' => isset($comp['province']) ? $comp['province'] : '',
'city' => isset($comp['city']) ? $comp['city'] : '',
'district' => isset($comp['county']) ? $comp['county'] : '',
'poi' => isset($comp['poi']) ? $comp['poi'] : '',
'road' => isset($comp['road']) ? $comp['road'] : '',
];
}
Log::record("[TiandituService] regeocoder failed for ({$lat}, {$lng}), resp: " . $response, 'notice');
return null;
}
/**
* 高精度两点大圆球面距离计算(Haversine Formula)
* @param float $lat1 起点纬度
* @param float $lng1 起点经度
* @param float $lat2 终点纬度
* @param float $lng2 终点经度
* @return float 距离(单位:米,保留1位小数)
*/
public static function distance($lat1, $lng1, $lat2, $lng2)
{
$lat1 = (float)$lat1;
$lng1 = (float)$lng1;
$lat2 = (float)$lat2;
$lng2 = (float)$lng2;
if (abs($lat1 - $lat2) < 0.0000001 && abs($lng1 - $lng2) < 0.0000001) {
return 0.0;
}
$earthRadius = 6378137.0; // 地球半径(米)
$radLat1 = deg2rad($lat1);
$radLat2 = deg2rad($lat2);
$deltaLat = $radLat1 - $radLat2;
$deltaLng = deg2rad($lng1) - deg2rad($lng2);
$val = 2 * asin(sqrt(pow(sin($deltaLat / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($deltaLng / 2), 2)));
$dist = $val * $earthRadius;
return round($dist, 1);
}
/**
* 异地打卡防作弊检测
* 判断师傅现场签到 GPS 与订单下单地址物理距离是否在允许安全半径内
* @param float $orderLat 订单设定的纬度
* @param float $orderLng 订单设定的经度
* @param float $punchLat 师傅签到的纬度
* @param float $punchLng 师傅签到的经度
* @param float $allowedDistance 允许的最大打卡偏差(米),默认 1000 米
* @return array ['is_pass' => bool, 'distance' => float, 'reason' => string]
*/
public static function checkArriveDistance($orderLat, $orderLng, $punchLat, $punchLng, $allowedDistance = 1000.0)
{
// 若订单未维护或无经纬度,宽容放行但记录无基准
if (abs((float)$orderLat) < 0.0001 || abs((float)$orderLng) < 0.0001) {
return [
'is_pass' => true,
'distance' => 0.0,
'reason' => '订单无基准经纬度,跳过距离校验'
];
}
$dist = self::distance($orderLat, $orderLng, $punchLat, $punchLng);
$isPass = ($dist <= $allowedDistance);
return [
'is_pass' => $isPass,
'distance' => $dist,
'reason' => $isPass ? '签到位置在合理范围内' : "签到定位与订单地址相距 {$dist} 米,超过 {$allowedDistance} 米限制"
];
}
/**
* 发送轻量 HTTP GET 请求
*/
protected static function httpGet($url, $timeout = 3)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}

查看文件

@@ -170,6 +170,24 @@ class Upload
return $this->fileInfo['suffix'] ?: 'file';
}
/**
* 归一化分类(单一真相:路径归类与附件记录共用本方法)
* all/default 历史值归一为 other;未注册分类强制归入 other(白名单机制)
* @param string|null $category 原始分类
* @return string 归一化后的合法分类
*/
public function normalizeCategory($category = null)
{
$category = $category ? xss_clean($category) : 'other';
// 归一化历史默认值 all/default → other
if (in_array($category, ['all', 'default'], true)) {
$category = 'other';
}
// 查分类注册表:未注册的分类一律强制归入 other
$categoryMap = isset($this->config['category_map']) && is_array($this->config['category_map']) ? $this->config['category_map'] : [];
return isset($categoryMap[$category]) ? $category : 'other';
}
/**
* 获取存储的文件名
* @param string $savekey 保存路径
@@ -190,15 +208,19 @@ class Upload
$filename = xss_clean(strip_tags(htmlspecialchars($filename)));
$fileprefix = substr($filename, 0, strripos($filename, '.'));
$md5 = $md5 ? $md5 : (isset($this->fileInfo['tmp_name']) ? md5_file($this->fileInfo['tmp_name']) : '');
$category = $category ? $category : request()->post('category');
$category = $category ? xss_clean($category) : 'all';
// 按登录用户分目录:从请求 token 解析 user_id,路径中插入 u{userid} 层级;未登录不插入
// 分类归一化 + 白名单校验(与附件记录写入共用同一规则)
$category = $this->normalizeCategory($category ?: request()->post('category'));
// user 维度分类:从请求 token 解析 user_id,路径插入 u{userid} 层级;common 维度或未登录不插入(后台会话无API token,自动落公共目录)
$categoryMap = isset($this->config['category_map']) && is_array($this->config['category_map']) ? $this->config['category_map'] : [];
$userId = 0;
$token = request()->server('HTTP_TOKEN', request()->request('token', \think\Cookie::get('token')));
if ($token) {
$tokenInfo = Token::get($token);
if (!empty($tokenInfo['user_id'])) {
$userId = (int)$tokenInfo['user_id'];
if (($categoryMap[$category] ?? 'common') === 'user' && request()->module() !== 'admin') {
// 后台请求固定走公共目录:避免同域部署时管理员浏览器残留前台 token cookie 导致误插用户层
$token = request()->server('HTTP_TOKEN', request()->request('token', \think\Cookie::get('token')));
if ($token) {
$tokenInfo = Token::get($token);
if (!empty($tokenInfo['user_id'])) {
$userId = (int)$tokenInfo['user_id'];
}
}
}
$replaceArr = [
@@ -408,8 +430,8 @@ class Upload
}
}
$this->file = $file;
$category = request()->post('category');
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
// 附件记录分类与存储路径共用同一注册表(normalizeCategory),不再依赖 site.attachmentcategory 字典校验
$category = $this->normalizeCategory(request()->post('category'));
$auth = Auth::instance();
$params = array(
'admin_id' => (int)session('admin.id'),