fix:修复首次使用书签树不展示问题
This commit is contained in:
parent
0aac5fdc20
commit
e55c86c407
@ -283,7 +283,7 @@ public class BookmarkServiceImpl implements BookmarkService {
|
||||
}
|
||||
try {
|
||||
URL urlObj = new URL(url);
|
||||
byte[] data = HttpUtil.download(urlIconAddress + "/icon?url=" + urlObj.getHost() + "&size=8..16..32");
|
||||
byte[] data = HttpUtil.download(urlIconAddress + "/icon?url=" + urlObj.getHost() + "&size=8..16..32", false);
|
||||
String base64 = new String(Base64.getEncoder().encode(data));
|
||||
if (StrUtil.isEmpty(base64)) {
|
||||
return "data:image/png;base64," + base64;
|
||||
|
@ -84,7 +84,7 @@ public class PinYinServiceImpl implements PinYinService {
|
||||
public List<String> changeStrings(List<String> stringList) {
|
||||
Map<String, String> header = Collections.singletonMap("token", Constant.pinyinToken);
|
||||
PinYinBody body = PinYinBody.builder().strs(stringList).config(new PinYinBody.Config(false, false, 1)).build();
|
||||
JSONArray result = HttpUtil.post(Constant.pinyinBaseUrl + PATH, JSON.toJSONString(body), header, JSONArray.class);
|
||||
JSONArray result = HttpUtil.postArray(Constant.pinyinBaseUrl + PATH, JSON.toJSONString(body), header, false);
|
||||
List<List<JSONArray>> list = result.toJavaList(JSONArray.class).stream().map(item -> item.toJavaList(JSONArray.class)).collect(Collectors.toList());
|
||||
List<String> res = new ArrayList<>(stringList.size());
|
||||
for (int i = 0, size = stringList.size(); i < size; i++) {
|
||||
|
@ -56,13 +56,13 @@ public class OAuthServiceImpl implements OAuthService {
|
||||
Map<String, String> header = new HashMap<>(2);
|
||||
header.put("accept", "application/json");
|
||||
String url = "https://github.com/login/oauth/access_token?client_id=" + githubClientId + "&client_secret=" + githubSecret + "&code=" + body.getCode();
|
||||
JSONObject obj = HttpUtil.get(url, header);
|
||||
JSONObject obj = HttpUtil.getObj(url, header, true);
|
||||
String accessToken = obj.getString("access_token");
|
||||
if (StrUtil.isEmpty(accessToken)) {
|
||||
throw new CustomException("github登陆失败,请稍后重试");
|
||||
}
|
||||
header.put("Authorization", "token " + accessToken);
|
||||
JSONObject userInfo = HttpUtil.get("https://api.github.com/user", header);
|
||||
JSONObject userInfo = HttpUtil.getObj("https://api.github.com/user", header, true);
|
||||
other.setGithubId(userInfo.getLong("id"));
|
||||
if (other.getGithubId() == null) {
|
||||
log.error("github返回异常:{}", userInfo.toString());
|
||||
|
@ -34,17 +34,25 @@ public class HttpUtil {
|
||||
|
||||
private static final int IP_LENGTH = 15;
|
||||
|
||||
private static OkHttpClient CLIENT;
|
||||
/**
|
||||
* 使用代理环境
|
||||
*/
|
||||
private static OkHttpClient PROXY_CLIENT;
|
||||
/**
|
||||
* 无代理环境
|
||||
*/
|
||||
private static final OkHttpClient CLIENT = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.build();
|
||||
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
if (StrUtil.isNotBlank(proxyIp)) {
|
||||
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort)));
|
||||
}
|
||||
CLIENT = builder.connectTimeout(10, TimeUnit.SECONDS)
|
||||
PROXY_CLIENT = builder.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
@ -53,11 +61,12 @@ public class HttpUtil {
|
||||
* 下载文件
|
||||
* @author fanxb
|
||||
* @param url 下载链接
|
||||
* @param proxy 是否使用代理
|
||||
* @return java.io.InputStream
|
||||
* @date 2021/3/12
|
||||
**/
|
||||
public static byte[] download(String url) {
|
||||
try (Response res = CLIENT.newCall(new Request.Builder().url(url).build()).execute()) {
|
||||
public static byte[] download(String url, boolean proxy) {
|
||||
try (Response res = (proxy ? PROXY_CLIENT : CLIENT).newCall(new Request.Builder().url(url).build()).execute()) {
|
||||
assert res.body() != null;
|
||||
if (checkIsOk(res.code())) {
|
||||
return res.body().byteStream().readAllBytes();
|
||||
@ -70,15 +79,17 @@ public class HttpUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述: get
|
||||
* 功能描述: get请求
|
||||
*
|
||||
* @param url url
|
||||
* @return com.alibaba.fastjson.JSONObject
|
||||
* @param headers header
|
||||
* @param proxy 是否使用代理
|
||||
* @return T
|
||||
* @author fanxb
|
||||
* @date 2020/3/22 21:11
|
||||
* @date 2020/3/22 21:07
|
||||
*/
|
||||
public static JSONObject get(String url) {
|
||||
return get(url, null, JSONObject.class);
|
||||
public static JSONObject getObj(String url, Map<String, String> headers, boolean proxy) {
|
||||
return get(url, headers, JSONObject.class, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,11 +97,13 @@ public class HttpUtil {
|
||||
*
|
||||
* @param url url
|
||||
* @param headers header
|
||||
* @param proxy 是否使用代理
|
||||
* @return T
|
||||
* @author fanxb
|
||||
* @date 2020/3/22 21:07
|
||||
*/
|
||||
public static JSONObject get(String url, Map<String, String> headers) {
|
||||
return get(url, headers, JSONObject.class);
|
||||
public static JSONArray getArray(String url, Map<String, String> headers, boolean proxy) {
|
||||
return get(url, headers, JSONArray.class, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,11 +112,12 @@ public class HttpUtil {
|
||||
* @param url url
|
||||
* @param headers header
|
||||
* @param typeClass type
|
||||
* @param proxy 是否使用代理
|
||||
* @return T
|
||||
* @author fanxb
|
||||
* @date 2020/3/22 21:07
|
||||
*/
|
||||
public static <T> T get(String url, Map<String, String> headers, Class<T> typeClass) {
|
||||
private static <T> T get(String url, Map<String, String> headers, Class<T> typeClass, boolean proxy) {
|
||||
Request.Builder builder = new Request.Builder().url(url);
|
||||
if (headers != null && headers.size() > 0) {
|
||||
Set<String> keys = headers.keySet();
|
||||
@ -111,44 +125,50 @@ public class HttpUtil {
|
||||
builder = builder.addHeader(key, headers.get(key));
|
||||
}
|
||||
}
|
||||
return request(builder.build(), typeClass);
|
||||
return request(builder.build(), typeClass, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述:不带header,返回jsonObject的post方法
|
||||
*
|
||||
* @param url url
|
||||
* @param jsonObj body
|
||||
* @return com.alibaba.fastjson.JSONObject
|
||||
* @author fanxb
|
||||
* @date 2020/3/22 21:05
|
||||
*/
|
||||
public static JSONObject post(String url, String jsonObj) {
|
||||
return post(url, jsonObj, null, JSONObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述:返回jsonObject的post方法
|
||||
*
|
||||
* @param url url
|
||||
* @param jsonObj body
|
||||
* @param headers headers
|
||||
* @return com.alibaba.fastjson.JSONObject
|
||||
* @author fanxb
|
||||
* @date 2020/3/22 21:05
|
||||
*/
|
||||
public static JSONObject post(String url, String jsonObj, Map<String, String> headers) {
|
||||
return post(url, jsonObj, headers, JSONObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送post请求,带header
|
||||
* POST请求,返回jsonObj
|
||||
*
|
||||
* @param url url
|
||||
* @param jsonObj 请求体
|
||||
* @param headers 请求头
|
||||
*/
|
||||
public static <T> T post(String url, String jsonObj, Map<String, String> headers, Class<T> typeClass) {
|
||||
* @param proxy 是否使用代理
|
||||
* @author fanxb
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static JSONObject postObj(String url, String jsonObj, Map<String, String> headers, boolean proxy) {
|
||||
return post(url, jsonObj, headers, JSONObject.class, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求,返回jsonArray
|
||||
*
|
||||
* @param url url
|
||||
* @param jsonObj 请求体
|
||||
* @param headers 请求头
|
||||
* @param proxy 是否使用代理
|
||||
* @author fanxb
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static JSONArray postArray(String url, String jsonObj, Map<String, String> headers, boolean proxy) {
|
||||
return post(url, jsonObj, headers, JSONArray.class, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
*
|
||||
* @param url url
|
||||
* @param jsonObj 请求体
|
||||
* @param headers 请求头
|
||||
* @param typeClass 响应类
|
||||
* @param proxy 是否使用代理
|
||||
* @return T
|
||||
* @author fanxb
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
private static <T> T post(String url, String jsonObj, Map<String, String> headers, Class<T> typeClass, boolean proxy) {
|
||||
RequestBody body = RequestBody.create(JSON, jsonObj);
|
||||
Request.Builder builder = new Request.Builder().url(url).post(body);
|
||||
if (headers != null) {
|
||||
@ -157,16 +177,21 @@ public class HttpUtil {
|
||||
builder = builder.addHeader(key, headers.get(key));
|
||||
}
|
||||
}
|
||||
return request(builder.build(), typeClass);
|
||||
return request(builder.build(), typeClass, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造request,获取响应
|
||||
*
|
||||
* @param request request
|
||||
*/
|
||||
public static <T> T request(Request request, Class<T> typeClass) {
|
||||
try (Response res = CLIENT.newCall(request).execute()) {
|
||||
* @param request 请求request
|
||||
* @param typeClass 相应类型
|
||||
* @param proxy 是否使用代理
|
||||
* @return T
|
||||
* @author fanxb
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static <T> T request(Request request, Class<T> typeClass, boolean proxy) {
|
||||
try (Response res = (proxy ? PROXY_CLIENT : CLIENT).newCall(request).execute()) {
|
||||
return parseResponse(res, typeClass);
|
||||
} catch (Exception e) {
|
||||
throw new CustomException(e);
|
||||
@ -183,17 +208,13 @@ public class HttpUtil {
|
||||
try {
|
||||
assert res.body() != null;
|
||||
if (checkIsOk(res.code())) {
|
||||
if (typeClass.getCanonicalName().equals(InputStream.class.getCanonicalName())) {
|
||||
return (T) res.body().byteStream();
|
||||
}
|
||||
|
||||
String str = res.body().string();
|
||||
if (typeClass.getCanonicalName().equals(JSONObject.class.getCanonicalName())) {
|
||||
return (T) JSONObject.parseObject(str);
|
||||
} else if (typeClass.getCanonicalName().equals(String.class.getCanonicalName())) {
|
||||
} else if (typeClass.getCanonicalName().equals(JSONArray.class.getCanonicalName())) {
|
||||
return (T) str;
|
||||
} else {
|
||||
return (T) JSONArray.parseArray(str);
|
||||
throw new CustomException("仅支持JSONObject,JSONArray");
|
||||
}
|
||||
} else {
|
||||
throw new CustomException("http请求出错:" + res.body().string());
|
||||
|
@ -1,6 +1,5 @@
|
||||
import localforage from "localforage";
|
||||
import httpUtil from "../../util/HttpUtil";
|
||||
import { getUesrInfo } from "../../util/UserUtil";
|
||||
import HttpUtil from "../../util/HttpUtil";
|
||||
|
||||
const TOTAL_TREE_DATA = "totalTreeData";
|
||||
const VERSION = "version";
|
||||
@ -42,8 +41,9 @@ const actions = {
|
||||
if (context.state.isInit || context.state.isIniting) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
context.commit("isIniting", true);
|
||||
let realVersion = await httpUtil.get("/user/version");
|
||||
let realVersion = await HttpUtil.get("/user/version");
|
||||
let data = await localforage.getItem(TOTAL_TREE_DATA);
|
||||
let version = await localforage.getItem(VERSION);
|
||||
if (!data || realVersion > version) {
|
||||
@ -52,8 +52,10 @@ const actions = {
|
||||
context.commit(TOTAL_TREE_DATA, data);
|
||||
context.commit(VERSION, version);
|
||||
}
|
||||
context.commit("isIniting", false);
|
||||
context.commit("isInit", true);
|
||||
} finally {
|
||||
context.commit("isIniting", false);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 确保数据加载完毕
|
||||
@ -74,7 +76,7 @@ const actions = {
|
||||
},
|
||||
//刷新缓存数据
|
||||
async refresh(context) {
|
||||
let treeData = await httpUtil.get("/bookmark/currentUser");
|
||||
let treeData = await HttpUtil.get("/bookmark/currentUser");
|
||||
if (!treeData[""]) {
|
||||
treeData[""] = [];
|
||||
}
|
||||
@ -85,7 +87,7 @@ const actions = {
|
||||
item1.scopedSlots = { title: "nodeTitle" };
|
||||
})
|
||||
);
|
||||
let version = await httpUtil.get("/user/version");
|
||||
let version = await HttpUtil.get("/user/version");
|
||||
await context.dispatch("updateVersion", version);
|
||||
context.commit(TOTAL_TREE_DATA, treeData);
|
||||
await localforage.setItem(TOTAL_TREE_DATA, treeData);
|
||||
|
Loading…
x
Reference in New Issue
Block a user