Merge branch 'master' of http://gitea.dctpay.com/hurui/youtao
parent
b7c36f63e2
commit
5ae48e90e2
@ -0,0 +1,55 @@ |
||||
package com.hfkj.controller; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hfkj.common.security.UserCenter; |
||||
import com.hfkj.common.utils.ResponseMsgUtil; |
||||
import com.hfkj.model.ResponseData; |
||||
import com.hfkj.model.UserSessionObject; |
||||
import com.hfkj.service.meituan.MeiTuanService; |
||||
import com.hfkj.service.user.BsUserAccountRecordService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
@Controller |
||||
@RequestMapping(value="/takeOut") |
||||
@Api(value="外卖") |
||||
public class TakeOutController { |
||||
|
||||
private static Logger log = LoggerFactory.getLogger(TakeOutController.class); |
||||
|
||||
@Autowired |
||||
private UserCenter userCenter; |
||||
|
||||
@RequestMapping(value="/generateLink",method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "自助取链接口 ") |
||||
public ResponseData generateLink(@RequestParam(value = "actId" , required = false) Long actId, |
||||
@RequestParam(value = "linkType" , required = false) Integer linkType |
||||
) { |
||||
try { |
||||
// 用户session
|
||||
UserSessionObject session = userCenter.getSessionModel(UserSessionObject.class); |
||||
|
||||
JSONObject jsonObject = new JSONObject(); |
||||
jsonObject.put("actId" , actId); |
||||
jsonObject.put("sid" , session.getUser().getId()); |
||||
jsonObject.put("linkType" , linkType); |
||||
|
||||
JSONObject object = MeiTuanService.generateLink(jsonObject); |
||||
|
||||
return ResponseMsgUtil.success(object); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,118 @@ |
||||
package com.hfkj.common.utils; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hfkj.config.CommonSysConst; |
||||
import org.apache.commons.codec.binary.Hex; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
|
||||
import javax.xml.bind.annotation.adapters.HexBinaryAdapter; |
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.util.Arrays; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.TreeMap; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class SignatureUtil { |
||||
/** |
||||
* 参数签名 |
||||
* @param param 参数 |
||||
* @param secret 秘钥 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String createSign(Object param, String secret) throws Exception { |
||||
Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class); |
||||
return md5Encode(paramSort(map, secret).getBytes()); |
||||
} |
||||
|
||||
/** |
||||
* 验证签名 |
||||
* @param checkSign 需验证的签名字符串 |
||||
* @param param 参数 |
||||
* @param secret 秘钥 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static Boolean checkSign(String checkSign,Object param, String secret) throws Exception { |
||||
Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class); |
||||
// 去除签名
|
||||
map.remove("sign"); |
||||
if (checkSign.equals(createSign(map, secret))) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 参数排序 |
||||
* @param param |
||||
* @return |
||||
*/ |
||||
public static String paramSort(final Map<String, Object> param, String secret) throws Exception { |
||||
Set<String> keySet = param.keySet(); |
||||
String[] keyArray = keySet.toArray(new String[keySet.size()]); |
||||
Arrays.sort(keyArray); |
||||
StringBuilder sb = new StringBuilder(); |
||||
for (String k : keyArray) { |
||||
if (StringUtils.isBlank(sb.toString())) { |
||||
sb.append(k).append("=").append(param.get(k)); |
||||
} else { |
||||
sb.append("&").append(k).append("=").append(param.get(k)); |
||||
} |
||||
} |
||||
sb.append(secret); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
/** |
||||
* MD5加密 |
||||
* @param data |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String md5Encode(byte[] data) throws Exception { |
||||
// 初始化MessageDigest
|
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||
// 执行摘要信息
|
||||
byte[] digest = md.digest(data); |
||||
// 将摘要信息转换为32位的十六进制字符串
|
||||
return new String(new HexBinaryAdapter().marshal(digest)); |
||||
} |
||||
|
||||
|
||||
public static String genSign(JSONObject object , String secret) throws Exception { |
||||
|
||||
// 转换为TreeMap
|
||||
TreeMap<String, Object> params = new TreeMap<>(); |
||||
for (String key : object.keySet()) { |
||||
params.put(key, object.get(key)); |
||||
} |
||||
params.remove("sign"); |
||||
StringBuilder stringBuilder = new StringBuilder(); |
||||
stringBuilder |
||||
.append(secret) |
||||
.append(params.entrySet().stream() |
||||
.map(entry -> entry.getKey() + entry.getValue()) |
||||
.collect(Collectors.joining())) |
||||
.append(secret); |
||||
return md5(stringBuilder.toString()); |
||||
} |
||||
|
||||
public static String md5(String source) { |
||||
String md5Result = null; |
||||
try { |
||||
byte[] hash = org.apache.commons.codec.binary.StringUtils.getBytesUtf8(source); |
||||
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); |
||||
messageDigest.update(hash); |
||||
hash = messageDigest.digest(); |
||||
md5Result = Hex.encodeHexString(hash); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return md5Result; |
||||
} |
||||
} |
||||
|
||||
|
@ -0,0 +1,63 @@ |
||||
package com.hfkj.service.meituan; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hfkj.common.exception.ErrorCode; |
||||
import com.hfkj.common.exception.ErrorHelp; |
||||
import com.hfkj.common.exception.SysCode; |
||||
import com.hfkj.common.utils.HttpsUtils; |
||||
import com.hfkj.common.utils.SignatureUtil; |
||||
import com.hfkj.config.CommonSysConst; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
public class MeiTuanService { |
||||
|
||||
private static Logger log = LoggerFactory.getLogger(MeiTuanService.class); |
||||
|
||||
|
||||
/** |
||||
* @MethodName generateLink |
||||
* @Description: 自助取链接口 |
||||
* @param param |
||||
* @return: com.alibaba.fastjson.JSONObject |
||||
* @Author: Sum1Dream |
||||
* @Date: 2024/10/11 下午4:35 |
||||
*/ |
||||
public static JSONObject generateLink(JSONObject param)throws Exception { |
||||
JSONObject object = request("generateLink" , param); |
||||
|
||||
if (object.getInteger("status").equals(0)) { |
||||
return object; |
||||
} else { |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, object.getString("des")); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @MethodName request |
||||
* @Description: 统一请求 |
||||
* @param postUrl |
||||
* @param param |
||||
* @return: com.alibaba.fastjson.JSONObject |
||||
* @Author: Sum1Dream |
||||
* @Date: 2024/10/11 下午3:34 |
||||
*/ |
||||
public static JSONObject request(String postUrl, JSONObject param) throws Exception { |
||||
|
||||
log.info("============ 美团请求-START ============="); |
||||
param.put("appkey", CommonSysConst.getSysConfig().getMeiTuanAppKey()); |
||||
param.put("sign" , SignatureUtil.genSign(param , CommonSysConst.getSysConfig().getMeiTuanAppSecret())); |
||||
|
||||
log.info("请求接口:" + postUrl); |
||||
log.info("请求参数:" + JSONObject.toJSONString(param)); |
||||
|
||||
|
||||
JSONObject response = HttpsUtils.doGet(CommonSysConst.getSysConfig().getMeiTuanPostUrl() + postUrl, param); |
||||
|
||||
log.info("响应参数:" + response.toJSONString()); |
||||
log.info("============ 美团请求-END =============="); |
||||
return response; |
||||
|
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue