All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ajaxjs.framework.baidu_tongji.BaseTongjiController Maven / Gradle / Ivy

Go to download

AJAXJS aims to full-stack, not only the server-side framework, but also integrates the front-end library. It'€™s written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated, emphasizing the lightweight, and fast, very suitable for the Internet fast application.

The newest version!
package com.ajaxjs.framework.baidu_tongji;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import com.ajaxjs.framework.BaseController;
import com.ajaxjs.net.http.Post;
import com.ajaxjs.util.DateUtil;
import com.ajaxjs.util.map.JsonHelper;

/**
 * 百度统计
 * 
 * @author Frank Cheung
 *
 */
public abstract class BaseTongjiController extends BaseController {
	@Autowired
	private Config cfg;

	/**
	 * 请求 API
	 *
	 * @param body
	 * @return
	 */
	private String getData(Map body) {
		// 生成请求头
		Map header = new HashMap() {
			private static final long serialVersionUID = 1L;

			{
				put("username", cfg.getApi_username());
				put("password", cfg.getApi_password());
				put("token", cfg.getApi_token());
				put("account_type", 1);
			}
		};

		String _header = JsonHelper.toJson(header);
		String params = String.format("{\"header\": %s, \"body\": %s}", _header, JsonHelper.toJson(body));
		String json = Post.post("https://api.baidu.com/json/tongji/v1/ReportService/getData", params, conn -> conn.addRequestProperty("Content-type", "application/json"))
				.toString();

		return json;
	}

	private final static SimpleDateFormat FORMATER = DateUtil.simpleDateFormatFactory("yyyyMMdd");

	@GetMapping(value = "getTimeTrendRpt", produces = JSON)
	public String getTimeTrendRpt() {
		Date now = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.add(Calendar.DAY_OF_MONTH, -1);// 昨天
		String today = FORMATER.format(now), yesterday = FORMATER.format(calendar.getTime());

		Map body = new HashMap() {
			private static final long serialVersionUID = 1L;

			{
				put("site_id", cfg.getSiteId());
				put("start_date", yesterday);
				put("end_date", today);
				put("method", "overview/getTimeTrendRpt");
				put("metrics", "pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time");
			}
		};

		return getData(body);
	}

	@GetMapping(value = "getCommonTrackRpt", produces = JSON)
	public String getCommonTrackRpt() {
		Date now = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.add(Calendar.DAY_OF_MONTH, -1);// 昨天
		String today = FORMATER.format(now), yesterday = FORMATER.format(calendar.getTime());

		Map body = new HashMap() {
			private static final long serialVersionUID = 1L;

			{
				put("site_id", cfg.getSiteId());
				put("start_date", yesterday);
				put("end_date", today);
				put("method", "overview/getCommonTrackRpt");
			}
		};

		return getData(body);
	}

	@GetMapping(value = "getTrend", produces = JSON)
	public String getTrend(String start_date, String end_date) {
		Map body = new HashMap() {
			private static final long serialVersionUID = 1L;

			{
				put("site_id", cfg.getSiteId());
				put("start_date", start_date);
				put("end_date", end_date);
				put("method", "trend/time/a");
				put("metrics", "pv_count,visit_count");
				put("max_results", "0");
				put("gran", "day");
			}
		};

		return getData(body);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy