博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA H5微信分享
阅读量:5137 次
发布时间:2019-06-13

本文共 3656 字,大约阅读时间需要 12 分钟。

一、前台代码:

View Code

 

二、java后台代码:

package com.unionpay.acp.sdk;import java.util.UUID;import java.util.Map;import java.util.HashMap;import java.util.Formatter;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.io.UnsupportedEncodingException;import net.sf.json.JSONObject;import com.xx.common.config.Global;import com.unionpay.acp.sdk.HttpClient;/*** 微信分享签名**/public class Sign {public static void main(String[] args) {// String jsapi_ticket = "kgt8ON7yVITDhtdwci0qecRXz9HyXuyXgIslzmd3ri0s5EnLrVZZfrPJotRfKWQ-TTuQ2ZMAK86GUlWF1PHhcg";};public static Map
getResult(){// String jsapi_ticket = getTicket();// 注意 URL 一定要动态获取,不能 hardcodeString url = "http://www.xx.com/operation/mybusi/barcode";// String url = Global.getConfig("wx.shareUrl");Map
ret = sign(getTicket(), url);ret.put("appId", getAppId());return ret;}private static String getAppId(){return Global.getConfig("wx.appid");}private static String getToken(){String accessToken = "";String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+getAppId()+"&secret="+Global.getConfig("wx.secret");HttpClient hc = new HttpClient(url, 30000, 30000);try {int status = hc.sendGet("utf-8");if (200 == status) {String resultString = hc.getResult();if (null != resultString && !"".equals(resultString)) {JSONObject json = JSONObject.fromObject(resultString);if (null==json.get("errcode")){accessToken = json.get("access_token").toString();} else {System.out.println(resultString);}}}else{System.out.println("返回http状态码["+status+"],请检查请求报文或者请求地址是否正确");}} catch (Exception e) {e.printStackTrace();}return accessToken;}private static String getTicket(){String ticket = "";String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+getToken()+"&type=jsapi";HttpClient hc = new HttpClient(url, 30000, 30000);try {int status = hc.sendGet("utf-8");if (200 == status) {String resultString = hc.getResult();if (null != resultString && !"".equals(resultString)) {JSONObject json = JSONObject.fromObject(resultString);if ("0".equals(json.getString("errcode"))){ticket = json.getString("ticket");} else {System.out.println(resultString);}}}else{System.out.println("返回http状态码["+status+"],请检查请求报文或者请求地址是否正确");}} catch (Exception e) {e.printStackTrace();}return ticket;}public static Map
sign(String jsapi_ticket, String url) {Map
ret = new HashMap
();String nonce_str = create_nonce_str();String timestamp = create_timestamp();String string1;String signature = "";//注意这里参数名必须全部小写,且必须有序string1 = "jsapi_ticket=" + jsapi_ticket +"&noncestr=" + nonce_str +"&timestamp=" + timestamp +"&url=" + url;System.out.println(string1);try{MessageDigest crypt = MessageDigest.getInstance("SHA-1");crypt.reset();crypt.update(string1.getBytes("UTF-8"));signature = byteToHex(crypt.digest());}catch (NoSuchAlgorithmException e){e.printStackTrace();}catch (UnsupportedEncodingException e){e.printStackTrace();}ret.put("url", url);ret.put("jsapi_ticket", jsapi_ticket);ret.put("nonceStr", nonce_str);ret.put("timestamp", timestamp);ret.put("signature", signature);return ret;}private static String byteToHex(final byte[] hash) {Formatter formatter = new Formatter();for (byte b : hash){formatter.format("%02x", b);}String result = formatter.toString();formatter.close();return result;}private static String create_nonce_str() {return UUID.randomUUID().toString();}private static String create_timestamp() {return Long.toString(System.currentTimeMillis() / 1000);}}
View Code

 

转载于:https://www.cnblogs.com/ice-line/p/5646192.html

你可能感兴趣的文章
dede标签:arclist标签使用大全
查看>>
登录之后返回上一页
查看>>
字符串与文件的匹配
查看>>
关于软件IntelliJ IDEA的使用技巧(三)
查看>>
php中屏蔽date的错误
查看>>
iOS自动布局框架-Masonry详解
查看>>
IOS 开发-- 常用-- 核心代码
查看>>
JavaScript 学习心得 基础
查看>>
一些不太常见但很有用的java类
查看>>
web 前端安全
查看>>
027:高级复制
查看>>
css3
查看>>
windows2008 https TLS使用1.2
查看>>
移动端开发——关于局部区域滚动总结---实战系列
查看>>
JQuery框架原理模拟实现
查看>>
入门命令21-给磁盘做个体检:chkdsk
查看>>
SQL Server优化50法(转载)
查看>>
VS2013 Cordova blank app command failed with exit code 8 error
查看>>
scrapy爬虫框架将数据保存Mysql数据库中
查看>>
Gcc的编译流程分为了四个步骤:
查看>>