Merge branch 'dev' of fanxb/bookmark into master

This commit is contained in:
fanxb 2022-03-31 13:33:03 +08:00 committed by Gogs
commit 77086daeb9
9 changed files with 134 additions and 12 deletions

View File

@ -13,4 +13,8 @@ public class NumberConstant {
* 2^10 * 2^10
*/ */
public static final int K_SIZE = 1024; public static final int K_SIZE = 1024;
/**
* 一天的秒数
*/
public static final int S_DAY = 24 * 60 * 60;
} }

View File

@ -35,4 +35,6 @@ public class RedisConstant {
public static String getUserFailCountKey(String username) { public static String getUserFailCountKey(String username) {
return "bookmark_user_fail_count_" + username; return "bookmark_user_fail_count_" + username;
} }
public static final String BING_IMG = "bing_img";
} }

View File

@ -0,0 +1,20 @@
package com.fanxb.bookmark.common.entity.vo;
import lombok.Data;
/**
* 全局公共配置
*
* @author fanxb
*/
@Data
public class GlobalConfigVo {
/**
* 是否存在网络代理(外网访问)
*/
private boolean proxyExist;
/**
* bing每日一图地址
*/
private String bingImgSrc;
}

View File

@ -142,7 +142,7 @@ public class LoginFilter implements Filter {
UserContextHolder.set(context); UserContextHolder.set(context);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
log.error("jwt解密失败{},原因:{}", jwt, e.getMessage()); log.info("jwt解密失败{},原因:{}", jwt, e.getMessage());
return false; return false;
} }
} }

View File

@ -0,0 +1,28 @@
package com.fanxb.bookmark.common.schedule;
import com.fanxb.bookmark.common.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author fanxb
*/
@Component
public class BingImgSchedule {
private final ConfigService configService;
@Autowired
public BingImgSchedule(ConfigService configService) {
this.configService = configService;
}
@PostConstruct
@Scheduled(cron = "* 0 0/1 * * *")
public void cache() {
configService.getCacheBingImg();
}
}

View File

@ -1,6 +1,6 @@
package com.fanxb.bookmark.common.service; package com.fanxb.bookmark.common.service;
import java.util.Map; import com.fanxb.bookmark.common.entity.vo.GlobalConfigVo;
/** /**
* 全局配置相关 * 全局配置相关
@ -17,5 +17,12 @@ public interface ConfigService {
* @author fanxb * @author fanxb
* @date 2021/9/15 下午9:58 * @date 2021/9/15 下午9:58
*/ */
Map<String, Object> getGlobalConfig(); GlobalConfigVo getGlobalConfig();
/**
* 缓存bing每日一图到redis
*
* @author fanxb
*/
String getCacheBingImg();
} }

View File

@ -1,23 +1,77 @@
package com.fanxb.bookmark.common.service.impl; package com.fanxb.bookmark.common.service.impl;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.fanxb.bookmark.common.constant.NumberConstant;
import com.fanxb.bookmark.common.constant.RedisConstant;
import com.fanxb.bookmark.common.entity.vo.GlobalConfigVo;
import com.fanxb.bookmark.common.service.ConfigService; import com.fanxb.bookmark.common.service.ConfigService;
import com.fanxb.bookmark.common.util.HttpUtil; import com.fanxb.bookmark.common.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
/** /**
* @author fanxb * @author fanxb
* @date 2021-09-15-下午9:59 * @date 2021-09-15-下午9:59
*/ */
@Service @Service
@Slf4j
public class ConfigServiceImpl implements ConfigService { public class ConfigServiceImpl implements ConfigService {
@Override
public Map<String, Object> getGlobalConfig() { private final StringRedisTemplate stringRedisTemplate;
Map<String, Object> res = new HashMap<>(1);
res.put("proxyExist", HttpUtil.getProxyExist()); @Autowired
return res; public ConfigServiceImpl(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
} }
@Value("${bing.host}")
private String bingHost;
@Value("${bing.onePic}")
private String bingUrl;
@Override
public GlobalConfigVo getGlobalConfig() {
GlobalConfigVo vo = new GlobalConfigVo();
vo.setProxyExist(HttpUtil.getProxyExist());
vo.setBingImgSrc(getCacheBingImg());
return vo;
}
@Override
public String getCacheBingImg() {
String str = stringRedisTemplate.opsForValue().get(RedisConstant.BING_IMG);
if (str != null) {
return str;
}
str = getBingImg();
if (str != null) {
stringRedisTemplate.opsForValue().set(RedisConstant.BING_IMG, str, 2, TimeUnit.HOURS);
}
return str;
}
private String getBingImg() {
try {
JSONObject bingObj = HttpUtil.getObj(bingHost + bingUrl, null, false);
String path = bingObj.getJSONArray("images").getJSONObject(0).getString("url");
return bingHost + path;
} catch (Exception e) {
log.error("获取bing每日一图错误{}", e.getLocalizedMessage(), e);
}
return null;
}
} }

View File

@ -91,6 +91,10 @@ proxy:
# url icon服务提供地址 # url icon服务提供地址
urlIconAddress: http://localhost:11001 urlIconAddress: http://localhost:11001
bing:
host: https://cn.bing.com
onePic: /HPImageArchive.aspx?format=js&idx=0&n=1
# 管理员用户id(因为目前尚未设计分角色权限系统须指定管理员用户id) # 管理员用户id(因为目前尚未设计分角色权限系统须指定管理员用户id)
manageUserId: -1 manageUserId: -1

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="main"> <div class="main" :style="{ background: backgroundImg }">
<top /> <top />
<div class="content"> <div class="content">
<search :style="{ width: isPhone ? '100%' : '60%' }" /> <search :style="{ width: isPhone ? '100%' : '60%' }" />
@ -15,7 +15,7 @@ import Bottom from "@/layout/home/Bottom.vue";
import Search from "@/components/main/Search.vue"; import Search from "@/components/main/Search.vue";
import PinBookmark from "./PinBookmark.vue"; import PinBookmark from "./PinBookmark.vue";
import { mapState } from "vuex"; import { mapState } from "vuex";
import { GLOBAL_CONFIG, IS_PHONE } from "@/store/modules/globalConfig"; import { GLOBAL_CONFIG, IS_PHONE, SERVER_CONFIG } from "@/store/modules/globalConfig";
export default { export default {
name: "HOME", name: "HOME",
components: { components: {
@ -28,7 +28,11 @@ export default {
return {}; return {};
}, },
computed: { computed: {
...mapState(GLOBAL_CONFIG, [IS_PHONE, IS_PHONE]), ...mapState(GLOBAL_CONFIG, [IS_PHONE, SERVER_CONFIG]),
backgroundImg() {
let url = this.serverConfig.bingImgSrc ? this.serverConfig.bingImgSrc : "/static/img/homeBg.jpg";
return `url("${url}") no-repeat center center`;
},
}, },
methods: {}, methods: {},
}; };
@ -38,7 +42,6 @@ export default {
.main { .main {
width: 100%; width: 100%;
height: 100vh; height: 100vh;
background: url("/static/img/homeBg.jpg") no-repeat center center;
background-size: cover; background-size: cover;
background-attachment: fixed; background-attachment: fixed;