Przeglądaj źródła

短链接跳转

plf 4 lat temu
rodzic
commit
e8eb036f44

+ 44 - 0
src/main/java/com/caimei/www/controller/ShortLinkApi.java

@@ -0,0 +1,44 @@
+package com.caimei.www.controller;
+
+import com.caimei.www.service.link.ShortLinkService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.server.reactive.ServerHttpResponse;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+import org.thymeleaf.util.StringUtils;
+
+import java.net.URI;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/6/24
+ */
+@RestController
+public class ShortLinkApi {
+    private ShortLinkService shortLinkService;
+
+    @Autowired
+    public void setShortLinkService(ShortLinkService shortLinkService) {
+        this.shortLinkService = shortLinkService;
+    }
+
+    /**
+     * 短链接跳转
+     *
+     * @param link
+     * @param response
+     */
+    @GetMapping("/t/{link}")
+    public void linkJump(@PathVariable String link, ServerHttpResponse response) {
+        String jumpLink = shortLinkService.linkJump(link);
+        response.setStatusCode(HttpStatus.FOUND);
+        if (StringUtils.isEmpty(jumpLink)) {
+            response.getHeaders().setLocation(URI.create("https://www.caimei365.com/404.html"));
+        }
+        response.getHeaders().setLocation(URI.create(jumpLink));
+    }
+}

+ 29 - 0
src/main/java/com/caimei/www/mapper/ShortLinkDao.java

@@ -0,0 +1,29 @@
+package com.caimei.www.mapper;
+
+import com.caimei.www.pojo.link.ShortLink;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/6/25
+ */
+@Mapper
+public interface ShortLinkDao {
+
+    /**
+     * 查询跳转链接
+     *
+     * @param link
+     * @return
+     */
+    ShortLink findByShortLink(String link);
+
+    /**
+     * 修改点击数量
+     *
+     * @param markId
+     */
+    void updateOnClick(Integer markId);
+}

+ 24 - 0
src/main/java/com/caimei/www/pojo/link/ShortLink.java

@@ -0,0 +1,24 @@
+package com.caimei.www.pojo.link;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/6/25
+ */
+@Data
+public class ShortLink implements Serializable {
+    /**
+     * 标识id
+     */
+    private Integer markId;
+
+    /**
+     * 跳转链接
+     */
+    private String jumpLink;
+}

+ 18 - 0
src/main/java/com/caimei/www/service/link/ShortLinkService.java

@@ -0,0 +1,18 @@
+package com.caimei.www.service.link;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/6/25
+ */
+public interface ShortLinkService {
+
+    /**
+     * 查询短链接
+     *
+     * @param link
+     * @return
+     */
+    String linkJump(String link);
+}

+ 31 - 0
src/main/java/com/caimei/www/service/link/impl/ShortLinkServiceImpl.java

@@ -0,0 +1,31 @@
+package com.caimei.www.service.link.impl;
+
+import com.caimei.www.mapper.ShortLinkDao;
+import com.caimei.www.pojo.link.ShortLink;
+import com.caimei.www.service.link.ShortLinkService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/6/25
+ */
+@Service
+public class ShortLinkServiceImpl implements ShortLinkService {
+    @Resource
+    private ShortLinkDao shortLinkDao;
+
+    @Override
+    public String linkJump(String link) {
+        ShortLink shortLink = shortLinkDao.findByShortLink(link);
+        if (shortLink == null) {
+            return "";
+        }
+        //修改点击数量
+        shortLinkDao.updateOnClick(shortLink.getMarkId());
+        return shortLink.getJumpLink();
+    }
+}

+ 19 - 0
src/main/resources/mapper/ShortLinkMapper.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei.www.mapper.ShortLinkDao">
+    <select id="findByShortLink" resultType="com.caimei.www.pojo.link.ShortLink">
+        SELECT
+          markId,
+          jumpLink
+        FROM
+          cm_short_link
+        WHERE
+          shortLink = #{link}
+    </select>
+
+    <update id="updateOnClick">
+        UPDATE cm_sms_statistics SET onClick = (onClick + 1) WHERE markId = #{markId}
+    </update>
+</mapper>