diff --git a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/constant/RedisKeyConstant.java b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/constant/RedisKeyConstant.java index 903e961..4e1a47b 100644 --- a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/constant/RedisKeyConstant.java +++ b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/constant/RedisKeyConstant.java @@ -97,6 +97,11 @@ public class RedisKeyConstant { * 平台用户下载键 */ public static final String PLATFORM_USER_DOWNLOAD_KEY = "bnyer.img.user.download:"; + + /** + * 平台用户ai绘画键 + */ + public static final String PLATFORM_USER_AI_PAINT_KEY = "bnyer.img.user.aiPaint:"; /** * 艺术家上传键 */ diff --git a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java index 1a98f7a..511c6cc 100644 --- a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java +++ b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java @@ -37,4 +37,10 @@ public class TextToImgDto implements Serializable { @ApiModelProperty(value="绘图者昵称") private String painterName; + @ApiModelProperty(value="应用") + private String appType; + + @ApiModelProperty(value="平台") + private String platform; + } diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java index 463399c..4eb6bce 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java @@ -242,6 +242,13 @@ public class TiktokMiniController extends BaseController { return AjaxResult.success(tiktokUserService.checkUserCanDownload(dto.getUserId(),dto.getPlatform(),dto.getAppType())); } + @ApiOperation(value="检查某平台用户当日ai绘画次数是否超标") + @PostMapping(value = "/checkUserCanAiPaint") + public AjaxResult checkUserCanAiPaint(@Validated @RequestBody @ApiParam("检查超标对象") checkUserCanDownloadDto dto){ + log.debug("【抖音图文小程序】检查某平台用户当日ai绘画次数是否超标参数为:{}", JSON.toJSONString(dto)); + return AjaxResult.success(tiktokUserService.checkUserCanAiPaint(dto.getUserId(),dto.getPlatform(),dto.getAppType())); + } + @ApiOperation(value="新增/更新艺术家即将入账广告收益") @PostMapping(value = "/insertOrUpdatePreAdProfit") diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokUserService.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokUserService.java index 3cd9d52..4fa054a 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokUserService.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokUserService.java @@ -22,6 +22,15 @@ public interface TiktokUserService { */ boolean checkUserCanDownload(Long userId,String platform,String appType); + /** + * 检查某平台用户当日ai绘画次数是否超标 + * @param userId 用户id + * @param platform 平台 + * @param appType 应用 + * @return - + */ + boolean checkUserCanAiPaint(Long userId,String platform,String appType); + /** * 获取抖音小程序用户 * @param dto 登录参数 diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java index a69129d..1fccd3a 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java @@ -3,6 +3,7 @@ package com.bnyer.img.service.impl; import cn.hutool.core.util.IdUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.bnyer.common.core.constant.RedisKeyConstant; import com.bnyer.common.core.domain.AiPaint; import com.bnyer.common.core.domain.R; import com.bnyer.common.core.dto.TextToImgDto; @@ -10,6 +11,7 @@ import com.bnyer.common.core.utils.TranslateUtils; import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils; import com.bnyer.common.core.utils.file.ImageUtils; import com.bnyer.common.core.vo.TextToImgVo; +import com.bnyer.common.redis.service.RedisService; import com.bnyer.file.api.RemoteFileService; import com.bnyer.img.service.AiPaintService; import com.bnyer.img.service.StableDiffusionService; @@ -20,6 +22,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -38,6 +41,9 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { @Autowired private RemoteFileService remoteFileService; + @Autowired + private RedisService redisService; + @Override public TextToImgVo textToImg(TextToImgDto param) { try{ @@ -99,6 +105,8 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { paint.setPainterId(param.getPainterId()); paint.setPainterName(param.getPainterName()); aiPaintService.insert(paint); + //写入ai绘画次数 + writePlatformUserAiPaintNum(param.getAppType(),param.getPlatform(),param.getPainterId()); } img.setPaintId(paintId); img.setPaintTime(paintTime); @@ -110,6 +118,21 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { return null; } + /** + * 写入ai绘画次数 + * @param appType 应用 + * @param platform 平台 + * @param userId 用户id + */ + private void writePlatformUserAiPaintNum(String appType,String platform,Long userId){ + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + String date = df.format(new Date()); + String hashKey = appType+":"+platform+":"+userId; + log.info("日期【{}】应用【{}】平台【{}】用户【{}】ai绘画数+1",date,appType,platform,userId); + String redisKey = RedisKeyConstant.PLATFORM_USER_AI_PAINT_KEY + date; + redisService.hashIncr(redisKey, hashKey, 1); + } + @Override public void imgToImg() { diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokUserServiceImpl.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokUserServiceImpl.java index 972bce9..8e895cc 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokUserServiceImpl.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokUserServiceImpl.java @@ -177,6 +177,26 @@ public class TiktokUserServiceImpl implements TiktokUserService { } } + @Override + public boolean checkUserCanAiPaint(Long userId, String platform, String appType) { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + String date = df.format(new Date()); + String hashKey = appType+":"+platform+":"+userId; + String redisKey = RedisKeyConstant.PLATFORM_USER_AI_PAINT_KEY + date; + if(redisService.hasHashKey(redisKey,hashKey)){ + //存在键,判断次数,次数达标则不可ai绘画 + Integer aiPaintNum = redisService.getCacheMapValue(redisKey, hashKey); + if(aiPaintNum >= 3){ + return true; + }else{ + return false; + } + }else{ + //不存在键则可下载 + return false; + } + } + @Override public R getTiktokLoginUserByLoginParam(TiktokLoginDto dto) { TiktokSessionInfoVo sessionInfo = this.getSessionInfo(dto.getCode());