|
@ -0,0 +1,114 @@
|
|
|
package com.yihu.wlyy.util;
|
|
|
|
|
|
import com.yihu.wlyy.entity.security.Token;
|
|
|
import net.sf.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
|
* Created by Trick on 2018/8/20.
|
|
|
* 1.5.0 版本 将token 存储在redis
|
|
|
*/
|
|
|
@Component
|
|
|
public class SystemDataRedis {
|
|
|
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
/**
|
|
|
* tokenTypeName
|
|
|
*/
|
|
|
public final static String doctorTokens = "doctorTokens";//医生缓存
|
|
|
public final static String doctorPCTokens = "doctorPCTokens";//pc端缓存
|
|
|
public final static String patientTokens = "patientTokens";//居民缓存
|
|
|
public final static String doctorWXTokens = "doctorWXTokens";//医生微信缓存
|
|
|
|
|
|
|
|
|
//====设置token========
|
|
|
/**
|
|
|
* 设置医生缓存
|
|
|
* @param token
|
|
|
*/
|
|
|
public String setDoctorToken(Token token){
|
|
|
return setToken(doctorTokens,token);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置pc端缓存
|
|
|
* @param token
|
|
|
*/
|
|
|
public String setDoctorPCToken(Token token){
|
|
|
return setToken(doctorPCTokens,token);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置居民缓存
|
|
|
* @param token
|
|
|
*/
|
|
|
public String setPatientToken(Token token){
|
|
|
return setToken(patientTokens,token);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置医生微信缓存
|
|
|
* @param token
|
|
|
*/
|
|
|
public String setDoctorWXToken(Token token){
|
|
|
return setToken(doctorWXTokens,token);
|
|
|
}
|
|
|
//====设置token==end===
|
|
|
|
|
|
|
|
|
//====获取token========
|
|
|
|
|
|
public Token getDoctorToken(String uid){
|
|
|
return getToken(doctorTokens,uid);
|
|
|
}
|
|
|
public Token getDoctorPCToken(String uid){
|
|
|
return getToken(doctorPCTokens,uid);
|
|
|
}
|
|
|
public Token getPatientToken(String uid){
|
|
|
return getToken(patientTokens,uid);
|
|
|
}
|
|
|
public Token getDoctorWXToken(String uid){
|
|
|
return getToken(doctorWXTokens,uid);
|
|
|
}
|
|
|
//=====获取token===end===
|
|
|
|
|
|
/**
|
|
|
* 设置token
|
|
|
* @param tokenTypeName
|
|
|
* @param token
|
|
|
*/
|
|
|
public String setToken(String tokenTypeName,Token token){
|
|
|
|
|
|
JSONObject json = JSONObject.fromObject(token);
|
|
|
String key = "token:"+tokenTypeName+":"+token.getUser();
|
|
|
redisTemplate.opsForValue().set(key,json.toString());
|
|
|
return json.toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取token
|
|
|
* @param tokenTypeName
|
|
|
* @param uid
|
|
|
* @return
|
|
|
*/
|
|
|
public Token getToken(String tokenTypeName,String uid) {
|
|
|
String tokenJosn = redisTemplate.opsForValue().get("token:" + tokenTypeName + ":" + uid);
|
|
|
Token token = (Token)JSONObject.toBean(JSONObject.fromObject(tokenJosn),Token.class);
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除token
|
|
|
* @param tokenTypeName
|
|
|
* @param uid
|
|
|
* @return
|
|
|
*/
|
|
|
public void delToken(String tokenTypeName,String uid){
|
|
|
String key = "token:"+tokenTypeName+":"+uid;
|
|
|
redisTemplate.delete(key);
|
|
|
}
|
|
|
}
|