多公众号实现生成二维码扫描进行微信与数据库信息绑定
思路:token
解决方案:复写框架底层实现方法,增加token参数(详情参考总纲)
复写目标,底层源码QrcodeApi.java,复写后代码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.kudesoft.duo.util;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;
import java.util.HashMap;
import java.util.Map;
public class QrcodeApi {
private static String apiUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
private static String showQrcodeUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";
public QrcodeApi() {
}
public static ApiResult create(String jsonStr,String token) {
String jsonResult = HttpUtils.post(apiUrl + token, jsonStr);
return new ApiResult(jsonResult);
}
public static ApiResult createTemporary(int expireSeconds, String sceneId,String token) {
Map<String, Object> params = new HashMap();
params.put("expire_seconds", expireSeconds);
params.put("action_name", "QR_STR_SCENE");
Map<String, Object> actionInfo = new HashMap();
Map<String, Object> scene = new HashMap();
scene.put("scene_str", sceneId);
actionInfo.put("scene", scene);
params.put("action_info", actionInfo);
return create(JsonUtils.toJson(params),token);
}
public static ApiResult createTemporary(int expireSeconds, int sceneId,String token) {
Map<String, Object> params = new HashMap();
params.put("expire_seconds", expireSeconds);
params.put("action_name", "QR_SCENE");
Map<String, Object> actionInfo = new HashMap();
Map<String, Object> scene = new HashMap();
scene.put("scene_id", sceneId);
actionInfo.put("scene", scene);
params.put("action_info", actionInfo);
return create(JsonUtils.toJson(params),token);
}
public static ApiResult createPermanent(int sceneId,String token) {
Map<String, Object> params = new HashMap();
params.put("action_name", "QR_LIMIT_SCENE");
Map<String, Object> actionInfo = new HashMap();
Map<String, Object> scene = new HashMap();
scene.put("scene_id", sceneId);
actionInfo.put("scene", scene);
params.put("action_info", actionInfo);
return create(JsonUtils.toJson(params),token);
}
public static ApiResult createPermanent(String sceneStr,String token) {
Map<String, Object> params = new HashMap();
params.put("action_name", "QR_LIMIT_STR_SCENE");
Map<String, Object> actionInfo = new HashMap();
Map<String, Object> scene = new HashMap();
scene.put("scene_str", sceneStr);
actionInfo.put("scene", scene);
params.put("action_info", actionInfo);
return create(JsonUtils.toJson(params),token);
}
public static String getShowQrcodeUrl(String ticket) {
return showQrcodeUrl + ticket;
}
}
增加JsonUtil工具类进行对象与字符串之间互相转换
package cn.kudesoft.duo.util;
import com.jfinal.weixin.sdk.api.MenuApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
@Slf4j
public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
static{
//对象的所有字段全部列入
objectMapper.setSerializationInclusion(Inclusion.ALWAYS);
//取消默认转换timestamps形式
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false);
//忽略空Bean转json的错误
objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false);
//所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
objectMapper.setDateFormat(new SimpleDateFormat(DateTimeUtil.STANDARD_FORMAT));
//忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
}
public static String file2String(String filepath) throws IOException {
URL url = JsonUtil.class.getClassLoader().getResource(filepath);
System.out.println(url);
File file = new File(url.getPath());
InputStream in=new FileInputStream(file);//实例化FileInputStream
int fileLength=(int)file.length();
byte b[]=new byte[fileLength];
int length=in.read(b);
in.close();
String s = new String(b);
return s;
}
/**
* 对json字符串进行格式化
* "{\"name\": \"张 三 \", \"remark\": \",: {}[]啦啦啦\", \"data\": [\"1\", \"2\"]}"
* @param content 要格式化的json字符串
* @param indent 是否进行缩进(false时压缩为一行)
* @param colonWithSpace key冒号后是否加入空格
* @return
*/
public static String toFormat(String content, boolean indent, boolean colonWithSpace) {
if (content == null) return null;
StringBuilder sb = new StringBuilder();
int count = 0;
boolean inString = false;
String tab = "\t";
for (int i = 0; i < content.length(); i ++) {
char ch = content.charAt(i);
switch (ch) {
case '{':
case '[':
sb.append(ch);
if (!inString) {
if (indent) {
sb.append("\n");
count ++;
for (int j = 0; j < count; j ++) {
sb.append(tab);
}
}
}
break;
case '\uFEFF': //非法字符
if (inString) sb.append(ch);
break;
case '}':
case ']':
if (!inString) {
if (indent) {
count --;
sb.append("\n");
for (int j = 0; j < count; j ++) {
sb.append(tab);
}
}
sb.append(ch);
} else {
sb.append(ch);
}
break;
case ',':
sb.append(ch);
if (!inString) {
if (indent) {
sb.append("\n");
for (int j = 0; j < count; j ++) {
sb.append(tab);
}
} else {
if (colonWithSpace) {
sb.append(' ');
}
}
}
break;
case ':':
sb.append(ch);
if (!inString) {
if (colonWithSpace) { //key名称冒号后加空格
sb.append(' ');
}
}
break;
case ' ':
case '\n':
case '\t':
if (inString) {
sb.append(ch);
}
break;
case '"':
if (i > 0 && content.charAt(i - 1) != '\\') {
inString = !inString;
}
sb.append(ch);
break;
default:
sb.append(ch);
break;
}
}
return sb.toString();
}
public static <T> String obj2String(T obj){
if(obj == null){
return null;
}
try {
return obj instanceof String ? (String)obj : objectMapper.writeValueAsString(obj);
} catch (Exception e) {
log.warn("Parse Object to String error",e);
return null;
}
}
public static <T> String obj2StringPretty(T obj){
if(obj == null){
return null;
}
try {
return obj instanceof String ? (String)obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (Exception e) {
log.warn("Parse Object to String error",e);
return null;
}
}
public static <T> T string2Obj(String str,Class<T> clazz){
if(StringUtils.isEmpty(str) || clazz == null){
return null;
}
try {
return clazz.equals(String.class)? (T)str : objectMapper.readValue(str,clazz);
} catch (Exception e) {
log.warn("Parse String to Object error",e);
return null;
}
}
public static <T> T string2Obj(String str, TypeReference<T> typeReference){
if(StringUtils.isEmpty(str) || typeReference == null){
return null;
}
try {
return (T)(typeReference.getType().equals(String.class)? str : objectMapper.readValue(str,typeReference));
} catch (Exception e) {
log.warn("Parse String to Object error",e);
return null;
}
}
public static <T> T string2Obj(String str,Class<?> collectionClass,Class<?>... elementClasses){
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
try {
return objectMapper.readValue(str,javaType);
} catch (Exception e) {
log.warn("Parse String to Object error",e);
return null;
}
}
}
增加实体WXTicket方便获取ticket(根据项目需要自行设计)
package cn.kudesoft.duo.wx;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Map;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class WXTicket {
private Integer expire_seconds = 2592000;
private String action_name = "QR_STR_SCENE";
private Map<String,Map<String,String>> action_info;
}
WXUtil工具类增加方法从微信换取ticket
public static String getTicket(String id,String token){
WXTicket wxTicket = new WXTicket();
Map<String, Map<String,String>> info = new HashMap<>();
Map<String,String> scene = new HashMap<>();
info.put("scene",scene);
scene.put("scene_str",id);
wxTicket.setAction_info(info);
String sendEntity = JsonUtil.obj2String(wxTicket);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> entity = new HttpEntity<String>(sendEntity,headers);
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token;
GetTicket getTicket = restTemplate.exchange(url, HttpMethod.POST, entity,GetTicket.class).getBody();
return getTicket.getTicket();
}
复写前调用方法参考框架文档:https://gitee.com/jfinal/jfinal-weixin/wikis/pages?title=Home
复写后调用方法(根据实际需要自行调整带参二维码传递参数,因需要扫描二维码绑定信息,故多公众号下最好以临时二维码传递id为字符串,因为微信加密后openid不一致,详情请查看微信官方文档):
@Test
public void sdf(){
WXConfig wxConfig = wxConfigDao.getOne(3);
String token = WXUtil.getAccessToken(wxConfig);
ApiResult apiResult = QrcodeApi.createTemporary(60*100,"adw23_451854",token);
GetWxCode getWxCode = JsonUtil.string2Obj(apiResult.getJson(), GetWxCode.class);
//微信二维码地址,可浏览器地址栏直接打开
String string = QrcodeApi.getShowQrcodeUrl(getWxCode.getTicket());
System.out.println(string);
}