微信相关地址加密

private String code;
	private String timeStamp;
	private String nonceStr;
	private String ftlUrl;



 * ajax请求拿到 相关地址信息
	 * @return
	 * @throws Exception 
	 */
	public String getWeiXingInfo() throws Exception{
		
		
		
		WeixinPayRequestHandler handler=new WeixinPayRequestHandler();
		String key=WeixinpayConfig.getKey(this.getSiteEnum());
		String appId = WeixinpayConfig.getAppid(this.getSiteEnum());
		String timestamp = String.valueOf(System.currentTimeMillis()/1000);
		String nonceStr = UUID.randomUUID().toString();
		String access_tocken = getCurrentUser().getAccessToken();
		String openid = getCurrentUser().getWebKey();
//		handler.setKey(key);
//		checkTocken(access_tocken, openid);
//		handler.setParameter("appId", appId);
//		handler.setParameter("url", "http://minitimes.oicp.net:54293/frontend/cart/view.php");
//		handler.setParameter("timestamp", timestamp);
//		handler.setParameter("noncestr", nonceStr);
//		handler.setParameter("access_token", access_tocken);
//		String addrSign=handler.createPaySign();
		
		String url = "http://yangmenghello.xicp.net:14561/frontend/checkout/view.php";
		String str = "accesstoken="+access_tocken+"&appid="+appId+"&noncestr="+nonceStr+"×tamp="+timeStamp+"&url="+url;;
		String addrSign1 = SHA1(str.toLowerCase());
		
		resultMap.put("appId", appId);
		resultMap.put("addrSign", addrSign1);
		resultMap.put("timestamp", timestamp);
		resultMap.put("nonceStr", nonceStr);
		
		resultMap.put("dd", "dd");
			
		return "json";
	}
	
	/** 检查 checkTocken 是否正确 */
	
	public void checkTocken(String access_token,String openid) throws IOException{
		String refresh_tocken = getCurrentUser().getRefreshToken();
		URL url = new URL("https://api.weixin.qq.com/sns/auth?access_token="+access_token+"&openid="+openid);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
		JSONObject json = JSONObject.fromObject(in.readLine());
		if(!json.get("errcode").equals(0)){
			this.refreshTocken(openid,refresh_tocken);
			System.out.println("密钥验证失败");
		}else{
			System.out.println("密钥验证成功");
		}
	}
	
	/** 刷新 refreshTocken */
	public void refreshTocken(String appid,String refresh_tocken) throws IOException{
		URL url = new URL("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+appid+"&grant_type=refresh_token&refresh_token="+refresh_tocken);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
		JSONObject json = JSONObject.fromObject(in.readLine());
		if(!json.get("errcode").equals(40029)){
			getCurrentUser().setAccessToken(String.valueOf(json.get("access_token")));
		}
	}
	
	/**
	 * SHA1加密成  addrsign
	 */
	
	public String SHA1(String decript) {
        try {
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();
 
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
	
	public String getTockenByCode() throws IOException{
		if(code == null){
			resultMap.put("status", false);
		}else{
				URL url1 = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WeiXinLoginMConfig.getWeiXinAppID(SiteEnum.LOVO)
						+"&secret="+WeiXinLoginMConfig.getWeiXinAppSecret(SiteEnum.LOVO)
						+"&code="+code
						+"&grant_type=authorization_code");
				HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
				BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
				JSONObject json = JSONObject.fromObject(in.readLine());
				if(json.get("access_token")==null){
					resultMap.put("status", false);
				}else{
					System.out.println("-------------------------------access_tocken:"+json.get("access_token").toString());
					String appId = WeixinpayConfig.getAppid(this.getSiteEnum());
					checkTocken(json.get("access_token").toString(), json.get("openid").toString());
					
					String str = "accesstoken="+getCurrentUser().getAccessToken()+"&appid="+appId+"&noncestr="+nonceStr+"×tamp="+timeStamp+"&url="+ftlUrl;
					String addrSign = SHA1(str);
					resultMap.put("access_token",getCurrentUser().getAccessToken());
					resultMap.put("refresh_token",json.get("refresh_token"));
					resultMap.put("openid",json.get("openid"));
					resultMap.put("unionid",json.get("unionid"));
					resultMap.put("scope",json.get("scope"));
					resultMap.put("addrSign",addrSign);
					resultMap.put("status",true);
					System.out.println("---------------------------------------------------------------输出迷药= "+addrSign);
					System.out.println("---------------------------------------------------------------输出常量  = "+nonceStr);
					System.out.println("---------------------------------------------------------------输出常量  = "+timeStamp);
					System.out.println("---------------------------------------------------------------输出常量  = "+appId);
				}
		}
		System.out.println("---------------------------------------------------------------输出url= "+ftlUrl);
		
		return "json";
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值