package org.springblade.common.cache;

import org.springblade.common.enums.ResCode;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;

import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description: 微信模块缓存
 * @since: 9/6/21 -- 8:54 PM
 */
public class WechatCache {

	private static BladeRedis bladeRedis;
	private static final String AC_PREFIX = "ac";

	static {
		bladeRedis = SpringUtil.getBean("bladeRedis");
		if (Objects.isNull(bladeRedis)) {
			throw new ServiceException(ResCode.USER_PWS_ERROR);
		}
	}

	/**
	 * 获取缓存的AccessToken信息
	 */
	public static String getAccessToken(String appId) {
		return bladeRedis.get(StringUtil.format("{}:{}:{}", CacheNames.WECHAT, AC_PREFIX, appId));
	}

	/**
	 * 缓存AccessToken信息
	 */
	public static void putAccessToken(String appId, String ac) {
		bladeRedis.setEx(StringUtil.format("{}:{}:{}", CacheNames.WECHAT, AC_PREFIX, appId), ac, 60 * 60 * 1L);
	}
}
