generated from root/miduo_server
190 lines
7.2 KiB
Java
190 lines
7.2 KiB
Java
package com.jmfy.controller;
|
||
|
||
import com.google.gson.Gson;
|
||
import com.google.gson.reflect.TypeToken;
|
||
import com.jmfy.dto.CPayOrder;
|
||
import com.jmfy.dto.CUserDao;
|
||
import com.jmfy.paramBean.CMail;
|
||
import com.jmfy.paramBean.PaySdkEnum;
|
||
import com.jmfy.paramBean.YouGuProp;
|
||
import com.jmfy.redisProperties.RedisUserKey;
|
||
import com.jmfy.util.JsonUtil;
|
||
import com.jmfy.util.RedisUtil;
|
||
import com.jmfy.util.ToolUtils;
|
||
import com.jmfy.util.UUIDEnum;
|
||
import org.json.JSONObject;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.lang.reflect.Type;
|
||
import java.net.URLEncoder;
|
||
import java.security.MessageDigest;
|
||
import java.security.NoSuchAlgorithmException;
|
||
import java.util.*;
|
||
|
||
@RestController
|
||
public class ItemSendController {
|
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ItemSendController.class);
|
||
|
||
private static final String SECRET_KEY = "38444756b58ff89b799291b3d8ed7ccf";
|
||
|
||
private static CUserDao cuserDao;
|
||
@Autowired
|
||
public ItemSendController(CUserDao cuserDao) {
|
||
this.cuserDao = cuserDao;
|
||
}
|
||
|
||
@RequestMapping(value = "/sendItem")
|
||
public String youguSendItem(HttpServletRequest request) throws Exception {
|
||
// 返回json
|
||
JSONObject result = new JSONObject();
|
||
result.put("data", "");
|
||
// 获取参数
|
||
HashMap<String, String> parameterMap = JsonUtil.getInstence().getParameterMap2(request);
|
||
LOGGER.info("运营返利参数:{}", parameterMap);
|
||
// 校验签名
|
||
boolean checkSign = checkSignature(parameterMap);
|
||
if (!checkSign) {
|
||
result.put("code", 1);
|
||
result.put("msg", "sign verify fail");
|
||
LOGGER.error("签名校验失败,参数:{}", parameterMap);
|
||
return result.toString();
|
||
}
|
||
// 校验订单
|
||
boolean checkOrder = checkOrder(parameterMap);
|
||
if (!checkOrder) {
|
||
result.put("code", 2);
|
||
result.put("msg", "order exist");
|
||
LOGGER.error("订单已存在,参数:{}", parameterMap);
|
||
return result.toString();
|
||
}
|
||
// 发送邮件
|
||
sendMail(parameterMap);
|
||
result.put("code", 0);
|
||
result.put("msg", "success");
|
||
return result.toString();
|
||
}
|
||
|
||
/**
|
||
* 校验订单
|
||
*/
|
||
public boolean checkOrder(HashMap<String, String> params){
|
||
int free = Integer.parseInt(params.get("free"));
|
||
if (free == 1){
|
||
LOGGER.info("免费返利,直接返回");
|
||
return true;
|
||
}
|
||
String orderId = params.get("sendid");
|
||
CPayOrder cPayOrder = cuserDao.getCpayOrderByOrderId(orderId);
|
||
if (cPayOrder != null) {
|
||
LOGGER.error("订单已存在,订单ID:{}", orderId);
|
||
return false;
|
||
}
|
||
|
||
String roleid = params.get("roleid");
|
||
String nonce = params.get("nonce");
|
||
|
||
CPayOrder payOrder = new CPayOrder();
|
||
payOrder.setOrderId(orderId);
|
||
payOrder.setUserId(roleid);
|
||
payOrder.setServerId(Integer.parseInt(params.get("sid")));
|
||
payOrder.setDelivery_time(ToolUtils.getTimeStamp(Integer.parseInt(nonce) * 1000L));
|
||
payOrder.setAmount(params.get("amount"));
|
||
payOrder.setPaySdk(PaySdkEnum.REBATES.getName());
|
||
payOrder.setGoodsId("0");
|
||
payOrder.setCc_id("0");
|
||
payOrder.setPlatform("0");
|
||
|
||
RedisUtil.getInstence().putMapEntry(RedisUserKey.REBATES_ORDER, roleid, orderId, payOrder, 0);
|
||
// cuserDao.addCpayOrder(payOrder);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 发送邮件
|
||
*/
|
||
public void sendMail(HashMap<String, String> params) {
|
||
String title = params.get("title");
|
||
String content = params.get("content");
|
||
String uid = params.get("roleid");
|
||
int time = (int) (System.currentTimeMillis() / 1000);
|
||
String props = params.get("props");
|
||
Gson gson = new Gson();
|
||
Type listType = new TypeToken<List<YouGuProp>>() {}.getType();
|
||
List<YouGuProp> list = gson.fromJson(props, listType);
|
||
String reward = ToolUtils.getMailReward(list);
|
||
sendMailToRedis(Integer.parseInt(uid), title, content, reward, time, 2592000);
|
||
}
|
||
|
||
public static void sendMailToRedis(int uid, String title, String content, String reward, int sendTime, int mailEffectiveTime){
|
||
CMail mail = new CMail(uid,title,content,reward,sendTime, mailEffectiveTime,"rebate", 4);
|
||
String key = RedisUserKey.MAIL_REWARD_MARK + ":" + uid;
|
||
String timeKey = String.valueOf(System.currentTimeMillis() + UUIDEnum.MAIL.getValue());
|
||
// 有新邮件就增加30天的有效时间
|
||
RedisUtil.getInstence().putMapEntry(RedisUserKey.MAIL_REWARD_MARK, String.valueOf(uid), timeKey, mail, mailEffectiveTime);
|
||
LOGGER.info("redis返利邮件标记:key:{}, time:{}, mail:{}",key,timeKey,mail);
|
||
}
|
||
|
||
public boolean checkSignature(Map<String, String> map) {
|
||
HashMap<String, String> params = new HashMap<>(map);
|
||
String sign = params.remove("sign");
|
||
// 1. 生成签名
|
||
String signature = generateSignature(params, SECRET_KEY);
|
||
LOGGER.info("返利发放道具验证签名,签名:{}, 生成的签名:{}", sign, signature);
|
||
if (signature == null || signature.isEmpty()) {
|
||
return false;
|
||
}
|
||
// 2. 判断签名是否正确
|
||
return signature.equalsIgnoreCase(sign);
|
||
}
|
||
|
||
public String generateSignature(Map<String, String> params, String secretKey) {
|
||
// 1. 将参数按键名进行升序排列
|
||
TreeMap<String, String> sortedParams = new TreeMap<>(params);
|
||
|
||
// 2. 拼接参数
|
||
StringBuilder queryStringBuilder = new StringBuilder();
|
||
for (Map.Entry<String, String> entry : sortedParams.entrySet()) {
|
||
if (queryStringBuilder.length() > 0) {
|
||
queryStringBuilder.append('&');
|
||
}
|
||
queryStringBuilder.append(entry.getKey()).append('=').append(urlEncode(entry.getValue())); // 手动进行 URL 编码
|
||
}
|
||
|
||
// 3. 拼接游戏密钥
|
||
queryStringBuilder.append('&').append(secretKey);
|
||
|
||
// 4. MD5 加密
|
||
String queryString = queryStringBuilder.toString();
|
||
String signature = null;
|
||
try {
|
||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||
byte[] bytes = md.digest(queryString.getBytes("UTF-8"));
|
||
StringBuilder resultStringBuilder = new StringBuilder();
|
||
for (byte b : bytes) {
|
||
resultStringBuilder.append(String.format("%02x", b));
|
||
}
|
||
signature = resultStringBuilder.toString();
|
||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
return signature;
|
||
}
|
||
|
||
private String urlEncode(String value) {
|
||
try {
|
||
return URLEncoder.encode(value, "UTF-8");
|
||
} catch (UnsupportedEncodingException e) {
|
||
e.printStackTrace();
|
||
return value;
|
||
}
|
||
}
|
||
}
|