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



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; 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值