java如何快速接入微信JS-SDK

本文以接入微信图像接口为例,简单介绍如何快速使用java接入微信JS-SDK

1、jsp页面代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
    content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
<meta name="description" content="">
<meta name="author" content="">
<title>微信demo</title>
<script type="text/javascript"
    src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</head>
<body>
    <h3 id="menu-image">图像接口</h3>
    <span>拍照或从手机相册中选图接口</span>
    <button class="btn btn_primary" id="chooseImage">chooseImage</button>
    <script type="text/javascript">
        //ajax动态请求签名等参数
        $.ajax({
            url : "wx/config",
            type : 'POST',
            data : {
                //当前网页url
                url : location.href.split('#')[0]
            },
            success : function(res) {
                wx.config({
                    debug : false,
                    appId : res.appId,
                    timestamp : res.timestamp,
                    nonceStr : res.nonceStr,
                    signature : res.signature,
                    jsApiList : [ 'chooseImage' ]
                });
            }
        });

        wx
                .ready(function() {
                    // 5 图片接口
                    // 5.1 拍照、本地选图
                    var images = {
                        localId : [],
                        serverId : []
                    };
                    document.querySelector('#chooseImage').onclick = function() {
                        wx.chooseImage({
                            success : function(res) {
                                images.localId = res.localIds;
                                alert('已选择 ' + res.localIds.length + ' 张图片');
                            }
                        });
                    };

                    var shareData = {
                        title : '方倍工作室 微信JS-SDK DEMO',
                        desc : '微信JS-SDK,帮助第三方为用户提供更优质的移动web服务',
                        link : 'http://www.cnblogs.com/txw1958/',
                        imgUrl : 'http://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0'
                    };
                    wx.onMenuShareAppMessage(shareData);
                    wx.onMenuShareTimeline(shareData);
                });

        wx.error(function(res) {
            alert(res.errMsg);
        });
    </script>
</body>
</html>

2、后台java代码(以springmvc为例)

package com.hojo.avengers.app.wx.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hojo.avengers.app.wx.util.WXCommonUtil;
import com.hojo.avengers.app.wx.util.WXSign;
import com.hojo.avengers.app.wx.util.WXToken;

/**
 * 
 * @ClassName: WXAction
 * @Description: JS-SDK接入
 * @author yuxw http://my.oschina.net/yxwblog/blog
 * @date 2015年6月8日 下午5:51:25
 *
 */
@Controller
public class WXAction {
    private String appId = "appid";//填入自己公众号appid
    private String appsecret = "appsecret";//填入自己公众号appsecret


    @RequestMapping("/wx/config")
    @ResponseBody
    public WXParam config(HttpServletRequest request, String url) {
        WXToken token = WXCommonUtil.getToken(appId, appsecret);
        String ticket = WXCommonUtil.getJSSDKTicket(token.getAccessToken());
        Map<String, String> ret = WXSign.sign(ticket, url);

        System.out.println("timestamp:" + ret.get("timestamp"));
        System.out.println("nonceStr:" + ret.get("nonceStr"));
        System.out.println("signature:" + ret.get("signature"));
        System.out.println("url:" + url);

        WXParam wxParam = new WXParam();
        wxParam.setAppId(appId);
        wxParam.setTimestamp(ret.get("timestamp"));
        wxParam.setNonceStr(ret.get("nonceStr"));
        wxParam.setSignature(ret.get("signature"));
        //返回json对象
        return wxParam;
    }

    
    //封装微信签名等参数的对象
    class WXParam {
        private String appId;
        private String timestamp;
        private String nonceStr;
        private String signature;

        public String getAppId() {
            return appId;
        }

        public void setAppId(String appId) {
            this.appId = appId;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getNonceStr() {
            return nonceStr;
        }

        public void setNonceStr(String nonceStr) {
            this.nonceStr = nonceStr;
        }

        public String getSignature() {
            return signature;
        }

        public void setSignature(String signature) {
            this.signature = signature;
        }

    }
}

最终效果如下图:

java如何快速接入微信JS-SDK_第1张图片


你可能感兴趣的:(java如何快速接入微信JS-SDK)