|
@ -0,0 +1,156 @@
|
|
|
|
package com.yihu.jw.care.service.yunxin;
|
|
|
|
|
|
|
|
import com.yihu.jw.entity.base.yx.YxTokenMappingDO;
|
|
|
|
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
|
|
|
|
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
|
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
|
import com.yihu.jw.utils.CheckSumBuilder;
|
|
|
|
import com.yihu.jw.utils.GenerateUserSig;
|
|
|
|
import com.yihu.jw.yx.dao.YxTokenMappingDao;
|
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.http.NameValuePair;
|
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import static com.yihu.jw.util.common.StringUtil.randomInt;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created with IntelliJ IDEA.
|
|
|
|
*
|
|
|
|
* @Author: yeshijie
|
|
|
|
* @Date: 2021/9/9
|
|
|
|
* @Description:
|
|
|
|
*/
|
|
|
|
@Service
|
|
|
|
public class YunxinService {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(YunxinService.class);
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private WlyyHospitalSysDictDao hospitalSysDictDao;
|
|
|
|
@Autowired
|
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
|
@Autowired
|
|
|
|
private YxTokenMappingDao yxTokenMappingDao;
|
|
|
|
|
|
|
|
|
|
|
|
public String yxToken2(String userId,String channelName){
|
|
|
|
YxTokenMappingDO yxTokenMappingDO = yxTokenMappingDao.findMappingByAccid(userId);
|
|
|
|
if (yxTokenMappingDO!=null){
|
|
|
|
return yxTokenMappingDO.getToken();
|
|
|
|
}
|
|
|
|
WlyyHospitalSysDictDO sysDictDO = hospitalSysDictDao.findById("YXAPPKEY");
|
|
|
|
WlyyHospitalSysDictDO hospitalSysDictDO = hospitalSysDictDao.findById("YXAPPSECRET");
|
|
|
|
if (sysDictDO==null){
|
|
|
|
return "找不到对应的key";
|
|
|
|
}
|
|
|
|
String appKey = sysDictDO.getDictValue();
|
|
|
|
String appSecret = hospitalSysDictDO.getDictValue();
|
|
|
|
String nonce = randomInt(10);
|
|
|
|
String curTime = String.valueOf((new Date()).getTime() / 1000L);
|
|
|
|
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码
|
|
|
|
String url = "https://api.netease.im/nimserver/user/create.action";
|
|
|
|
Map<String,Object> httpPost = new HashedMap();
|
|
|
|
// 设置请求的header
|
|
|
|
httpPost.put("AppKey", appKey);
|
|
|
|
httpPost.put("Nonce", nonce);
|
|
|
|
httpPost.put("CurTime", curTime);
|
|
|
|
httpPost.put("CheckSum", checkSum);
|
|
|
|
httpPost.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
|
nvps.add(new BasicNameValuePair("accid", userId));
|
|
|
|
YxTokenMappingDO yxTokenMappingDO1 = new YxTokenMappingDO();
|
|
|
|
if (StringUtils.isNoneBlank(channelName)){
|
|
|
|
nvps.add(new BasicNameValuePair("name",channelName));
|
|
|
|
yxTokenMappingDO1.setName(channelName);
|
|
|
|
}
|
|
|
|
String response = httpClientUtil.headerPost(url,nvps,"UTF-8",httpPost);
|
|
|
|
logger.info("返回日志"+response);
|
|
|
|
if(StringUtils.isNoneBlank(response)){
|
|
|
|
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response);
|
|
|
|
if (jsonObject.getString("code").equalsIgnoreCase("200")){
|
|
|
|
com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(jsonObject.getString("info"));
|
|
|
|
if (StringUtils.isNoneBlank(jsonObject1.getString("token"))){
|
|
|
|
yxTokenMappingDO1.setAccid(userId);
|
|
|
|
yxTokenMappingDO1.setToken(jsonObject1.getString("token"));
|
|
|
|
yxTokenMappingDao.save(yxTokenMappingDO1);
|
|
|
|
}
|
|
|
|
return jsonObject1.getString("token");
|
|
|
|
}else if (jsonObject.getString("code").equalsIgnoreCase("414")&&jsonObject.getString("desc").contains("already")){
|
|
|
|
return refreshToken(userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 重置云信token
|
|
|
|
* @param userId
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String refreshToken(String userId){
|
|
|
|
YxTokenMappingDO yxTokenMappingDO = yxTokenMappingDao.findMappingByAccid(userId);
|
|
|
|
if (yxTokenMappingDO!=null){
|
|
|
|
return yxTokenMappingDO.getToken();
|
|
|
|
}
|
|
|
|
WlyyHospitalSysDictDO sysDictDO = hospitalSysDictDao.findById("YXAPPKEY");
|
|
|
|
WlyyHospitalSysDictDO hospitalSysDictDO = hospitalSysDictDao.findById("YXAPPSECRET");
|
|
|
|
if (sysDictDO==null){
|
|
|
|
return "找不到对应的key";
|
|
|
|
}
|
|
|
|
String appKey = sysDictDO.getDictValue();
|
|
|
|
String appSecret = hospitalSysDictDO.getDictValue();
|
|
|
|
String nonce = randomInt(10);
|
|
|
|
String curTime = String.valueOf((new Date()).getTime() / 1000L);
|
|
|
|
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码
|
|
|
|
String url = "https://api.netease.im/nimserver/user/refreshToken.action";
|
|
|
|
Map<String,Object> httpPost = new HashedMap();
|
|
|
|
// 设置请求的header
|
|
|
|
httpPost.put("AppKey", appKey);
|
|
|
|
httpPost.put("Nonce", nonce);
|
|
|
|
httpPost.put("CurTime", curTime);
|
|
|
|
httpPost.put("CheckSum", checkSum);
|
|
|
|
httpPost.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
|
nvps.add(new BasicNameValuePair("accid", userId));
|
|
|
|
YxTokenMappingDO yxTokenMappingDO1 = new YxTokenMappingDO();
|
|
|
|
String response = httpClientUtil.headerPost(url,nvps,"UTF-8",httpPost);
|
|
|
|
logger.info("重置云信"+response);
|
|
|
|
if(StringUtils.isNoneBlank(response)){
|
|
|
|
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response);
|
|
|
|
if (jsonObject.getString("code").equalsIgnoreCase("200")){
|
|
|
|
com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(jsonObject.getString("info"));
|
|
|
|
if (StringUtils.isNoneBlank(jsonObject1.getString("token"))){
|
|
|
|
yxTokenMappingDO1.setAccid(userId);
|
|
|
|
yxTokenMappingDO1.setToken(jsonObject1.getString("token"));
|
|
|
|
yxTokenMappingDao.save(yxTokenMappingDO1);
|
|
|
|
}
|
|
|
|
return jsonObject1.getString("token");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 腾讯视频生成签名
|
|
|
|
* @param userId
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String appletSign(String userId){
|
|
|
|
WlyyHospitalSysDictDO sysDictDO = hospitalSysDictDao.findById("SDKAPPID");
|
|
|
|
WlyyHospitalSysDictDO hospitalSysDictDO = hospitalSysDictDao.findById("SECRETKEY");
|
|
|
|
if (sysDictDO!=null&&hospitalSysDictDO!=null){
|
|
|
|
return GenerateUserSig.GenTLSSignature(Long.parseLong(sysDictDO.getDictValue()),userId,604800,null,hospitalSysDictDO.getDictValue());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|