|
@@ -0,0 +1,192 @@
|
|
|
+package com.usky.dxtop.service.api;
|
|
|
+
|
|
|
+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 lombok.Data;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.TreeMap;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yq
|
|
|
+ * @date 2022/2/25 15:10
|
|
|
+ */
|
|
|
+@Data
|
|
|
+@Component
|
|
|
+public class WxApi {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token的url
|
|
|
+ */
|
|
|
+ private static final String GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送消息
|
|
|
+ */
|
|
|
+ private static final String SEND_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/template/send";
|
|
|
+ /**
|
|
|
+ * 登录
|
|
|
+ */
|
|
|
+ private static final String WX_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+ /**
|
|
|
+ * 获取ticket
|
|
|
+ */
|
|
|
+ private static final String GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
|
|
|
+
|
|
|
+
|
|
|
+ public static final String APP_ID = "wxd64360a4b8c50006";
|
|
|
+
|
|
|
+ public static final String SECRET = "b758e45c89162542610509dafd9db7c3";
|
|
|
+ /**
|
|
|
+ * 模版id
|
|
|
+ */
|
|
|
+ private static final String TEMPLATE_ID = "Eps4guetElB_6uzu-AYvvGM4xc9LwO7UnEwxNwn0ZFE";
|
|
|
+ /**
|
|
|
+ * 公众号的key
|
|
|
+ */
|
|
|
+ public static final String ACCESS_TOKEN = "access_token";
|
|
|
+
|
|
|
+ private static final String WX_TICKET_KEY = "wxTicketKey";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 小程序登录
|
|
|
+ * @param appId
|
|
|
+ * @param wxAppSecret
|
|
|
+ * @param jsCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String wxLogin(String appId,String wxAppSecret,String jsCode){
|
|
|
+ TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
+ treeMap.put("appid", appId);
|
|
|
+ treeMap.put("secret", wxAppSecret);
|
|
|
+ treeMap.put("js_code", jsCode);
|
|
|
+ treeMap.put("grant_type", "authorization_code");
|
|
|
+ String result = HttpUtils.sendGet(WX_LOGIN_URL, joinParam(treeMap),null);
|
|
|
+ JSONObject obj= JSON.parseObject(result);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String sendMessageApi(String openId,Object data,String token){
|
|
|
+ String url = String.format("%s%s%s", SEND_MESSAGE, "?access_token=", token);
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("touser",openId);
|
|
|
+ map.put("template_id",TEMPLATE_ID);
|
|
|
+ map.put("data",data);
|
|
|
+ String result = HttpUtils.sendPost(url, JSONObject.toJSONString(map), null);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ return jsonObject.toJSONString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ * @param appId
|
|
|
+ * @param appSecret
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getToken(String appId,String appSecret){
|
|
|
+ Object cacheObject = redisCache.getCacheObject(appId+ACCESS_TOKEN);
|
|
|
+ if (!Objects.isNull(cacheObject)){
|
|
|
+ return cacheObject.toString();
|
|
|
+ }
|
|
|
+ TreeMap<String,String> tokenMap = new TreeMap<>();
|
|
|
+ tokenMap.put("grant_type","client_credential");
|
|
|
+ tokenMap.put("appid",appId);
|
|
|
+ tokenMap.put("secret",appSecret);
|
|
|
+ String token = HttpUtils.sendGet(GET_TOKEN, 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());
|
|
|
+ }
|
|
|
+ String s = obj.get("access_token").toString();
|
|
|
+ redisCache.setCacheObject(appId+ACCESS_TOKEN, s, 7200, TimeUnit.MINUTES);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取ticket
|
|
|
+ * @param token
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ 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, 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取支付参数
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public TreeMap<String, String> getPayParam(String wxAppId,String secret,String url){
|
|
|
+ TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
+ treeMap.put("appid", 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(wxAppId,secret));
|
|
|
+ //验签参数
|
|
|
+ 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 = joinParam(signMap);
|
|
|
+ treeMap.put("signature", Sha1.getSha1(joinParam));
|
|
|
+ return treeMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static String joinParam(TreeMap<String, String> params){
|
|
|
+ StringBuilder buf = new StringBuilder();
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ if (!com.usky.dxtop.common.utils.StringUtils.isBlank(entry.getValue()) && !"sign".equals(entry.getKey())) {
|
|
|
+ buf.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String changeBuf = buf.toString();
|
|
|
+ return changeBuf.substring(0, changeBuf.length() - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|