generated from root/miduo_server
188 lines
6.8 KiB
Java
188 lines
6.8 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.MailCache;
|
||
import com.jmfy.paramBean.PaySdkEnum;
|
||
import com.jmfy.redisProperties.RedisUserKey;
|
||
import com.jmfy.util.JsonUtil;
|
||
import com.jmfy.util.RedisUtil;
|
||
import com.jmfy.util.ToolUtils;
|
||
import com.mongodb.util.JSON;
|
||
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.IOException;
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.lang.reflect.Type;
|
||
import java.net.URLEncoder;
|
||
import java.security.MessageDigest;
|
||
import java.security.NoSuchAlgorithmException;
|
||
import java.text.DateFormat;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
import java.util.TreeMap;
|
||
|
||
@RestController
|
||
public class ItemSendController {
|
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ItemSendController.class);
|
||
|
||
private static final String SECRET_KEY = "fda6e5e13e75285dd0c8e8636a85704b";
|
||
|
||
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;
|
||
}
|
||
|
||
CPayOrder payOrder = new CPayOrder();
|
||
payOrder.setOrderId(orderId);
|
||
payOrder.setServerId(Integer.parseInt(params.get("sid")));
|
||
payOrder.setDelivery_time(ToolUtils.getTimeStamp(Integer.parseInt(params.get("time")) * 1000L));
|
||
payOrder.setUserId(params.get("roleid"));
|
||
payOrder.setAmount(params.get("amount"));
|
||
payOrder.setGoodsId("0");
|
||
payOrder.setCc_id("0");
|
||
payOrder.setPaySdk(PaySdkEnum.REBATES.getName());
|
||
payOrder.setPlatform("0");
|
||
cuserDao.addCpayOrder(payOrder);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 发送邮件
|
||
*/
|
||
public void sendMail(HashMap<String, String> params) {
|
||
String title = params.get("title");
|
||
String content = params.get("content");
|
||
String serverId = params.get("sid");
|
||
String uid = params.get("roleid");
|
||
|
||
Gson gson = new Gson();
|
||
Type type = new TypeToken<Map<String, String>>() {}.getType();
|
||
String props = params.get("props");
|
||
Map<String, String> item = gson.fromJson(props, type);
|
||
String reward = ToolUtils.getMailReward(item);
|
||
|
||
MailCache cache = new MailCache();
|
||
cache.setTitle(title);
|
||
cache.setContent(content);
|
||
cache.setReward(reward);
|
||
cache.setTime((int)System.currentTimeMillis()/1000);
|
||
cache.setValidTime((int)(ToolUtils.ONE_DAY * 30 / 1000));
|
||
LOGGER.info("返利邮件,标题:{},内容:{}, 奖励:{}, 服务器ID: {}, UID: {}", title, content, reward, serverId, uid);
|
||
RedisUtil.getInstence().putMapEntry(serverId, RedisUserKey.READY_TO_USER_MAIL, String.valueOf(uid), cache, 0);
|
||
}
|
||
|
||
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);
|
||
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;
|
||
}
|
||
}
|
||
}
|