package com.yeshi.buwan.controller; import com.yeshi.buwan.pptv.PPTVApiUtil; import com.yeshi.buwan.pptv.PPTVUtil; import com.yeshi.buwan.util.AESUtil; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.util.log.LoggerUtil; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @Controller @RequestMapping("pptv") public class PPTVController { @ResponseBody @RequestMapping("user/{code}") public void validCode(@PathVariable String code, HttpServletResponse response) throws IOException { LoggerUtil.getTestLogger().info("PPTVCode回调:{}", code); response.setContentType("application/json;charset=UTF-8"); PPTVUtil.PPTVCodeInfo codeInfo = PPTVUtil.decryptPPTVCode(code); if (codeInfo == null) { LoggerUtil.getTestLogger().info("PPTVCode回调-解码失败:{}", code); response.getWriter().print(loadFalseResult(1, "code校验失败,解码失败")); } else { //间隔时间大于1天 if (System.currentTimeMillis() - codeInfo.time > 1000 * 60 * 60L * 24) { response.getWriter().print(loadFalseResult(2, "code校验失败,时间超限")); return; } LoggerUtil.getTestLogger().info("PPTVCode回调-解码成功:{},{}", code, codeInfo.pptvUid); response.getWriter().print(loadTrueResult(codeInfo.pptvUid)); } } private String loadFalseResult(int code, String msg) { JSONObject root = new JSONObject(); root.put("code", code); root.put("infoMsg", msg); return root.toString(); } private String loadTrueResult(String pptvUid) { JSONObject root = new JSONObject(); root.put("code", 100); root.put("infoMsg", "code校验成功"); JSONObject data = new JSONObject(); data.put("userid", pptvUid); data.put("channelNo", PPTVApiUtil.CHANNEL_ID); data.put("appKey", PPTVApiUtil.APP_KEY); root.put("data", data); return root.toString(); } }