|
@@ -1,24 +1,16 @@
|
|
package com.usky.dxtop.controller.web;
|
|
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.Assert;
|
|
|
|
-import com.usky.dxtop.common.utils.http.HttpUtils;
|
|
|
|
-import com.usky.dxtop.common.utils.sign.Sha1;
|
|
|
|
-import com.usky.dxtop.common.utils.uuid.UUID;
|
|
|
|
import com.usky.dxtop.service.api.TopApiConfiger;
|
|
import com.usky.dxtop.service.api.TopApiConfiger;
|
|
|
|
+import com.usky.dxtop.service.api.WxApi;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
import java.util.TreeMap;
|
|
import java.util.TreeMap;
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author yq
|
|
* @author yq
|
|
@@ -28,102 +20,44 @@ import java.util.concurrent.TimeUnit;
|
|
@RestController
|
|
@RestController
|
|
public class WxController {
|
|
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
|
|
@Autowired
|
|
private TopApiConfiger topApiConfiger;
|
|
private TopApiConfiger topApiConfiger;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private RedisCache redisCache;
|
|
|
|
|
|
+ private WxApi wxApi;
|
|
@GetMapping("/wxLogin")
|
|
@GetMapping("/wxLogin")
|
|
public String wxLogin(@RequestParam String jsCode){
|
|
public String wxLogin(@RequestParam String jsCode){
|
|
- TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
|
- treeMap.put("appid", topApiConfiger.wxAppId);
|
|
|
|
- treeMap.put("secret", topApiConfiger.wxAppSecret);
|
|
|
|
- treeMap.put("js_code", jsCode);
|
|
|
|
- treeMap.put("grant_type", "authorization_code");
|
|
|
|
- String result = HttpUtils.sendGet(WX_LOGIN_URL, topApiConfiger.joinParam(treeMap),null);
|
|
|
|
- JSONObject obj= JSON.parseObject(result);
|
|
|
|
- log.info("获取到的相应"+obj);
|
|
|
|
- String openId = "";
|
|
|
|
- if (null == obj.get("errcode")){
|
|
|
|
- openId = obj.get("openid").toString();
|
|
|
|
- }else {
|
|
|
|
- throw new CustomException(String.format("登录异常:code:%s,message:%s",obj.get("errcode"),obj.get("errmsg")));
|
|
|
|
- }
|
|
|
|
- return openId;
|
|
|
|
|
|
+ return wxApi.wxLogin(topApiConfiger.getWxAppId(),topApiConfiger.getWxAppSecret(),jsCode);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 拉起支付
|
|
|
|
+ * @param url
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
@GetMapping("/wxAccToken")
|
|
@GetMapping("/wxAccToken")
|
|
public TreeMap<String, String> getAccToken(@RequestParam String url){
|
|
public TreeMap<String, String> getAccToken(@RequestParam String url){
|
|
- TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
|
- treeMap.put("appid", topApiConfiger.wxAppId);
|
|
|
|
- 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 treeMap;
|
|
|
|
|
|
+ return wxApi.getPayParam(topApiConfiger.getWxAppId(),topApiConfiger.getWxAppSecret(),url);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取token
|
|
* 获取token
|
|
* @return
|
|
* @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);
|
|
|
|
- if (null != obj.get("errcode")){
|
|
|
|
- throw new CustomException(obj.get("errmsg").toString());
|
|
|
|
|
|
+ @GetMapping("/accentToken")
|
|
|
|
+ public String getToken(@RequestParam Integer type){
|
|
|
|
+ if (1 == type){
|
|
|
|
+ return wxApi.getToken(topApiConfiger.getWxAppId(),topApiConfiger.getWxAppSecret());
|
|
|
|
+ }else {
|
|
|
|
+ return wxApi.getToken(WxApi.APP_ID,WxApi.SECRET);
|
|
}
|
|
}
|
|
- 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);
|
|
|
|
- if (!"0".equals(obj.get("errcode").toString())){
|
|
|
|
- throw new CustomException(obj.get("errmsg").toString());
|
|
|
|
- }
|
|
|
|
- String s = obj.get("ticket").toString();
|
|
|
|
- redisCache.setCacheObject(WX_TICKET_KEY, s, 7200, TimeUnit.MINUTES);
|
|
|
|
- return s;
|
|
|
|
|
|
+ @GetMapping("/testt")
|
|
|
|
+ public String sendMessageTest(String openId){
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ Map<String, Object> firstMap = new HashMap<>();
|
|
|
|
+ firstMap.put("value","支付成功");
|
|
|
|
+ map.put("first",firstMap);
|
|
|
|
+ return wxApi.sendMessageApi(openId,map,wxApi.getToken(WxApi.APP_ID,WxApi.SECRET));
|
|
}
|
|
}
|
|
}
|
|
}
|