package com.qiwuai.garden.common.utils; import com.alibaba.fastjson.JSONObject; import com.qiwuai.garden.dto.request.PhraseRequest; import com.qiwuai.garden.dto.response.InviteResponse; import com.qiwuai.garden.dto.response.PhraseResponse; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.util.List; public class HttpUtil { static Logger log = LoggerFactory.getLogger(HttpUtil.class); //动态词组更新秘钥 public static String appSecret = YamlUtil.getValueByKey("appSecret"); //动态词组更新秘钥 public static String updatePhraseUrl = YamlUtil.getValueByKey("updatePhraseUrl"); //分享关系地址 public static String shareRelationUrl = YamlUtil.getValueByKey("shareRelationUrl"); //分享链接通道 public static String shareChannelId = YamlUtil.getValueByKey("shareChannelId"); //分享链接秘钥 public static String shareAppSecret = YamlUtil.getValueByKey("shareAppSecret"); //更新动态词组 public static void doPut(String phraseName, String phraseContent) { RestTemplate template = new RestTemplate(); //获取时间戳 long timestamp = System.currentTimeMillis(); //签名 String authorization = DigestUtils.md5Hex(appSecret + ":" + timestamp); //设置请求体 PhraseRequest phraseRequest = new PhraseRequest(); phraseRequest.setWorkName("追梦花园"); phraseRequest.setPhraseName(phraseName); phraseRequest.setPhraseContent(phraseContent); JSONObject object = new JSONObject(); object.put("workName", "追梦花园"); object.put("phraseName", phraseName); object.put("phraseContent", phraseContent); //设置请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json;charset=utf-8"); httpHeaders.set("Timestamp", String.valueOf(timestamp)); httpHeaders.set("Authorization", authorization); HttpEntity request = new HttpEntity<>(object, httpHeaders); ResponseEntity forObject = template.exchange(updatePhraseUrl, HttpMethod.PUT, request, PhraseResponse.class); log.info(phraseName + "的动态词组更新结果:" + forObject.getBody()); } //更新"园丁也疯狂"的动态词组 public static void doPutCrazyGardener(String phraseName, String phraseContent) { RestTemplate template = new RestTemplate(); //获取时间戳 long timestamp = System.currentTimeMillis(); //签名 String authorization = DigestUtils.md5Hex(appSecret + ":" + timestamp); //设置请求体 PhraseRequest phraseRequest = new PhraseRequest(); phraseRequest.setWorkName("园丁也疯狂"); phraseRequest.setPhraseName(phraseName); phraseRequest.setPhraseContent(phraseContent); JSONObject object = new JSONObject(); object.put("workName", "园丁也疯狂"); object.put("phraseName", phraseName); object.put("phraseContent", phraseContent); //设置请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json;charset=utf-8"); httpHeaders.set("Timestamp", String.valueOf(timestamp)); httpHeaders.set("Authorization", authorization); HttpEntity request = new HttpEntity<>(object, httpHeaders); ResponseEntity forObject = template.exchange(updatePhraseUrl, HttpMethod.PUT, request, PhraseResponse.class); log.info("《园丁也疯狂》" + phraseName + "的动态词组更新结果:" + forObject.getBody()); } //更新"花仙爱上我"的动态词组 public static void doPutAngelLoveMe(String phraseName, String phraseContent) { RestTemplate template = new RestTemplate(); //获取时间戳 long timestamp = System.currentTimeMillis(); //签名 String authorization = DigestUtils.md5Hex(appSecret + ":" + timestamp); //设置请求体 PhraseRequest phraseRequest = new PhraseRequest(); phraseRequest.setWorkName("花仙爱上我"); phraseRequest.setPhraseName(phraseName); phraseRequest.setPhraseContent(phraseContent); JSONObject object = new JSONObject(); object.put("workName", "花仙爱上我"); object.put("phraseName", phraseName); object.put("phraseContent", phraseContent); //设置请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json;charset=utf-8"); httpHeaders.set("Timestamp", String.valueOf(timestamp)); httpHeaders.set("Authorization", authorization); HttpEntity request = new HttpEntity<>(object, httpHeaders); ResponseEntity forObject = template.exchange(updatePhraseUrl, HttpMethod.PUT, request, PhraseResponse.class); log.info("《花仙爱上我》" + phraseName + "的动态词组更新结果:" + forObject.getBody()); } //获取app用户关联id public static List getInviteUid(String uid) { RestTemplate template = new RestTemplate(); //获取时间戳 long timestamp = System.currentTimeMillis(); //签名 String authorization = DigestUtils.md5Hex(shareAppSecret + ":" + timestamp); //获取用户关联ID链接 String relativeUrl = shareRelationUrl + "/invite/relative/user"; JSONObject object = new JSONObject(); object.put("invitedUid", uid); //设置请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json;charset=utf-8"); httpHeaders.set("Timestamp", String.valueOf(timestamp)); httpHeaders.set("Authorization", authorization); httpHeaders.set("App-Channel-Id", shareChannelId); HttpEntity request = new HttpEntity<>(object, httpHeaders); ResponseEntity forObject = template.exchange(relativeUrl, HttpMethod.POST, request, InviteResponse.class); log.info(uid + "用户分享关联id:" + forObject.getBody()); if (StringUtils.isEmpty(forObject.getBody())) { return null; } if (forObject.getBody().getCode() == 0) { return null; } if (StringUtils.isEmpty(forObject.getBody().getData())) { return null; } List inviteUidList = JSONObject.parseArray(forObject.getBody().getData()); if (inviteUidList.size() == 0) { return null; } // JSON转换 return inviteUidList; } //通过用户昵称获取用户uid public static String getUidByName(String uname) { RestTemplate template = new RestTemplate(); String uidUrl = "http://hw-gz54.heyqiwu.cn:8089/developmentAnalysis/uidByName?uName=" + uname; //获取时间戳 long timestamp = System.currentTimeMillis(); //签名 String authorization = DigestUtils.md5Hex(uidUrl + ":" + timestamp); //设置请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json;charset=utf-8"); httpHeaders.set("Timestamp", String.valueOf(timestamp)); httpHeaders.set("Authorization", authorization); HttpEntity request = new HttpEntity<>(httpHeaders); ResponseEntity forObject = template.exchange(uidUrl, HttpMethod.GET, request, InviteResponse.class); log.info(uname + "用户对应的uid:" + forObject.getBody().getData()); // JSON转换 return forObject.getBody().getData(); } }