微信公众号开发--微信JS-SDK扫一扫功能(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Qizonghui/article/details/78237436


package com.oel.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.httpclient.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.ouyeel.controller.AuthController;
import com.ouyeel.wx.pojo.AccessToken;
import com.ouyeel.wx.util.WeixinUtil;

import net.sf.json.JSONObject; 

public class Sign {
 
 private static Logger log = LoggerFactory.getLogger(Sign.class); 
    public static void main(String[] args) {
       String url=request.getRequestURL()+"";
  if(request.getQueryString()!=null){
   url = url+"?"+request.getQueryString().toString();
  }
  Map map = Sign.sign( url ,appid,secret);
    }
 
 
    public static Map<String, String> sign(String url,String appid,String secret) {
     //这里的jsapi_ticket是获取的jsapi_ticket。
//        String jsapi_ticket1 = JSAPITicketTool.getTicket();
//      AccessToken accessToken = WeixinUtil.getAccessToken(appid, secret);
     String jsapi_ticket = getJsapiTicket(appid, secret);
     
        Map<String, String> ret = new HashMap<String, String>();
        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);
    }
   
    private static String getJsapiTicket(String appid, String secret){
     log.debug("请求获取getJsapiTicket==="+"参数是:"+"appid="+appid+";secret=="+secret+"==============");
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?"; 
        String params = "grant_type=client_credential&appid=" + appid + "&secret=" + secret + ""; 
       
        log.debug("获取Token路径"+requestUrl+params);
        JSONObject result = WeixinUtil.httpRequest(requestUrl+params,"GET",null);
        if(result==null){
         log.debug("result获取Token路径为空");
        }
        System.out.println("JSONObject result========"+result);
       
        String access_token = result.getString("access_token");
        
        requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?"; 
        params = "access_token=" + access_token + "&type=jsapi";
        log.debug("获取路径"+requestUrl+params);
        result = WeixinUtil.httpRequest(requestUrl+params,"GET",null); 
        String jsapi_ticket =result.getString("ticket"); 
//        int activeTime=Integer.parseInt(JSONObject.parseObject(result).getString("expires_in")); 
       /* Jssdk jssdk = new Jssdk(); 
        jssdk.setActiveTime(activeTime-600); 
        jssdk.setJsapiTicket(jsapi_ticket); 
        jssdkDao.update(jssdk);*/ 
        return jsapi_ticket; 
    }
}

展开阅读全文

没有更多推荐了,返回首页