|
@@ -0,0 +1,167 @@
|
|
|
+package com.iamberry.rst.controllers.order;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.iamberry.app.tool.util.HttpUtility;
|
|
|
+//import com.iamberry.redis.RedisUtils;
|
|
|
+import com.iamberry.rst.controllers.mq.TaobaoConfig;
|
|
|
+import com.iamberry.rst.core.sys.SysConfig;
|
|
|
+import com.iamberry.rst.faces.sys.SysConfigService;
|
|
|
+import com.iamberry.wechat.tools.DateTimeUtil;
|
|
|
+import com.iamberry.wechat.tools.ResponseJson;
|
|
|
+import com.iamberry.wechat.tools.payUtil.StringUtil;
|
|
|
+import com.iamberry.zk.SpringContextHolder;
|
|
|
+import com.jd.open.api.sdk.DefaultJdClient;
|
|
|
+import com.jd.open.api.sdk.internal.util.HttpUtil;
|
|
|
+import com.taobao.api.ApiException;
|
|
|
+import com.taobao.api.DefaultTaobaoClient;
|
|
|
+import com.taobao.api.TaobaoClient;
|
|
|
+import com.taobao.api.request.TopAuthTokenCreateRequest;
|
|
|
+import com.taobao.api.response.TopAuthTokenCreateResponse;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author root
|
|
|
+ */
|
|
|
+@Controller
|
|
|
+@RequestMapping("/jingdong")
|
|
|
+public class AdminJingdongAuthController {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+
|
|
|
+ public static final Map<String, String> HREF_CACHE = new ConcurrentHashMap<>(10);
|
|
|
+
|
|
|
+// @RequestMapping("/tokenPage")
|
|
|
+// public ModelAndView tokenPage() {
|
|
|
+// return new ModelAndView("taobao/token").addObject("data", getToken());
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static Map<String, Object> getToken(SysConfig sysConfig) {
|
|
|
+ if (sysConfig == null || StringUtil.isEmpty(sysConfig.getConfigParameter()) || StringUtil.isEmpty(sysConfig.getConfigRemarks())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// String taobaoSession = RedisUtils.get("JINGDONG_SESSION");
|
|
|
+// // 判断数据
|
|
|
+// if (taobaoSession == null) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// JSONObject obj = JSONObject.parseObject(taobaoSession);
|
|
|
+ // 校验时间
|
|
|
+// Date time = obj.getDate("time");
|
|
|
+ Date time = DateTimeUtil.parse(sysConfig.getConfigRemarks());
|
|
|
+ if (time.getTime() <= System.currentTimeMillis()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 组装
|
|
|
+ Map<String, Object> res = new HashMap<>();
|
|
|
+ res.put("token", sysConfig.getConfigParameter());
|
|
|
+ res.put("time", DateTimeUtil.parse(sysConfig.getConfigRemarks()));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @RequestMapping("/check")
|
|
|
+ public ResponseJson checkToken() {
|
|
|
+ SysConfig sysConfig = sysConfigService.getSysConfigAll(27);
|
|
|
+ Map<String, Object> token = getToken(sysConfig);
|
|
|
+ if (token == null) {
|
|
|
+ return ResponseJson.getFAILURE();
|
|
|
+ }
|
|
|
+ Date time = (Date) token.get("time");
|
|
|
+ if (time.getTime() <= System.currentTimeMillis()) {
|
|
|
+ return ResponseJson.getFAILURE();
|
|
|
+ }
|
|
|
+ return ResponseJson.getSUCCESS().addResponseKeyValue("token", token);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @RequestMapping("/invalid")
|
|
|
+ public ResponseJson invalid() {
|
|
|
+// RedisUtils.del("JINGDONG_SESSION");
|
|
|
+ SysConfig sysConfig = new SysConfig();
|
|
|
+ sysConfig.setConfigId(27);
|
|
|
+ sysConfig.setConfigParameter("");
|
|
|
+ sysConfig.setConfigRemarks("");
|
|
|
+ sysConfigService.updateSysConfig(sysConfig);
|
|
|
+ return ResponseJson.getSUCCESS();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @RequestMapping("/token")
|
|
|
+ public ModelAndView token(
|
|
|
+ @RequestParam("code") String code,
|
|
|
+ @RequestParam("state") String state,
|
|
|
+ HttpServletRequest request) {
|
|
|
+
|
|
|
+ String authUrl = TaobaoConfig.JINGDONG_URL
|
|
|
+ .replaceAll("APP_KEY", TaobaoConfig.jd_appkey)
|
|
|
+ .replaceAll("APP_SECRET", TaobaoConfig.jd_secret)
|
|
|
+ .replaceAll("CODE", code);
|
|
|
+ try {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("app_key", TaobaoConfig.jd_appkey);
|
|
|
+ params.put("app_secret", TaobaoConfig.jd_secret);
|
|
|
+ params.put("grant_type", "authorization_code");
|
|
|
+ params.put("code", code);
|
|
|
+// String s = HttpUtil.doPost(TaobaoConfig.JINGDONG_URL, params, 6000, 6000);
|
|
|
+ String s = HttpUtility.httpsGet(authUrl);
|
|
|
+ System.out.println("获取到京东结果:" + s);
|
|
|
+ JSONObject result = JSONObject.parseObject(s);
|
|
|
+ System.out.println(result);
|
|
|
+ String accessToken = result.getString("access_token");
|
|
|
+ Integer timeout = result.getInteger("expires_in");//有效期, 单位秒
|
|
|
+ // 缓存数据
|
|
|
+// Map<String, Object> data = new HashMap<>(2);
|
|
|
+// data.put("token", accessToken);
|
|
|
+// data.put("time", DateTimeUtil.addMinute(new Date(), (timeout/60)-30));
|
|
|
+// RedisUtils.put("JINGDONG_SESSION", JSONObject.toJSONString(data));
|
|
|
+ SysConfig sysConfig = new SysConfig();
|
|
|
+ sysConfig.setConfigId(27);
|
|
|
+ sysConfig.setConfigParameter(accessToken);
|
|
|
+ sysConfig.setConfigRemarks(DateTimeUtil.format(DateTimeUtil.addMinute(new Date(), (timeout/60)-30)));
|
|
|
+ sysConfigService.updateSysConfig(sysConfig);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 跳转地址
|
|
|
+ String url = request.getContextPath() + "/admin/sys/_index";
|
|
|
+ String href = HREF_CACHE.get(state);
|
|
|
+ if (href != null) {
|
|
|
+ url = href;
|
|
|
+ HREF_CACHE.remove(state);
|
|
|
+ }
|
|
|
+ return new ModelAndView("taobao/redirect").addObject("url", url);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping("/auth")
|
|
|
+ public ModelAndView auth(@RequestParam("url") String href) {
|
|
|
+ // 存储跳转前的URL
|
|
|
+ int state = new Random(1_000_000).nextInt();
|
|
|
+ HREF_CACHE.put(state + "", href);
|
|
|
+ // 跳转 http://liurui.gz2vip.91tunnel.com/watero/jingdong/token
|
|
|
+// String url = "https://open-oauth.jd.com/oauth2/to_login?response_type=code&client_id=" + TaobaoConfig.jd_appkey + "&redirect_uri=https://rst.iamberry.com/jingdong/token&state=" + state + "&view=web";
|
|
|
+ String url = "https://open-oauth.jd.com/oauth2/to_login?app_key=" + TaobaoConfig.jd_appkey + "&response_type=code&redirect_uri=http://liurui.gz2vip.91tunnel.com/watero/jingdong/token&state=" + state + "&scope=snsapi_base";
|
|
|
+ return new ModelAndView("redirect:" + url);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|