首页>代码>jfinal开发java web开源微信商城源码>/jfinal-wxmall/weixin-sdk/src/main/java/com/weixin/sdk/api/AccessToken.java
/**
 * Copyright (c) 2011-2014, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package com.weixin.sdk.api;

import java.io.Serializable;
import java.util.Map;

import com.weixin.sdk.utils.JsonUtils;
import com.weixin.sdk.utils.RetryUtils.ResultCheck;

/**
 * 封装 access_token
 */
public class AccessToken implements ResultCheck, Serializable {
	
	private static final long serialVersionUID = -822464425433824314L;
	
	private String access_token;	// 正确获取到 access_token 时有值
	private Integer expires_in;		// 正确获取到 access_token 时有值
	private Integer errcode;		// 出错时有值
	private String errmsg;			// 出错时有值
	
	private Long expiredTime;		// 正确获取到 access_token 时有值,存放过期时间
	private String json;
	
	@SuppressWarnings("unchecked")
	public AccessToken(String jsonStr) {
		this.json = jsonStr;

		try {
			Map<String, Object> temp = JsonUtils.parse(jsonStr, Map.class);
			access_token = (String) temp.get("access_token");
			expires_in = getInt(temp, "expires_in");
			errcode = getInt(temp, "errcode");
			errmsg = (String) temp.get("errmsg");

			if (expires_in != null)
				expiredTime = System.currentTimeMillis() + ((expires_in -5) * 1000);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public String getJson() {
		return json;
	}
	
	public boolean isAvailable() {
		if (expiredTime == null)
			return false;
		if (errcode != null)
			return false;
		if (expiredTime < System.currentTimeMillis())
			return false;
		return access_token != null;
	}
	
	private Integer getInt(Map<String, Object> temp, String key) {
		Number number = (Number) temp.get(key);
		return number == null ? null : number.intValue();
	}
	
	public String getAccessToken() {
		return access_token;
	}
	
	public Integer getExpiresIn() {
		return expires_in;
	}
	
	public Integer getErrorCode() {
		return errcode;
	}
	
	public String getErrorMsg() {
		if (errcode != null) {
			String result = ReturnCode.get(errcode);
			if (result != null)
				return result;
		}
		return errmsg;
	}

	@Override
	public boolean matching() {
		return isAvailable();
	}
}
最近下载更多
微信网友_6790966844739584  LV5 2024年6月12日
ewan007  LV30 2024年2月24日
是夜在熬我.  LV6 2024年2月20日
去码头整点薯条  LV3 2023年11月15日
wuying8208  LV15 2023年7月27日
gdt633  LV2 2023年6月30日
稻草人a  LV1 2023年4月6日
臧家旺  LV3 2022年12月7日
zymnba  LV2 2022年6月25日
我喜欢蔡徐坤  LV1 2022年6月20日
最近浏览更多
周粥粥 4月11日
暂无贡献等级
zhangkk  LV7 2月8日
微信网友_6955249237250048  LV5 2024年12月28日
txf123  LV6 2024年12月10日
sjwc88  LV3 2024年12月4日
pangzhihui  LV14 2024年11月12日
火心人  LV2 2024年11月1日
颜菜菜  LV2 2024年9月13日
曾显示  LV6 2024年7月7日
binzhuyou  LV2 2024年7月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友