| | |
| | | import com.hx.util.OSSUtil; |
| | | import com.hx.util.SimpleTool; |
| | | import com.hx.util.StringUtils; |
| | | import com.hz.util.http.HttpHzUtil; |
| | | import com.hz.util.http.dto.HttpHzResponse; |
| | | import net.sf.json.JSONException; |
| | | import net.sf.json.JSONObject; |
| | | import org.apache.commons.codec.binary.Base64; |
| | |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.MessageFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | |
| | |
| | | private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?"; |
| | | /**发送订阅消息通知链接*/ |
| | | private static final String SEND_SUBSCRIBE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; |
| | | //生成小程序二维码地址(方形) |
| | | /**生成小程序二维码地址(方形)*/ |
| | | public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; |
| | | //生成小程序二维码地址(圆形) |
| | | /**生成小程序二维码地址(圆形)*/ |
| | | public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}"; |
| | | //生成无限二维码 |
| | | /**生成无限二维码*/ |
| | | public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; |
| | | /**获取小程序订阅个人模板列表*/ |
| | | private static final String GET_APP_TEMPLATE = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token="; |
| | | /**获取公众号模板列表*/ |
| | | private static final String GET_GZH_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="; |
| | | ////////////////////////////////////////////////// |
| | | |
| | | /** (小程序)通过code换取网页授权access_token/如果是snsapi_base模式的授权,这里就可以拿到openId了 ***/ |
| | |
| | | * @return |
| | | */ |
| | | public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, |
| | | JSONObject lineColor, boolean isHyaline) throws IOException { |
| | | JSONObject lineColor, boolean isHyaline) { |
| | | String imgUrl = null; |
| | | InputStream in = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | //创建临时文件 |
| | | File file = File.createTempFile("temp", ".jpg"); |
| | | File file = null; |
| | | try { |
| | | file = File.createTempFile("temp", ".jpg"); |
| | | //生成发送数据 |
| | | JSONObject obj = new JSONObject(); |
| | | obj.put("width", width); |
| | |
| | | conn = null; |
| | | } |
| | | }finally { |
| | | file.deleteOnExit(); |
| | | if(file != null){ |
| | | file.deleteOnExit(); |
| | | } |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | |
| | | /**生成有限二维码,返回临时文 |
| | | * @param at 微信token |
| | | * @param page 跳转链接 |
| | | * @param width 宽度 |
| | | * @param autoColor 默认false |
| | | * @param lineColor 默认null |
| | | * @param isHyaline 默认false |
| | | * @param envVersion 要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop |
| | | * @return |
| | | */ |
| | | public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, |
| | | JSONObject lineColor, boolean isHyaline,String envVersion) { |
| | | String imgUrl = null; |
| | | InputStream in = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | //创建临时文件 |
| | | File file = null; |
| | | try { |
| | | file = File.createTempFile("temp", ".jpg"); |
| | | //生成发送数据 |
| | | JSONObject obj = new JSONObject(); |
| | | obj.put("width", width); |
| | | obj.put("path", page); |
| | | obj.put("auto_color", autoColor); |
| | | obj.put("line_color", lineColor); |
| | | obj.put("is_hyaline", isHyaline); |
| | | obj.put("env_version", envVersion); |
| | | |
| | | // 创建url资源 |
| | | URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); |
| | | // 建立http连接 |
| | | conn = (HttpURLConnection) url.openConnection(); |
| | | // 设置允许输出 |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | // 设置不用缓存 |
| | | conn.setUseCaches(false); |
| | | // 设置传递方式 |
| | | conn.setRequestMethod("POST"); |
| | | // 设置维持长连接 |
| | | conn.setRequestProperty("Connection", "Keep-Alive"); |
| | | // 设置文件字符集: |
| | | conn.setRequestProperty("Charset", "UTF-8"); |
| | | // 设置文件类型: |
| | | conn.setRequestProperty("contentType", "application/json"); |
| | | // 开始连接请求 |
| | | conn.connect(); |
| | | OutputStream out = conn.getOutputStream(); |
| | | // 写入请求的字符串 |
| | | out.write((obj.toString()).getBytes()); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | // 请求返回的状态 |
| | | if (conn.getResponseCode() == 200) { |
| | | // 请求返回的数据 |
| | | in = conn.getInputStream(); |
| | | //输入到临时文件 |
| | | /*OutputStream os = new FileOutputStream(file); |
| | | int bytesRead = 0; |
| | | byte[] buffer = new byte[8192]; |
| | | while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | }*/ |
| | | FileUtils.copyInputStreamToFile(in, file); |
| | | |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | |
| | | if (in != null) { |
| | | in.close(); |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (in != null) { |
| | | try { |
| | | in.close(); |
| | | }catch (Exception ep) { |
| | | ep.printStackTrace(); |
| | | } |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }finally { |
| | | if(file != null){ |
| | | file.deleteOnExit(); |
| | | } |
| | | } |
| | | return file; |
| | | } |
| | |
| | | return obj; |
| | | } |
| | | |
| | | |
| | | |
| | | /**生成无限二维码,返回临时文 |
| | | * @param at 微信token |
| | | * @param scene 参数,只能32位,最好不要中文 |
| | | * @param page 跳转链接 |
| | | * @param width 宽度 |
| | | * @param autoColor 默认false |
| | | * @param lineColor 默认null |
| | | * @param isHyaline 默认false |
| | | * @return |
| | | */ |
| | | public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
| | | JSONObject lineColor, boolean isHyaline, String env_version) throws IOException { |
| | | String imgUrl = null; |
| | | InputStream in = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | //创建临时文件 |
| | | File file = File.createTempFile("temp", ".jpg"); |
| | | |
| | | try { |
| | | //生成发送数据 |
| | | JSONObject obj = new JSONObject(); |
| | | obj.put("scene", scene); |
| | | obj.put("width", width); |
| | | obj.put("page", page); |
| | | obj.put("auto_color", autoColor); |
| | | obj.put("line_color", lineColor); |
| | | obj.put("is_hyaline", isHyaline); |
| | | obj.put("env_version", env_version); |
| | | //体验版时,可不检查页面是否存在 |
| | | if("trial".equals(env_version)){ |
| | | obj.put("check_path", false); |
| | | } |
| | | // 创建url资源 |
| | | URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
| | | // 建立http连接 |
| | | conn = (HttpURLConnection) url.openConnection(); |
| | | // 设置允许输出 |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | // 设置不用缓存 |
| | | conn.setUseCaches(false); |
| | | // 设置传递方式 |
| | | conn.setRequestMethod("POST"); |
| | | // 设置维持长连接 |
| | | conn.setRequestProperty("Connection", "Keep-Alive"); |
| | | // 设置文件字符集: |
| | | conn.setRequestProperty("Charset", "UTF-8"); |
| | | // 设置文件类型: |
| | | conn.setRequestProperty("contentType", "application/json"); |
| | | // 开始连接请求 |
| | | conn.connect(); |
| | | OutputStream out = conn.getOutputStream(); |
| | | // 写入请求的字符串 |
| | | out.write((obj.toString()).getBytes()); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | // 请求返回的状态 |
| | | if (conn.getResponseCode() == 200) { |
| | | // 请求返回的数据 |
| | | in = conn.getInputStream(); |
| | | //输入到临时文件 |
| | | /*OutputStream os = new FileOutputStream(file); |
| | | int bytesRead = 0; |
| | | byte[] buffer = new byte[8192]; |
| | | while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | }*/ |
| | | FileUtils.copyInputStreamToFile(in, file); |
| | | |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | |
| | | if (in != null) { |
| | | in.close(); |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (in != null) { |
| | | try { |
| | | in.close(); |
| | | }catch (Exception ep) { |
| | | ep.printStackTrace(); |
| | | } |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }finally { |
| | | file.deleteOnExit(); |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | /** |
| | | * 获取小程序订阅模板 |
| | | * @param accessToken token |
| | | * **/ |
| | | public static HttpHzResponse appTemplateList(String accessToken) { |
| | | return HttpHzUtil.HttpURLUtilJson(GET_APP_TEMPLATE + accessToken, null,null,null,"GET",null); |
| | | } |
| | | |
| | | /** |
| | | * 公众号订阅模板 |
| | | * @param accessToken token |
| | | * **/ |
| | | public static HttpHzResponse gzhTemplateList(String accessToken) { |
| | | return HttpHzUtil.HttpURLUtilJson(GET_GZH_TEMPLATE+accessToken, null,null,null,"GET",null); |
| | | } |
| | | |
| | | } |