请记住,你是否拥有完整的微信公众号权限!
请记住,你是否拥有完整的微信公众号权限!
请记住,你是否拥有完整的微信公众号权限!
这是一个血淋淋的教训,自己开发完了。然后数据不及预期。从周五周六晚上排查 一共10个小时。
票据接入方案JAVA版本
package com.murphyyi.wechat.manger;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.murphyyi.wechat.bean.vo.WechatTicketVO;
import com.murphyyi.wechat.bean.vo.WechatTokenVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.murphyyi.wechat.utils.RandomString.SHA1;
import static com.murphyyi.wechat.utils.RandomString.getRandomStringByLength;
@Service
public class WechatConfig {
@Resource
RedisTemplate<Object, Object> redisTemplate;
public Map<String, Object> jsAPI(String appId,String appSecret,String url){
String noncestr = getRandomStringByLength(32);
String jsapiTicket = null;
try {
jsapiTicket = getJsapiTicket(appId,appSecret);
} catch (InterruptedException e) {
e.printStackTrace();
}
String timestamp = String.valueOf(System.currentTimeMillis()).substring(0,10);
String str = "jsapi_ticket="+jsapiTicket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+url;
Map<String, Object> map = new HashMap<>();
map.put("appId",appId);
map.put("timestamp",timestamp);
map.put("nonceStr",noncestr);
map.put("signature",SHA1(str));
map.put("jsticket",jsapiTicket);
map.put("url",url);
return map;
}
private String getJsapiTicket(String appId,String appSecret) throws InterruptedException {
Object obj =redisTemplate.opsForValue().get(appId+appSecret+"TICKET");
String ticket = null;
if(obj!=null){
ticket = obj.toString();
}
while (null == ticket){
Map<String, Object> data = new HashMap<>();
data.put("access_token",getAccessToken(appId,appSecret));
data.put("type","jsapi");
String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket",data);
WechatTicketVO wechatTicketVO = JSON.parseObject(result, WechatTicketVO.class);
if(wechatTicketVO.getTicket()!=null){
redisTemplate.opsForValue().set(appId+appSecret+"TICKET", wechatTicketVO.getTicket(), 7200, TimeUnit.SECONDS);
break;
}
Thread.sleep(3000);
}
return ticket;
}
private String getAccessToken(String appId,String appSecret) throws InterruptedException {
Object obj =redisTemplate.opsForValue().get(appId+appSecret+"TOKEN");
String token = null;
if(obj!=null){
token = obj.toString();
}
while (token==null){
Map<String, Object> data = new HashMap<>();
data.put("grant_type","client_credential");
data.put("appid",appId);
data.put("secret",appSecret);
String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", data);
WechatTokenVO wechatTokenVO = JSON.parseObject(result, WechatTokenVO.class);
if(wechatTokenVO.getAccess_token()!=null) {
token = wechatTokenVO.getAccess_token();
redisTemplate.opsForValue().set(appId + appSecret + "TOKEN", wechatTokenVO.getAccess_token(), 7200, TimeUnit.SECONDS);
break;
}
Thread.sleep(3000);
}
return token;
}
}
SHA1加密
package com.murphyyi.wechat.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
public class RandomString {
//可以指定字符串的某个位置是什么范围的值
public static String getRandomStringByLength(int length){
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(3);
long result=0;
switch(number){
case 0:
result=Math.round(Math.random()*25+65);
sb.append(String.valueOf((char)result));
break;
case 1:
result=Math.round(Math.random()*25+97);
sb.append(String.valueOf((char)result));
break;
case 2:
sb.append(String.valueOf(new Random().nextInt(10)));
break;
}
}
return sb.toString();
}
public static String SHA1(String decript) {
try {
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (byte b : messageDigest) {
String shaHex = Integer.toHexString(b & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
}
VO
package com.murphyyi.wechat.bean.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WechatTicketVO {
Integer errcode;
String errmsg;
String ticket;
Integer expirs_in;
}
package com.murphyyi.wechat.bean.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WechatTokenVO {
Integer errcode;
String errmsg;
String access_token;
Integer expirs_in;
}
Golang方案待完成