|
|
@@ -4,8 +4,11 @@ import com.etotem.cfc.common.Result;
|
|
|
import com.etotem.cfc.entity.DistributionRelation;
|
|
|
import com.etotem.cfc.entity.DistributionSystem;
|
|
|
import com.etotem.cfc.service.DistributionService;
|
|
|
+import com.etotem.cfc.service.WechatService;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
@@ -13,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import com.etotem.cfc.util.ParamUtils;
|
|
|
@@ -22,17 +26,84 @@ import com.etotem.cfc.util.ParamUtils;
|
|
|
@RequestMapping("/api/distribution/invite")
|
|
|
public class DistributionInviteController {
|
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DistributionInviteController.class);
|
|
|
+
|
|
|
+ /** 团队邀请独立落地页(§11-#11 已拍板;参照家庭邀请 pages/invite/join) */
|
|
|
+ private static final String TEAM_INVITE_LANDING_PAGE = "pages/team-invite/join";
|
|
|
+
|
|
|
@Resource
|
|
|
private DistributionService distributionService;
|
|
|
|
|
|
- @Operation(summary = "生成/获取我的销售邀请码")
|
|
|
+ @Resource
|
|
|
+ private WechatService wechatService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成团队邀请(能力键 {@code team.invitation.create},契约 §5.1/§5.2)。
|
|
|
+ *
|
|
|
+ * <p>返回 {@code inviteCode + 小程序码(wxacode, 指向独立团队落地页) + systemId/teamName/inviterId};
|
|
|
+ * 邀请码复用 {@code distribution_relations.invite_code},不新建表(§7.7)。
|
|
|
+ */
|
|
|
+ @Operation(summary = "生成团队邀请(邀请码 + 小程序码 + 团队预览)")
|
|
|
@PostMapping("/generate")
|
|
|
- public Result<String> generate(
|
|
|
+ public Result<Map<String, Object>> generate(
|
|
|
@RequestAttribute("userId") Long userId,
|
|
|
@RequestBody Map<String, Object> params) {
|
|
|
Long systemId = ParamUtils.getLong(params.get("systemId"));
|
|
|
if (systemId == null) return Result.error("systemId不能为空");
|
|
|
- return distributionService.generateInviteCode(systemId, userId);
|
|
|
+
|
|
|
+ Result<String> codeResult = distributionService.generateInviteCode(systemId, userId);
|
|
|
+ if (codeResult == null) return Result.error("生成邀请码失败");
|
|
|
+ if (!Integer.valueOf(200).equals(codeResult.getCode()) || codeResult.getData() == null) {
|
|
|
+ // 体系不存在 / 未启用 / 用户不在该体系等边界错误原样透传
|
|
|
+ return Result.error(codeResult.getCode(), codeResult.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ String inviteCode = codeResult.getData();
|
|
|
+ Map<String, Object> data = new LinkedHashMap<>();
|
|
|
+ data.put("inviteCode", inviteCode);
|
|
|
+ data.put("systemId", systemId);
|
|
|
+ data.put("teamName", resolveTeamName(systemId, userId));
|
|
|
+ data.put("inviterId", userId);
|
|
|
+ data.put("wxacode", generateTeamWxacode(inviteCode));
|
|
|
+ data.put("landingPage", TEAM_INVITE_LANDING_PAGE);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 团队名预览:取我在该体系下的团队名(查不到时返回空串,不阻断邀请码生成)。 */
|
|
|
+ private String resolveTeamName(Long systemId, Long userId) {
|
|
|
+ try {
|
|
|
+ Result<List<DistributionSystem>> teamsResult = distributionService.getUserTeams(userId);
|
|
|
+ if (teamsResult == null || teamsResult.getData() == null) return "";
|
|
|
+ List<DistributionSystem> teams = teamsResult.getData();
|
|
|
+ for (DistributionSystem team : teams) {
|
|
|
+ if (team != null && systemId.equals(team.getId())) {
|
|
|
+ return team.getName() != null ? team.getName() : "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("查询团队名失败 systemId={} userId={}", systemId, userId, e);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 团队邀请小程序码(scene 带 {@code code=邀请码},落地页独立)。
|
|
|
+ * 微信能力不可用(测试模式 / 未配置 / 调用异常)时退化为可直接跳转的落地页路径,
|
|
|
+ * 保证邀请码仍可分享,不因小程序码失败阻断生成。
|
|
|
+ */
|
|
|
+ private String generateTeamWxacode(String inviteCode) {
|
|
|
+ String scene = "code=" + inviteCode;
|
|
|
+ if (wechatService != null) {
|
|
|
+ try {
|
|
|
+ String base64 = wechatService.generateWxacode(scene, TEAM_INVITE_LANDING_PAGE);
|
|
|
+ if (base64 != null && !base64.isEmpty()) {
|
|
|
+ return "data:image/png;base64," + base64;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("生成团队邀请小程序码失败,退化为落地页路径 inviteCode={}", inviteCode, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return TEAM_INVITE_LANDING_PAGE + "?scene=" + scene;
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "校验邀请码有效性")
|