package cn.hmsoft.weixin.request;

import java.time.LocalDateTime;

import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.weixin.entity.response.TokenResponseJson;
import cn.hmsoft.weixin.util.WeiXinParamUtil;

/*******************************
 * @note 获取token并保存到数据库
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-09 01:02:10
 * @version 4.0.0
 */
public class TokenRequest extends AbstractRequest<TokenResponseJson, TokenResponseJson, String> {

	private final static String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

	@Override
	protected String makeUrl() {
		return url + "&appid=" + this.wxDefine.getApp_id() + "&secret=" + this.wxDefine.getApp_secrect();
	}

	@Override
	public void processBusiness() {
		// 如果获取到了
		if (StringHelper.isNotEmpty(this.jsonEntity.getAccess_token())) {
			this.wxDefine.setApp_accesstoken(jsonEntity.getAccess_token());
			WeiXinParamUtil.TokenExpiredSeconds = jsonEntity.getExpires_in();
			// 到期时间
			this.wxDefine.setApp_token_expired(LocalDateTime.now().plusSeconds(WeiXinParamUtil.TokenExpiredSeconds));
		}
		this.returnEntity = jsonEntity;

	}
}
