首页>代码>springboot+layui简单易懂的短网址服务搭建,提供源码>/src/main/java/com/yutons/service/ShortUrlConvertService.java
package com.yutons.service;

import com.yutons.response.ResultBean;
import com.yutons.response.ResultCode;
import com.yutons.utils.MyShortUrlUtil;
import com.yutons.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Title 短网址转换服务
 * @Author yutons
 * @Date 2019/7/12 22:50
 * @Desc
 */
@Service
public class ShortUrlConvertService {
    @Value("${domain}")
    private String domain;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 短网址加密生成(为空默认有效期7天,可自定义时长)
     *
     * @param urlLong
     * @param days
     * @return
     */
    public ResultBean shorten(String urlLong, Integer days) {
        days = days == null ? 7 : days;
        //加密短网址,返回短网址URL数组
        String shortUrl = generateUrl(urlLong, days, TimeUnit.DAYS);
        domain = domain.endsWith("/") ? domain : domain + "/";
        shortUrl = domain + shortUrl;
        ResultBean resultBean = new ResultBean<>(shortUrl);
        if (shortUrl==null){
            resultBean=new ResultBean(ResultCode.FAIL,"网址非法,转换失败!");
        }
        return resultBean;
    }

    /**
     * 短网址解密
     *
     * @param shortUrl
     * @return
     */
    public ResultBean shortde(String shortUrl) {
        shortUrl = shortUrl.replaceAll(domain, "");
        //判断网址是否存在
        String url = redisUtil.get("shortUrl:" + shortUrl);
        ResultBean resultBean = new ResultBean<>(url);
        if (url==null){
            resultBean=new ResultBean(ResultCode.FAIL,"短网址非法,解析失败!");
        }
        return resultBean;
    }


    private String generateUrl(String urlLong, Integer times, TimeUnit timeUnit) {
        //加密短网址,返回短网址URL数组
        String[] shortUrls = MyShortUrlUtil.shortUrl(urlLong);

        //循环获取加密url
        for (String shortUrl : shortUrls) {
            //判断短网址是否存在
            Boolean exists = redisUtil.exists("shortUrl:" + shortUrl);
            if (exists) {
                //判断网址是否存在
                String url = redisUtil.get("shortUrl:" + shortUrl);
                if (urlLong.equals(url)) {
                    redisUtil.set("shortUrl:" + shortUrl, urlLong);
                    if (times!=0){
                        redisUtil.expire("shortUrl:" + shortUrl, times, timeUnit);
                    }
                    return shortUrl;
                }
                continue;
            }
            redisUtil.set("shortUrl:" + shortUrl, urlLong);
            if (times!=0){
                redisUtil.expire("shortUrl:" + shortUrl, times, timeUnit);
            }
            return shortUrl;
        }
        return null;
    }
}
最近下载更多
尹恒yingying  LV18 2021年10月23日
yutons  LV9 2021年3月27日
910353504  LV14 2020年12月20日
ck1744  LV3 2020年1月14日
Retuen  LV7 2019年12月18日
robin123456  LV5 2019年10月11日
jp6863655  LV13 2019年9月2日
我本葬花人葬花亦葬人  LV16 2019年8月30日
janiter  LV15 2019年8月28日
乞丐哥哥  LV23 2019年8月22日
最近浏览更多
admin_z  LV22 1月28日
cwb6357123  LV6 2023年9月21日
淡心伤  LV11 2023年8月2日
Lamber 2023年2月28日
暂无贡献等级
gala209  LV3 2023年2月4日
微信网友_6256859815333888 2022年12月11日
暂无贡献等级
123456asdczx  LV1 2022年12月4日
内心向阳  LV4 2022年11月30日
wl010101  LV9 2022年10月22日
小全全  LV1 2022年9月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友