179 lines
5.8 KiB
PHP
179 lines
5.8 KiB
PHP
<?php
|
||
|
||
namespace app\api\model;
|
||
|
||
use app\common\library\DatabaseRoute;
|
||
use support\Redis;
|
||
use support\think\Cache;
|
||
use think\facade\Db;
|
||
use support\Log;
|
||
use think\Model;
|
||
|
||
class UniAdCallbackRecord extends Model
|
||
{
|
||
|
||
public static function getFreeWatchRemainTime($user_id, $isPermanent):int
|
||
{
|
||
// 构造Redis键名(根据是否永久免费区分)
|
||
$redisKey = $isPermanent
|
||
? "user:free_watch:permanent:{$user_id}"
|
||
: "user:free_watch:normal:{$user_id}";
|
||
|
||
$redis = new Redis;
|
||
// 从Redis获取值,默认返回0
|
||
$remainTime = $redis->get($redisKey, 0);
|
||
// 确保返回整数类型
|
||
return (int)$remainTime;
|
||
}
|
||
|
||
|
||
public static function adCallBack(array $callBackDTO)
|
||
{
|
||
$respData = [];
|
||
|
||
$db = Db::connect(config('think-orm.search_library'));
|
||
// 检查是否重复回调
|
||
$record = $db->name('uni_ad_callback_record')
|
||
->where('trans_id', $callBackDTO['trans_id'])
|
||
->find();
|
||
|
||
if ($record) {
|
||
Log::info("回调重复, trans_id: {$record['trans_id']}");
|
||
$respData['isValid'] = false;
|
||
return $respData;
|
||
}
|
||
|
||
// 准备记录数据
|
||
$recordData = [
|
||
'user_id' => (int)$callBackDTO['user_id'],
|
||
'platform' => $callBackDTO['platform'],
|
||
'trans_id' => $callBackDTO['trans_id'],
|
||
'adpid' => $callBackDTO['adpid'],
|
||
'provider' => $callBackDTO['provider'],
|
||
'sign' => $callBackDTO['sign'],
|
||
'extra' => $callBackDTO['extra'],
|
||
'create_time' => date('Y-m-d H:i:s'),
|
||
'is_ended' => 1
|
||
];
|
||
$security = 'cbc34e14ee6d64738557c96623dd6da89f77cac8ae519c6a5c87191d614d386a';
|
||
// 签名验证
|
||
$flag = self::validateSign($security, $callBackDTO['trans_id'], $callBackDTO['sign']);
|
||
if (!$flag) {
|
||
$recordData['err_msg'] = "签名验证失败";
|
||
Log::info(json_encode($recordData));
|
||
Db::name('uni_ad_callback_record')->insert($recordData);
|
||
$respData['isValid'] = false;
|
||
return $respData;
|
||
}
|
||
|
||
// 检查用户是否存在
|
||
$userEntity = Db::name('user_entity')
|
||
->where('id', $callBackDTO['user_id'])
|
||
->find();
|
||
|
||
if (!$userEntity) {
|
||
$recordData['err_msg'] = "用户不存在";
|
||
Log::warning(self::getBaseErrInfo($recordData));
|
||
Db::name('uni_ad_callback_record')->insert($recordData);
|
||
$respData['isValid'] = false;
|
||
return $respData;
|
||
}
|
||
|
||
// 根据extra字段处理不同逻辑
|
||
if (!str_contains($callBackDTO['extra'], "cash")) {
|
||
// 获取配置信息
|
||
$info = Db::name('common_info')
|
||
->where('id', 921)
|
||
->find();
|
||
|
||
if (!$info || empty($info['value'])) {
|
||
$recordData['err_msg'] = "CommonInfo时长时间未配置";
|
||
Log::warning(self::getBaseErrInfo($recordData));
|
||
Db::name('uni_ad_callback_record')->insert($recordData);
|
||
$respData['isValid'] = false;
|
||
return $respData;
|
||
}
|
||
|
||
// 设置免费观看时间(分钟转秒)
|
||
self::setFreeWatchTime($recordData['user_id'], (int)$info['value'] * 60, true);
|
||
} else {
|
||
// 设置可提现标志
|
||
$recordId = Db::name('uni_ad_callback_record')->insertGetId($recordData);
|
||
self::setCanCashFlag($userEntity['id'], $recordId);
|
||
return ['isValid' => true]; // 提前返回,避免重复插入
|
||
}
|
||
|
||
// 保存记录
|
||
Db::name('uni_ad_callback_record')->insert($recordData);
|
||
|
||
// 返回成功响应
|
||
$respData['isValid'] = true;
|
||
return $respData;
|
||
}
|
||
|
||
/**
|
||
* 验证签名
|
||
* @param string $securityKey 安全密钥
|
||
* @param string $transId 交易ID
|
||
* @param string $sign 签名
|
||
* @return bool 验证结果
|
||
*/
|
||
public static function validateSign(string $securityKey, string $transId, string $sign): bool
|
||
{
|
||
// 实际签名验证逻辑(根据业务需求实现)
|
||
$expectedSign = self::generateSign($securityKey, $transId);
|
||
return $expectedSign === $sign;
|
||
}
|
||
|
||
/**
|
||
* 设置免费观看时间
|
||
* @param int $userId 用户ID
|
||
* @param int $duration 时长(秒)
|
||
* @param bool $isPermanent 是否永久
|
||
*/
|
||
public static function setFreeWatchTime(int $userId, int $duration, bool $isPermanent)
|
||
{
|
||
$key = $isPermanent ? "free_watch:permanent:{$userId}" : "free_watch:normal:{$userId}";
|
||
Cache::set($key, $duration, $duration); // 缓存时间等于有效时长
|
||
}
|
||
|
||
/**
|
||
* 设置可提现标志
|
||
* @param int $userId 用户ID
|
||
* @param int $recordId 记录ID
|
||
*/
|
||
public static function setCanCashFlag(int $userId, int $recordId)
|
||
{
|
||
Cache::set("can_cash:{$userId}", $recordId, 86400); // 缓存24小时
|
||
}
|
||
|
||
/**
|
||
* 获取基础错误信息
|
||
* @param array $record 记录数据
|
||
* @return string 错误信息
|
||
*/
|
||
public static function getBaseErrInfo(array $record): string
|
||
{
|
||
return "广告回调错误: [user_id={$record['user_id']}, trans_id={$record['trans_id']}, err_msg={$record['err_msg']}]";
|
||
}
|
||
|
||
/**
|
||
* 生成签名
|
||
* @param string $secret 安全密钥
|
||
* @param string $transId 交易ID
|
||
* @return string 生成的签名(十六进制字符串)
|
||
*/
|
||
public static function generateSign(string $secret, string $transId): string
|
||
{
|
||
// 生成待加密的字符串
|
||
$data = $secret . ':' . $transId;
|
||
|
||
// 使用SHA-256生成签名(返回十六进制字符串)
|
||
return hash('sha256', $data);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
} |