ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package jnpf.message.controller;
 
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jnpf.base.controller.SuperController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.MsgCode;
import jnpf.consts.DeviceType;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.message.entity.ShortLinkEntity;
import jnpf.message.service.ShortLinkService;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
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 jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
 
@Slf4j
@RestController
@Tag(name = "短链接跳转", description = "message")
@RequestMapping("/ShortLink")
public class ShortLinkController extends SuperController<ShortLinkService, ShortLinkEntity> {
    @Autowired
    private ShortLinkService shortLinkService;
    @Autowired
    private ConfigValueUtil configValueUtil;
    @Autowired
    protected AuthUtil authUtil;
 
 
 
    /**
     * 消息发送配置弹窗列表
     *
     *
     * @return
     */
    @Operation(summary = "根据短链接获取实际链接地址")
    @Parameters({
            @Parameter(name = "shortLink", description = "短链接", required = true),
            @Parameter(name = "tenant", description = "租户")
    })
    @NoDataSourceBind
    @GetMapping(value = {"/{shortLink}/{tenant}","/{shortLink}"})
    public ActionResult getShortUrl(@PathVariable("shortLink") String shortLink,@PathVariable(value = "tenant" , required = false) String tenant,HttpServletResponse response) throws LoginException, IOException {
        if (configValueUtil.isMultiTenancy()) {
            if (StringUtil.isNotEmpty(tenant)) {
                //切换成租户库
                TenantDataSourceUtil.switchTenant(tenant);
            } else {
                return ActionResult.fail(MsgCode.LOG115.get());
            }
        }
        String link = new String();
        ShortLinkEntity entity = shortLinkService.getInfoByLink(shortLink);
        String frontDomain = configValueUtil.getFrontDomain();
        String appDomain = configValueUtil.getAppDomain();
        String realPcLink = entity.getRealPcLink();
        String realAppLink = entity.getRealAppLink();
        if (!realPcLink.contains("http")) {
            realPcLink = frontDomain + realPcLink;
        }
        if (!realAppLink.contains("http")) {
            realAppLink = appDomain + realAppLink;
        }
        DeviceType type = UserProvider.getDeviceForAgent();
        if (entity != null) {
//            String encode = "";
            String token = authUtil.loginTempUser(entity.getUserId(), tenant);
            if (StringUtil.isEmpty(token)) {
                return ActionResult.fail(MsgCode.AD104.get());
            }
//            if(StringUtil.isNotEmpty(entity.getShortLink())) {
//                String bodyText = entity.getBodyText();
//                Map<String, Object> map = new HashMap<>();
//                map = JsonUtil.stringToMap(bodyText);
//                map.put("token", token);
//                bodyText = map.toString();
//                byte[] bytes = bodyText.getBytes(StandardCharsets.UTF_8);
//                encode = Base64.getEncoder().encodeToString(bytes);
//            }
            if (entity.getIsUsed() == 1) {
                if (entity.getClickNum() < entity.getUnableNum() && entity.getUnableTime().after(DateUtil.getNowDate())) {
                    if (DeviceType.PC.equals(type)) {
                        link = realPcLink + "&token=" + token;
                        entity.setClickNum(entity.getClickNum()+1);
                        shortLinkService.updateById(entity);
                    } else {
                        link = realAppLink + "&token=" + token;
                        entity.setClickNum(entity.getClickNum()+1);
                        shortLinkService.updateById(entity);
                    }
                } else {
                    return ActionResult.fail(MsgCode.FA039.get());
                }
            } else {
                if (entity.getUnableTime().after(DateUtil.getNowDate())) {
                    if (DeviceType.PC.equals(type)) {
                        link = realPcLink + "&token=" + token;
                        entity.setClickNum(entity.getClickNum()+1);
                        shortLinkService.updateById(entity);
                    } else {
                        link = realAppLink + "&token=" + token;
                        entity.setClickNum(entity.getClickNum()+1);
                        shortLinkService.updateById(entity);
                    }
                } else {
                    return ActionResult.fail(MsgCode.FA039.get());
                }
            }
 
        } else {
            return ActionResult.fail(MsgCode.FA039.get());
        }
        response.sendRedirect(link);
        return ActionResult.success("");
    }
 
}