package com.uas.platform.b2b.manage.model;

import com.uas.platform.core.util.StringUtil;

import java.io.Serializable;
import java.util.Date;

/**
 * token登录，用于存放token信息
 * @author yingp
 * @version 2017年8月2日14:30:38 wangyc 完善类的注释
 *
 */
public class AccessToken implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String id;

	/**
	 * 用户名
	 */
	private String user;

	/**
	 * 可以在token绑定任意数据
	 */
	private String bind;

	/**
	 * 生成时间
	 */
	private Date time;

	/**
	 * 超时时间
	 */
	private int expires_in;
	
	/**
	 * 临时token，默认为60秒过期
	 */
	public final static int default_expires_in = 60;

	public AccessToken() {
	}

	public AccessToken(User user, Object bindObject) {
		this(user, bindObject, default_expires_in);
	}

	public AccessToken(User user, Object bindObject, int expires_in) {
		this.id = StringUtil.uuid();
		this.user = user.getName();
		this.bind = String.valueOf(bindObject);
		this.time = new Date();
		this.expires_in = expires_in;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public Object getBind() {
		return bind;
	}

	public void setBind(String bind) {
		this.bind = bind;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public int getExpires_in() {
		return expires_in;
	}

	public void setExpires_in(int expires_in) {
		this.expires_in = expires_in;
	}

	/**
	 * 是否过期
	 * 
	 * @return
	 */
	public boolean isExpired() {
		return System.currentTimeMillis() - this.time.getTime() > expires_in * 1000;
	}

}
