<script src="<%=path %>/resources/weixin/js/jquery-1.11.3.js">script>
<script src="<%=path %>/resources/weixin/js/vue.js">script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js">script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: '<%=path %>' + '/wx/member/share',
type: "POST",
data: {url : window.location.href},
//获取当前页面路径
dataType: "json",
cache: false,
//
第一次请求完毕之后,如果再次去请求,可以直接从缓存里面读取而不是再到服务器端读取
success: function(data) {
//alert(data.member.nickName);
wx.config({
debug:false,
// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参
数,
可以在pc端打开,参数信息会通过log打出,
仅在pc端时才会打印。
appId: data.wxAppId,
// 必填,公众号的唯一标识
timestamp: data.timestamp,
// 必填,生成签名的时间戳
nonceStr: data.nonce_str,
// 必填,生成签名的随机串
signature: data.signature,
// 必填,签名,见附录1
jsApiList: ["scanQRCode"] // 必填,需要使用的JS接口列表,
});
wx.error(function(res){
alert("错误信息== " + res.errMsg);
});
}
});
});
$("#scanQRCode").click(function() {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
$("#id_securityCode_input").val(result);
}
});
});
script>
控制层
package com.xuanyu.wmlt.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
/**
* Utils - 微信操作工具类
* add by sl on 2015-12-04 09:42:34
* @author XIAER Team
* @version 1.0
*/
public final class WeiXinUtils {
private static Logger logger = Logger.getLogger(WeiXinUtils.class);
//正式帐号
//微信公共账号appid
// public static final String APPID = "wx59eb0c0109637d21";
// //微信公共账号addsecret
// public static final String APP_SECRET ="3d2c5b85646f0a608afddcb98f1d2398";
// //微信商户号
// public static final String MCH_ID="1253487501";
// //微信商户号32位密钥
// public static final String PARTNERKEY = "wan1mu2liang3tianxingbiantianxia";
// //微信商户安全证书路径
// public static final String CERT_URL = "/root/cert/tg/apiclient_cert.p12";
//测试帐号
//微信公共账号appid
// public static final String APPID = "wxbe05b09decc1569c";
public static final String APPID = "wxf343d4ffc3ac5a13";
//微信公共账号addsecret
// public static final String APP_SECRET ="7c748ef830b84a7ad853880e2a558af2";
public static final String APP_SECRET ="aa8c8091b6ad3635ac81d8ee29289903";
//微信商户号
public static final String MCH_ID="1438446002";
//微信商户号32位密钥
public static final String PARTNERKEY = "wan1mu2liang3tianxingbiantianxia";
//微信商户安全证书路径
public static final String CERT_URL = "D://apiclient_cert.p12";
//本地--微信回调地址
public static final String REDIRECT_URI = "http://wangxuesong.tunnel.qydev.com/wmlt/wx/navigation/toIndex";
//微信登录注册链接
public static final String REQUEST_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID+"&redirect_uri="+REDIRECT_URI+"&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect";
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(WeiXinUtils.shortURL(WeiXinUtils.REQUEST_URL, WeiXinUtils.APPID, WeiXinUtils.APP_SECRET));
}
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/**
* add by sl on 2016年4月20日 14:53:50
* 获取accessToken
* @return accessToken
*/
public static String getAccess_token(String wxAppId, String secret) {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ wxAppId +"&secret=" + secret;
JSONObject jsonobject = JsonUtils.toJSONObject(HttpUtil.getInvoke(url));
return jsonobject.getString("access_token");
}
/**
* 长链接转成短链接 提高扫码速度和成功率
* add by sl on 2016年4月20日 14:54:02
* @param longURL
* @return shortURL
*/
public static String shortURL(String longURL, String wxAppId, String secret) {
String requestUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN";
try {
requestUrl = requestUrl.replace("ACCESS_TOKEN",getAccess_token(wxAppId, secret));
} catch (Exception e) {
logger.error(e.getMessage());
}
String jsonMsg = "{\"action\":\"long2short\",\"long_url\":\"%s\"}";
String.format(jsonMsg, longURL);
JSONObject jsonobject = JsonUtils.toJSONObject(HttpUtil.post(requestUrl, String.format(jsonMsg, longURL)));
return jsonobject.getString("short_url");
}
/**
* 获取网页授权accessToken json字符串
* add by zl on 2016-1-9 18:43:09
* @return accessToken
*/
public static String getAccessTokenJson(String code, String wxAppId, String wxSecret){
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+ wxAppId + "&secret=" + wxSecret + "&code=" + code + "&grant_type=authorization_code";
logger.info("获取微信code:" + code);
logger.info("appid------>:" + wxAppId);
logger.info("secret------>:" + wxSecret);
logger.info("url------>:" + url);
return HttpUtil.getInvoke(url);
}
/**
* 获取openid
* add by zl on 2016-3-25
* @return
*/
public static String getOpenid(String code, String wxAppId, String wxSecret){
JSONObject jsontoken = JsonUtils.toJSONObject(getAccessTokenJson(code, wxAppId, wxSecret));
return jsontoken.getString("openid");
}
/**
* 获取用户信息
* add by zl on 2016-1-9 19:18:33
* @param code
* @return
* @throws IOException
*/
public static String getUserInfo(String code, String wxAppId, String wxSecret) throws IOException {
JSONObject jsontoken = JsonUtils.toJSONObject(getAccessTokenJson(code, wxAppId, wxSecret));
try {
String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+jsontoken.getString("access_token")+"&openid="+ jsontoken.getString("openid") +"&lang=zh_CN";
String newstr = HttpUtil.getInvoke(url);
String userinfo = newstr.replace("\"", "'");
return userinfo;
} catch (Exception e) {
logger.error(e.getMessage());
return null;
}
}
/**
* 获取jsapi_ticket
* @author WFJ
* @time 2017年4月4日21:50:52
* @return
*/
public static String getJsapiTicket(){
String accessToken = getAccess_token(APPID, APP_SECRET);
try {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
JSONObject jsonobject = JsonUtils.toJSONObject(HttpUtil.getInvoke(url));
return jsonobject.getString("ticket");
} catch (Exception e) {
logger.error(e.getMessage());
return null;
}
}
/**
* 获取微信签名signature
* @param url jsapi_ticket
* add by zl on 2016-2-2
* @return signature
* @throws IOException
*/
public Map getSignature(String url, String jsapi_ticket) throws IOException {
Map wxMap = new HashMap();
// 调用获取随机字符串的方法
String nonce_str = HttpRequest.create_nonce_str();
// 调用获取随机数的方法
String timestamp = HttpRequest.create_timestamp();
// 定义签名参数
String signature = "";
// 拼接字符串
String string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr="
+ nonce_str + "×tamp=" + timestamp + "&url=" + url;
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
// 用算法获取签名
signature = HttpRequest.byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
wxMap.put("nonce_str", nonce_str);
wxMap.put("timestamp", timestamp);
wxMap.put("signature", signature);
return wxMap;
}
}