码迷,mamicode.com
首页 > 微信 > 详细

关于微信

时间:2015-07-06 19:27:44      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

1、获取微信js所必须的签名

 1 logger.info("获取凭证..");
 2             String code=(String) SecurityUtils.getSubject().getSession().getAttribute("Sign_code");
 3             String state=(String) SecurityUtils.getSubject().getSession().getAttribute("Sign_state");
 4             logger.info(state);
 5             logger.info(code);
 6             String ticket = null;
 7             // 存储凭证和生成时间
 8             FishingSingle instance = FishingSingle.instance;
 9             if (null != instance.getJsGenerateTime()) {
10                 Date currentTime = new Date();
11                 // 计算时间差
12                 long time = ((currentTime.getTime() - instance.getJsGenerateTime()
13                         .getTime()) / 1000);
14                 // ticket有效时间7200秒
15                 if (time >= 7100) {
16                     // 获取js凭证
17                     ticket = WeixinUtil.jsApi();
18                 } else {
19                     ticket = instance.getJsTicket();
20                 }
21             } else {
22                 // 获取js凭证
23                 ticket = WeixinUtil.jsApi();
24             }
25             //ticket = WeixinUtil.jsApi();
26             Map<String, Object> paramsMap = new HashMap<String, Object>();
27             paramsMap.put("jsapi_ticket", ticket);
28             paramsMap.put("timestamp", System.currentTimeMillis());
29             paramsMap.put("noncestr", "weixin");
30             // 调用js的页面完成路径
31             String url=(String) MiceConfig.getContextProperty("wd_URL");
32             if(code==null){
33                 paramsMap.put("url", url+"personal/toSign");
34             }else{
35                 paramsMap.put("url", url+"personal/toSign?code="+code+"&state="+state);
36             }
37             
38             // 排序后的结果
39             String listStr = createLinkString(paramsMap);
40             logger.info("排序后..." + listStr);
41             // SHA-1加密
42             String tmpStr = new MySecurity().encode(listStr, MySecurity.SHA_1);
43             logger.info("SHA-1加密后:" + tmpStr);
44             // 保存js需要的4个参数
45             mv.addObject("appId", WeixinUtil.appId);
46             logger.info(WeixinUtil.appId);
47             mv.addObject("timestamp", paramsMap.get("timestamp"));
48             mv.addObject("nonceStr", paramsMap.get("noncestr"));
49             mv.addObject("signature", tmpStr);

所需要的加密方法

 1 /**
 2      * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
 3      * 
 4      * @param params
 5      *            需要排序并参与字符拼接的参数组
 6      * @return 拼接后字符串
 7      */
 8     public static String createLinkString(Map<String, Object> params) {
 9 
10         List<String> keys = new ArrayList<String>(params.keySet());
11         Collections.sort(keys);
12 
13         String prestr = "";
14 
15         for (int i = 0; i < keys.size(); i++) {
16             String key = keys.get(i);
17             Object value = params.get(key);
18 
19             if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
20                 prestr = prestr + key + "=" + value;
21             } else {
22                 prestr = prestr + key + "=" + value + "&";
23             }
24         }
25 
26         return prestr;
27     }
28 
29     


2、页面必须要导入微信所提供的js

1 <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

3、页面必须先通过签名

 1 wx.config({
 2                 debug : false,
 3                 appId : ‘${appId}‘,
 4                 timestamp : ${timestamp},
 5                 nonceStr : ‘${nonceStr}‘,
 6                 signature : ‘${signature}‘,
 7                 jsApiList : [ ‘checkJsApi‘, ‘onMenuShareTimeline‘,
 8                         ‘onMenuShareAppMessage‘, ‘onMenuShareQQ‘, ‘onMenuShareWeibo‘,
 9                         ‘hideMenuItems‘, ‘showMenuItems‘, ‘hideAllNonBaseMenuItem‘,
10                         ‘showAllNonBaseMenuItem‘, ‘translateVoice‘, ‘startRecord‘,
11                         ‘stopRecord‘, ‘onRecordEnd‘, ‘playVoice‘, ‘pauseVoice‘,
12                         ‘stopVoice‘, ‘uploadVoice‘, ‘downloadVoice‘, ‘chooseImage‘,
13                         ‘previewImage‘, ‘uploadImage‘, ‘downloadImage‘,
14                         ‘getNetworkType‘, ‘openLocation‘, ‘getLocation‘,
15                         ‘hideOptionMenu‘, ‘showOptionMenu‘, ‘closeWindow‘,
16                         ‘scanQRCode‘, ‘chooseWXPay‘, ‘openProductSpecificView‘,
17                         ‘addCard‘, ‘chooseCard‘, ‘openCard‘ ]
18             });


4、执行一些具体的微信提供的东西

wx.ready(function() {

}

 5、扫一扫(在4中执行)

wx.scanQRCode({
needResult :
1, desc : ‘scanQRCode desc‘, success : function(res) { //alert(JSON.stringify(res)); //alert(res.resultStr);
//获取一个字符串数组取值
var str=res.resultStr.split(","); var str1=str[0]; var str2=str[1];
//post提交方法 $.post(
‘‘,{}).success(function(data) {} } });

 

关于微信

标签:

原文地址:http://www.cnblogs.com/xyt-0412/p/4625051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!