Commit d0efb5f6 authored by 马超's avatar 马超

feat(微问诊): 添加获取微问诊token的接口

parent b95d5824
package com.cftech.prescription.service;
import com.alibaba.fastjson.JSONObject;
/**
* 获取微问诊token Service
*
* @author :machao
* @date :Created in 2021/08/19 9:30
*/
public interface TokenService {
/**
* 获取微问诊token
*
* @return
*/
JSONObject getTokenResult();
}
package com.cftech.prescription.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.cftech.prescription.service.TokenService;
import com.cftech.prescription.utils.CdfortisTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 获取微问诊token Service
*
* @author :machao
* @date :Created in 2021/08/19 9:30
*/
@Slf4j
@Service
public class TokenServiceImpl implements TokenService {
@Autowired
CdfortisTokenUtil cdfortisTokenUtil;
@Override
public JSONObject getTokenResult() {
JSONObject rtnJson = new JSONObject();
try {
String token = cdfortisTokenUtil.getToken();
rtnJson.put("errorNo", "0");
rtnJson.put("token", token);
} catch (Exception e) {
e.printStackTrace();
rtnJson.put("errorNo", "1");
rtnJson.put("errorMsg", e.getMessage());
rtnJson.put("errorEnMsg", e.getMessage());
}
return rtnJson;
}
}
package com.cftech.prescription.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cftech.core.util.OKHttpUtils;
import com.cftech.core.util.SystemConfig;
import com.qcloud.cos.utils.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* 获取微问诊token Service
*
* @author :machao
* @date :Created in 2021/08/19 15:30
*/
@Slf4j
@Component
public class CdfortisTokenUtil {
/**
* 获取token的Url
*/
private static String tokenUrl = SystemConfig.p.getProperty("cdfortis.token_url");
/**
* appkey
*/
private static String appid = SystemConfig.p.getProperty("cdfortis.appid");
/**
* appSecrt
*/
private static String appSecret = SystemConfig.p.getProperty("cdfortis.secret");
/**
* flag
*/
private static String flag = SystemConfig.p.getProperty("cdfortis.flag");
/**
* redis存储key
*/
private static String CDFORTIS_TOKEN_KEY = "cdfortis_token" + ":" + appid;
/**
* 成功的返回值
*/
private static String RESULT_SUCC_CODE = "C00010000";
@Autowired
private RedisTemplate<String, String> redisTemplate;
private RedisTemplate<String, String> getConfig() {
StringRedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
/**
* 获取token
*
* @return
*/
public String getToken() throws Exception {
// 检查redis中是否存在
if (getConfig().hasKey(CDFORTIS_TOKEN_KEY)) {
return redisTemplate.opsForValue().get(CDFORTIS_TOKEN_KEY);
}
long timestamp = System.currentTimeMillis();
// 获取签名
String sign = sign(appid, appSecret, flag, timestamp);
// 获取tokenUrl
String url = tokenUrl.replace("{appid}", appid).replace("{flag}", flag)
.replace("{timestamp}", timestamp + "").replace("{sign}", sign);
log.debug("微问诊token请求地址:{}", url);
try {
// 解析结果
String retStr = OKHttpUtils.getJSON(url);
log.debug("微问诊token请求结果:{}", retStr);
JSONObject retObj = JSON.parseObject(retStr);
JSONObject returnCode = retObj.getJSONObject("returnCode");
if (!RESULT_SUCC_CODE.equals(returnCode.getString("key"))) {
log.error(returnCode.toJSONString());
throw new Exception(returnCode.getString("content"));
}
JSONObject data = retObj.getJSONObject("data");
String token = data.getString("token");
Long expire = data.getLongValue("expire");
//存到redis
redisTemplate.opsForValue().set(CDFORTIS_TOKEN_KEY, token, expire, TimeUnit.SECONDS);
return token;
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new Exception(e.getMessage());
}
}
/**
* 获取签名
*
* @param appId
* @param secret
* @param flag
* @param timestamp
* @return
*/
private String sign(String appId, String secret, String flag, long timestamp) {
String signStr = String.format("appid=%s&flag=%s&secret=%s&timestamp=%s", appId, flag, secret, timestamp);
String sign = Md5Utils.md5Hex(signStr.getBytes());
return sign;
}
}
package com.cftech.prescription.web;
import com.alibaba.fastjson.JSONObject;
import com.cftech.prescription.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 获取微问诊token
*
* @author :machao
* @date :Created in 2021/08/19 9:30
*/
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/mobile/auth/cdfortis")
public class CdfortisTokenController {
@Autowired
TokenService tokenService;
@GetMapping("get/token")
public JSONObject getToken() {
return tokenService.getTokenResult();
}
}
GET http://localhost:8080/aidea/a/prescription/list
###
\ No newline at end of file
GET http://localhost:8080/aidea/mobile/auth/cdfortis/get/token
###
\ No newline at end of file
...@@ -142,3 +142,12 @@ map.ads_resolve=https://apis.map.qq.com/ws/geocoder/v1/?address=${address} ...@@ -142,3 +142,12 @@ map.ads_resolve=https://apis.map.qq.com/ws/geocoder/v1/?address=${address}
map.key=OPIBZ-VYIW2-CK7UO-CM4WN-ELY2K-EUFYP map.key=OPIBZ-VYIW2-CK7UO-CM4WN-ELY2K-EUFYP
#\u7B7E\u540D\u5BC6\u94A5 #\u7B7E\u540D\u5BC6\u94A5
map.secret_key=W1je0RfMuDsfxCy73M0b3iEfZRF5cAcU map.secret_key=W1je0RfMuDsfxCy73M0b3iEfZRF5cAcU
#\u5fae\u95ee\u8bca\u914d\u7f6e\u53c2\u6570
#\u83b7\u53d6\u0074\u006f\u006b\u0065\u006e\u5730\u5740
#cdfortis.token_url=https://171.cdfortis.com/api/verify/token?appid={appid}&flag={flag}&timestamp={timestamp}&sign={sign}
cdfortis.token_url=https://api.cdfortis.com/api/verify/token?appid={appid}&flag={flag}&timestamp={timestamp}&sign={sign}
cdfortis.appid=74523ca670a6ceab8095a7476805c649
cdfortis.secret=c2cf8d3e9a6c715a8046541bf397ccc6
cdfortis.flag=aidea888
\ No newline at end of file
...@@ -142,4 +142,11 @@ map.ads_resolve=https://apis.map.qq.com/ws/geocoder/v1/?address=${address} ...@@ -142,4 +142,11 @@ map.ads_resolve=https://apis.map.qq.com/ws/geocoder/v1/?address=${address}
#\u5F00\u53D1\u8005\u5BC6\u94A5 #\u5F00\u53D1\u8005\u5BC6\u94A5
map.key=OPIBZ-VYIW2-CK7UO-CM4WN-ELY2K-EUFYP map.key=OPIBZ-VYIW2-CK7UO-CM4WN-ELY2K-EUFYP
#\u7B7E\u540D\u5BC6\u94A5 #\u7B7E\u540D\u5BC6\u94A5
map.secret_key=W1je0RfMuDsfxCy73M0b3iEfZRF5cAcU map.secret_key=W1je0RfMuDsfxCy73M0b3iEfZRF5cAcU
\ No newline at end of file
#\u5fae\u95ee\u8bca\u914d\u7f6e\u53c2\u6570
#\u83b7\u53d6\u0074\u006f\u006b\u0065\u006e\u5730\u5740
cdfortis.token_url=https://api.cdfortis.com/api/verify/token?appid={appid}&flag={flag}&timestamp={timestamp}&sign={sign}
cdfortis.appid=wxc587e8869baec269
cdfortis.secret=ad5b018eb390c9f8c1532c56f4767cc0
cdfortis.flag=aidea888
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment