|
|
@@ -15,32 +15,63 @@
|
|
|
*/
|
|
|
package io.jpress.ui.freemarker.tag;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import io.jpress.core.render.freemarker.JTag;
|
|
|
import io.jpress.utils.HttpUtils;
|
|
|
+import io.jpress.utils.StringUtils;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class ApiDataTag extends JTag {
|
|
|
|
|
|
public static final String TAG_NAME = "jp.apiData";
|
|
|
|
|
|
+ /**
|
|
|
+ * api id和url 配置容器
|
|
|
+ */
|
|
|
+ public static Map<String, ApiData> apiDataCollection = new HashMap<String, ApiData>();
|
|
|
+
|
|
|
HttpServletRequest request;
|
|
|
|
|
|
public ApiDataTag(HttpServletRequest request) {
|
|
|
this.request = request;
|
|
|
+ if (CollectionUtils.isEmpty(apiDataCollection)) {
|
|
|
+ initCollection();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onRender() {
|
|
|
|
|
|
String id = getParam("id");
|
|
|
+ ApiData apiData = apiDataCollection.get(id);
|
|
|
+ if (apiData != null) {
|
|
|
+ setVariable("data", apiData.getData());
|
|
|
|
|
|
+ }
|
|
|
setVariable("id", id);
|
|
|
|
|
|
renderBody();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 初始化容器 <br/>
|
|
|
+ * 扩展只需要往容器里丢id和url,就可以通过id获取数据
|
|
|
+ */
|
|
|
+ public static void initCollection() {
|
|
|
+ // 商城最新询价
|
|
|
+ apiDataCollection.put("mall_inquiry_new", new ApiData("mall_inquiry_new", "https://www.usoftmall.com/inquiry/public?pageNumber=1&pageSize=10"));
|
|
|
+ // 商城热卖店铺
|
|
|
+ apiDataCollection.put("mall_stores_hot", new ApiData("mall_stores_hot", "https://www.usoftmall.com/api/store-service/stores?page=1&count=8&type=AGENCY-DISTRIBUTION&field=&op=similar"));
|
|
|
+ // 商城热卖产品
|
|
|
+ apiDataCollection.put("mall_products_new", new ApiData("mall_products_new", "https://cms.usoftmall.com/cmsApi?method=queryContentPage&module=home_expand_f2&orderBy=order_number%20ASC"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* api 地址及数据结果类
|
|
|
*/
|
|
|
@@ -73,6 +104,14 @@ public class ApiDataTag extends JTag {
|
|
|
}
|
|
|
|
|
|
public String getData() {
|
|
|
+ // 空值时抓取
|
|
|
+ if (StringUtils.isBlank(data)) {
|
|
|
+ sendRequest();
|
|
|
+ }
|
|
|
+ // 过期时重新抓取,4小时失效
|
|
|
+ if (System.currentTimeMillis() - this.updateTime.getTime() > 60 * 1000 * 60 * 4) {
|
|
|
+ sendRequest();
|
|
|
+ }
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
@@ -94,6 +133,15 @@ public class ApiDataTag extends JTag {
|
|
|
public void sendRequest() {
|
|
|
try {
|
|
|
String responseData = HttpUtils.get(this.url);
|
|
|
+ // 为了防止freemarker在把字符串转json时报错,需要将json中的空对象转为空字符串
|
|
|
+ // responseData = responseData.replaceAll(":null", ":\"\"");
|
|
|
+ // 将字符串转json再转字符串,这样可以生产比较严格的json字符串,但如果字符串不是json格式或者是jsonArray格式,会抛异常
|
|
|
+ try {
|
|
|
+ Object jsonObject = JSON.parse(responseData);
|
|
|
+ responseData = JSON.toJSONString(jsonObject);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 如果不是json格式,会跑异常,不处理
|
|
|
+ }
|
|
|
this.data = responseData;
|
|
|
this.updateTime = new Date();
|
|
|
} catch (Exception e) {
|