From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001
From: lee <4766465@qq.com>
Date: Wed, 18 Dec 2024 13:27:00 +0800
Subject: [PATCH] init

---
 yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnQrCodeController.java |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 118 insertions(+), 0 deletions(-)

diff --git a/yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnQrCodeController.java b/yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnQrCodeController.java
new file mode 100644
index 0000000..28fb2ff
--- /dev/null
+++ b/yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnQrCodeController.java
@@ -0,0 +1,118 @@
+package com.yami.shop.api.controller.cdn;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.yami.shop.bean.model.User;
+import com.yami.shop.bean.param.CdnQrcodeParam;
+import com.yami.shop.common.bean.HuaWeiOss;
+import com.yami.shop.common.bean.TaskPageUrl;
+import com.yami.shop.common.config.Constant;
+import com.yami.shop.common.response.ServerResponseEntity;
+import com.yami.shop.service.SysConfigService;
+import com.yami.shop.service.UserService;
+import com.yami.shop.service.impl.CdnQRCodeService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Random;
+import java.util.Set;
+
+@RestController
+@RequestMapping("/p/QRCode")
+@Tag(name = "任务分享二维码生成")
+public class CdnQrCodeController {
+    @Autowired
+    private CdnQRCodeService qrCodeService;
+
+    @Autowired
+    private SysConfigService sysConfigService;
+
+    @Autowired
+    private UserService userService;
+
+    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+    private static final int CODE_LENGTH = 6;
+
+
+
+    @GetMapping("/{userId}")
+    @Operation(summary = "生成邀请二维码", description = "生成邀请二维码")
+    @Parameter(name = "userId", description = "用户id" )
+    public ServerResponseEntity<CdnQrcodeParam> getQRCode(@PathVariable("userId") String userId) {
+        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUserId, userId));
+        if (user != null) {
+            CdnQrcodeParam cdnQrcodeParam = new CdnQrcodeParam();
+            if (user.getQrCode() != null && !"".equals(user.getQrCode())) {
+                cdnQrcodeParam.setInviteCode(user.getInviteCode());
+                cdnQrcodeParam.setQrCodeUrl(user.getQrCode());
+                cdnQrcodeParam.setLink(user.getInviteLink());
+                return ServerResponseEntity.success(cdnQrcodeParam);
+            }
+            String value = sysConfigService.getValue(Constant.TASK_PAGE_URL_CONFIG);
+            TaskPageUrl taskPageUrl = JSONObject.parseObject(value,TaskPageUrl.class);
+            String page; //页面地址
+            String content = ""; // 拼接二维码链接
+            page = taskPageUrl.getRegisterPage(); // 注册页
+//            String inviteCode = IdUtil.randomUUID().replace("-", "").substring(0, 8); // 邀请码
+            String zeros = "";
+            if (userId.length() < 6) {
+                int numZerosNeeded = 6 - userId.length();
+                zeros = String.format("%0" + numZerosNeeded + "d", 0);
+            }
+            String formattedUserId = zeros + userId;
+            String inviteCode = "A" + formattedUserId;
+            HuaWeiOss aliOss = sysConfigService.getSysConfigObject(Constant.HUAWEI_OBS_CONFIG, HuaWeiOss.class);
+            content = page + "?inviteCode=" + inviteCode;
+            cdnQrcodeParam.setLink(content);
+            String qrCodeUrl = qrCodeService.generateQRCodeUrl(content, aliOss);
+            cdnQrcodeParam.setInviteCode(inviteCode);
+            cdnQrcodeParam.setQrCodeUrl(qrCodeUrl);
+            if (StringUtils.isBlank(user.getInviteCode())) {
+                user.setQrCode(qrCodeUrl);
+                user.setInviteCode(inviteCode);
+                user.setInviteLink(content);
+                userService.updateById(user);
+            }
+            return ServerResponseEntity.success(cdnQrcodeParam);
+        }
+        return ServerResponseEntity.showFailMsg("用户不存在");
+    }
+
+    /**
+     * 生成一个六位的不重复邀请码。
+     *
+     * @param existingCodes 已存在的邀请码集合,用于检查新生成的邀请码是否重复。
+     * @return 新生成的六位不重复邀请码,如果无法生成唯一的邀请码,则返回null。
+     */
+    public static String generateUniqueInviteCode(Set<String> existingCodes) {
+        Random random = new Random();
+        StringBuilder sb = new StringBuilder(CODE_LENGTH);
+
+        while (true) {
+            // 重置StringBuilder以准备构造新的邀请码
+            sb.setLength(0);
+
+            for (int i = 0; i < CODE_LENGTH; i++) {
+                // 随机选择一个字符并添加到StringBuilder中
+                sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
+            }
+
+            // 构建完成的邀请码
+            String newCode = sb.toString();
+
+            // 检查新生成的邀请码是否已经存在
+            if (!existingCodes.contains(newCode)) {
+                // 如果是新的邀请码,添加到集合中并返回
+                existingCodes.add(newCode);
+                return newCode;
+            }
+        }
+    }
+}

--
Gitblit v1.9.3