feat: 1.uni-ad广告回调接入 2.广告奖励免费观看时长
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.sqx.modules.callback;
|
||||
|
||||
import com.sqx.common.utils.Result;
|
||||
import com.sqx.modules.callback.dao.UniAdCallBackDTO;
|
||||
import com.sqx.modules.callback.service.UniAdCallbackRecordService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/uniCallBack")
|
||||
@Slf4j
|
||||
public class UniCallBackController {
|
||||
private final UniAdCallbackRecordService uniCallBackService;
|
||||
|
||||
public UniCallBackController(UniAdCallbackRecordService uniCallBackService) {
|
||||
this.uniCallBackService = uniCallBackService;
|
||||
}
|
||||
|
||||
@GetMapping("/adCallBack")
|
||||
public ResponseEntity<?> adCallBack(@RequestBody UniAdCallBackDTO callBackDTO) {
|
||||
log.info("接收到uni-ad广告完播回调,回调信息: {}", callBackDTO);
|
||||
return ResponseEntity.ok(uniCallBackService.adCallBack(callBackDTO));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.sqx.modules.callback.dao;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class UniAdCallBackDTO {
|
||||
private String adpid;
|
||||
private String provider;
|
||||
private String platform;
|
||||
private String sign;
|
||||
private String trans_id;
|
||||
private String user_id;
|
||||
private String extra;
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.sqx.modules.callback.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
*
|
||||
* @TableName uni_ad_callback_record
|
||||
*/
|
||||
@TableName(value ="uni_ad_callback_record")
|
||||
@Data
|
||||
public class UniAdCallbackRecord implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 平台
|
||||
*/
|
||||
private String platform;
|
||||
|
||||
/**
|
||||
* 交易id
|
||||
*/
|
||||
private String transId;
|
||||
|
||||
/**
|
||||
* DCloud广告位id
|
||||
*/
|
||||
private String adpid;
|
||||
|
||||
/**
|
||||
* 广告服务商
|
||||
*/
|
||||
private String provider;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private String sign;
|
||||
|
||||
/**
|
||||
* 调用SDK传入并透传,自定义数据
|
||||
*/
|
||||
private String extra;
|
||||
|
||||
/**
|
||||
* 是否播放完毕
|
||||
*/
|
||||
private Integer isEnded;
|
||||
|
||||
/**
|
||||
* 回调时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private String errMsg;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object that) {
|
||||
if (this == that) {
|
||||
return true;
|
||||
}
|
||||
if (that == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != that.getClass()) {
|
||||
return false;
|
||||
}
|
||||
UniAdCallbackRecord other = (UniAdCallbackRecord) that;
|
||||
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
|
||||
&& (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
|
||||
&& (this.getPlatform() == null ? other.getPlatform() == null : this.getPlatform().equals(other.getPlatform()))
|
||||
&& (this.getTransId() == null ? other.getTransId() == null : this.getTransId().equals(other.getTransId()))
|
||||
&& (this.getAdpid() == null ? other.getAdpid() == null : this.getAdpid().equals(other.getAdpid()))
|
||||
&& (this.getProvider() == null ? other.getProvider() == null : this.getProvider().equals(other.getProvider()))
|
||||
&& (this.getSign() == null ? other.getSign() == null : this.getSign().equals(other.getSign()))
|
||||
&& (this.getExtra() == null ? other.getExtra() == null : this.getExtra().equals(other.getExtra()))
|
||||
&& (this.getIsEnded() == null ? other.getIsEnded() == null : this.getIsEnded().equals(other.getIsEnded()))
|
||||
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
|
||||
&& (this.getErrMsg() == null ? other.getErrMsg() == null : this.getErrMsg().equals(other.getErrMsg()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
|
||||
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
|
||||
result = prime * result + ((getPlatform() == null) ? 0 : getPlatform().hashCode());
|
||||
result = prime * result + ((getTransId() == null) ? 0 : getTransId().hashCode());
|
||||
result = prime * result + ((getAdpid() == null) ? 0 : getAdpid().hashCode());
|
||||
result = prime * result + ((getProvider() == null) ? 0 : getProvider().hashCode());
|
||||
result = prime * result + ((getSign() == null) ? 0 : getSign().hashCode());
|
||||
result = prime * result + ((getExtra() == null) ? 0 : getExtra().hashCode());
|
||||
result = prime * result + ((getIsEnded() == null) ? 0 : getIsEnded().hashCode());
|
||||
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
|
||||
result = prime * result + ((getErrMsg() == null) ? 0 : getErrMsg().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", id=").append(id);
|
||||
sb.append(", userId=").append(userId);
|
||||
sb.append(", platform=").append(platform);
|
||||
sb.append(", transId=").append(transId);
|
||||
sb.append(", adpid=").append(adpid);
|
||||
sb.append(", provider=").append(provider);
|
||||
sb.append(", sign=").append(sign);
|
||||
sb.append(", extra=").append(extra);
|
||||
sb.append(", isEnded=").append(isEnded);
|
||||
sb.append(", createTime=").append(createTime);
|
||||
sb.append(", errMsg=").append(errMsg);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.sqx.modules.callback.mapper;
|
||||
|
||||
import com.sqx.modules.callback.entity.UniAdCallbackRecord;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【uni_ad_callback_record】的数据库操作Mapper
|
||||
* @createDate 2024-12-23 10:37:52
|
||||
* @Entity com.sqx.modules.callback.entity.UniAdCallbackRecord
|
||||
*/
|
||||
@Mapper
|
||||
public interface UniAdCallbackRecordMapper extends BaseMapper<UniAdCallbackRecord> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.sqx.modules.callback.service;
|
||||
|
||||
import com.sqx.modules.callback.dao.UniAdCallBackDTO;
|
||||
import com.sqx.modules.callback.entity.UniAdCallbackRecord;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【uni_ad_callback_record】的数据库操作Service
|
||||
* @createDate 2024-12-23 10:37:52
|
||||
*/
|
||||
public interface UniAdCallbackRecordService extends IService<UniAdCallbackRecord> {
|
||||
|
||||
Map<String, Object> adCallBack(UniAdCallBackDTO callBackDTO);
|
||||
|
||||
HashMap<String, Object> getStateByExtraKey(Long userId, String extraKey);
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.sqx.modules.callback.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.sqx.modules.app.dao.UserDao;
|
||||
import com.sqx.modules.app.entity.UserEntity;
|
||||
import com.sqx.modules.callback.dao.UniAdCallBackDTO;
|
||||
import com.sqx.modules.callback.entity.UniAdCallbackRecord;
|
||||
import com.sqx.modules.callback.service.UniAdCallbackRecordService;
|
||||
import com.sqx.modules.callback.mapper.UniAdCallbackRecordMapper;
|
||||
import com.sqx.modules.common.dao.CommonInfoDao;
|
||||
import com.sqx.modules.common.entity.CommonInfo;
|
||||
import com.sqx.modules.redisService.RedisService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【uni_ad_callback_record】的数据库操作Service实现
|
||||
* @createDate 2024-12-23 10:37:52
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UniAdCallbackRecordServiceImpl extends ServiceImpl<UniAdCallbackRecordMapper, UniAdCallbackRecord>
|
||||
implements UniAdCallbackRecordService{
|
||||
@Value("${sqx.uni.adSecret}")
|
||||
private String adSecurity;
|
||||
|
||||
private final UserDao userDao;
|
||||
private final CommonInfoDao commonInfoDao;
|
||||
|
||||
private final RedisService redisService;
|
||||
|
||||
public UniAdCallbackRecordServiceImpl(UserDao userDao, CommonInfoDao commonInfoDao, RedisService redisService) {
|
||||
this.userDao = userDao;
|
||||
this.commonInfoDao = commonInfoDao;
|
||||
this.redisService = redisService;
|
||||
}
|
||||
|
||||
private String getBaseErrInfo(UniAdCallbackRecord callbackRecord) {
|
||||
return StrUtil.format("广告获取免费观看时长回调异常: {}, 用户id: {}, 广告播放回调trans_id: {}",
|
||||
callbackRecord.getErrMsg(), callbackRecord.getUserId(), callbackRecord.getTransId());
|
||||
}
|
||||
|
||||
// 生成签名
|
||||
public static String generateSign(String secret, String transId) {
|
||||
// 生成待加密的字符串
|
||||
String data = secret + ":" + transId;
|
||||
|
||||
// 使用SHA-256生成签名
|
||||
return DigestUtil.sha256Hex(data);
|
||||
}
|
||||
|
||||
// 验证签名
|
||||
public static boolean validateSign(String secret, String transId, String providedSign) {
|
||||
// 生成系统计算出来的签名
|
||||
String generatedSign = generateSign(secret, transId);
|
||||
|
||||
// 比较计算出来的签名和提供的签名
|
||||
return StrUtil.equalsIgnoreCase(generatedSign, providedSign);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> adCallBack(UniAdCallBackDTO callBackDTO) {
|
||||
HashMap<String, Object> respData = new HashMap<>();
|
||||
UniAdCallbackRecord one = getOne(new LambdaQueryWrapper<UniAdCallbackRecord>()
|
||||
.eq(UniAdCallbackRecord::getTransId, callBackDTO.getTrans_id()));
|
||||
if (one != null) {
|
||||
log.warn("回调重复, {}", one.getTransId());
|
||||
respData.put("isValid", false);
|
||||
return respData;
|
||||
}
|
||||
UniAdCallbackRecord callbackRecord = new UniAdCallbackRecord();
|
||||
callbackRecord.setUserId(Long.valueOf(callBackDTO.getUser_id()));
|
||||
callbackRecord.setPlatform(callBackDTO.getPlatform());
|
||||
callbackRecord.setTransId(callBackDTO.getTrans_id());
|
||||
callbackRecord.setAdpid(callBackDTO.getAdpid());
|
||||
callbackRecord.setProvider(callBackDTO.getProvider());
|
||||
callbackRecord.setSign(callBackDTO.getSign());
|
||||
callbackRecord.setExtra(callBackDTO.getExtra());
|
||||
callbackRecord.setCreateTime(DateUtil.date());
|
||||
callbackRecord.setIsEnded(1);
|
||||
|
||||
boolean flag = validateSign(adSecurity, callBackDTO.getTrans_id(), callBackDTO.getSign());
|
||||
if (!flag) {
|
||||
callbackRecord.setErrMsg("签名验证失败");
|
||||
log.warn(getBaseErrInfo(callbackRecord));
|
||||
save(callbackRecord);
|
||||
respData.put("isValid", false);
|
||||
return respData;
|
||||
}
|
||||
|
||||
UserEntity userEntity = userDao.selectById(callBackDTO.getUser_id());
|
||||
if (userEntity == null) {
|
||||
callbackRecord.setErrMsg("用户不存在");
|
||||
log.warn(getBaseErrInfo(callbackRecord));
|
||||
save(callbackRecord);
|
||||
respData.put("isValid", false);
|
||||
return respData;
|
||||
}
|
||||
|
||||
CommonInfo info = commonInfoDao.findOne(921);
|
||||
if (info == null || StrUtil.isBlank(info.getValue())){
|
||||
callbackRecord.setErrMsg("CommonInfo时长时间未配置");
|
||||
log.warn(getBaseErrInfo(callbackRecord));
|
||||
save(callbackRecord);
|
||||
respData.put("isValid", false);
|
||||
return respData;
|
||||
}
|
||||
|
||||
redisService.setFreeWatchTime(callbackRecord.getUserId(), Integer.valueOf(info.getValue()), true);
|
||||
save(callbackRecord);
|
||||
respData.put("isValid", true);
|
||||
return respData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<String, Object> getStateByExtraKey(Long userId, String extraKey) {
|
||||
UniAdCallbackRecord one = getOne(new LambdaQueryWrapper<UniAdCallbackRecord>().eq(UniAdCallbackRecord::getUserId, userId)
|
||||
.eq(UniAdCallbackRecord::getExtra, extraKey));
|
||||
return new HashMap<String, Object>(){{
|
||||
put("isEnded", one == null ? 0 : 1);
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user