|
@@ -2,19 +2,23 @@ package com.usky.dxtop.controller.web;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.usky.dxtop.common.core.redis.RedisCache;
|
|
|
import com.usky.dxtop.common.exception.CustomException;
|
|
|
-import com.usky.dxtop.common.utils.DateUtils;
|
|
|
+import com.usky.dxtop.common.utils.Assert;
|
|
|
import com.usky.dxtop.common.utils.http.HttpUtils;
|
|
|
-import com.usky.dxtop.common.utils.sign.Md5Utils;
|
|
|
+import com.usky.dxtop.common.utils.sign.Sha1;
|
|
|
import com.usky.dxtop.common.utils.uuid.UUID;
|
|
|
import com.usky.dxtop.service.api.TopApiConfiger;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
import java.util.TreeMap;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author yq
|
|
@@ -25,8 +29,21 @@ import java.util.TreeMap;
|
|
|
public class WxController {
|
|
|
|
|
|
private static final String WX_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+
|
|
|
+ private static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
+
|
|
|
+ private static final String GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
|
|
|
+
|
|
|
+ private static final String WX_TOKEN_KEY = "wxTokenKey";
|
|
|
+
|
|
|
+ private static final String WX_TICKET_KEY = "wxTicketKey";
|
|
|
+
|
|
|
+
|
|
|
@Autowired
|
|
|
private TopApiConfiger topApiConfiger;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
@GetMapping("/wxLogin")
|
|
|
public String wxLogin(@RequestParam String jsCode){
|
|
|
TreeMap<String, String> treeMap = new TreeMap<>();
|
|
@@ -47,15 +64,62 @@ public class WxController {
|
|
|
}
|
|
|
|
|
|
@GetMapping("/wxPayParam")
|
|
|
- public String wxPayParam(@RequestParam String payInfo){
|
|
|
+ public String wxPayParam(@RequestParam String url){
|
|
|
TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
treeMap.put("appid", topApiConfiger.wxAppId);
|
|
|
- treeMap.put("timeStamp", DateUtils.dateTimeNow());
|
|
|
- treeMap.put("nonceStr", UUID.randomUUID().toString(true));
|
|
|
- treeMap.put("package", String.format("prepay_id=%s",payInfo));
|
|
|
- treeMap.put("signType", "MD5");
|
|
|
- String format = String.format("%s&key=%s", topApiConfiger.joinParam(treeMap), topApiConfiger.secret);
|
|
|
- treeMap.put("paySign", Md5Utils.hash(format).toUpperCase());
|
|
|
+ String currentTimeMillis = String.valueOf(System.currentTimeMillis());
|
|
|
+ treeMap.put("timeStamp",currentTimeMillis);
|
|
|
+ String noncestr = UUID.randomUUID().toString(true);
|
|
|
+ treeMap.put("nonceStr", noncestr);
|
|
|
+
|
|
|
+ String ticket = getTicket(getToken());
|
|
|
+ //验签参数
|
|
|
+ TreeMap<String, String> signMap = new TreeMap<>();
|
|
|
+ signMap.put("jsapi_ticket",ticket);
|
|
|
+ signMap.put("noncestr",noncestr);
|
|
|
+ signMap.put("timestamp",currentTimeMillis);
|
|
|
+ signMap.put("url",url);
|
|
|
+ String joinParam = topApiConfiger.joinParam(signMap);
|
|
|
+ treeMap.put("signature", Sha1.getSha1(joinParam));
|
|
|
return JSONObject.toJSONString(treeMap);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getToken(){
|
|
|
+ Object cacheObject = redisCache.getCacheObject(WX_TOKEN_KEY);
|
|
|
+ if (!Objects.isNull(cacheObject)){
|
|
|
+ return cacheObject.toString();
|
|
|
+ }
|
|
|
+ TreeMap<String,String> tokenMap = new TreeMap<>();
|
|
|
+ tokenMap.put("grant_type","client_credential");
|
|
|
+ tokenMap.put("appid",topApiConfiger.getWxAppId());
|
|
|
+ tokenMap.put("secret",topApiConfiger.getWxAppSecret());
|
|
|
+ String token = HttpUtils.sendGet(GET_TOKEN_URL, topApiConfiger.joinParam(tokenMap), null);
|
|
|
+ Assert.check(StringUtils.isNotBlank(token),"获取微信token异常");
|
|
|
+ JSONObject obj= JSON.parseObject(token);
|
|
|
+ Assert.check(Objects.isNull(obj.get("errcode")),obj.get("errmsg").toString());
|
|
|
+ String s = obj.get("access_token").toString();
|
|
|
+ redisCache.setCacheObject(WX_TOKEN_KEY, s, 7200, TimeUnit.MINUTES);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTicket(String token){
|
|
|
+ Object cacheObject = redisCache.getCacheObject(WX_TICKET_KEY);
|
|
|
+ if (!Objects.isNull(cacheObject)){
|
|
|
+ return cacheObject.toString();
|
|
|
+ }
|
|
|
+ TreeMap<String,String> ticketMap = new TreeMap<>();
|
|
|
+ ticketMap.put("access_token",token);
|
|
|
+ ticketMap.put("type","jsapi");
|
|
|
+ String ticket = HttpUtils.sendGet(GET_TICKET_URL, topApiConfiger.joinParam(ticketMap), null);
|
|
|
+ Assert.check(StringUtils.isNotBlank(ticket),"获取时间异常");
|
|
|
+ JSONObject obj= JSON.parseObject(ticket);
|
|
|
+ Assert.check(Objects.isNull(obj.get("errcode")),obj.get("errmsg").toString());
|
|
|
+ String s = obj.get("ticket").toString();
|
|
|
+ redisCache.setCacheObject(WX_TICKET_KEY, s, 7200, TimeUnit.MINUTES);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
}
|