generated from root/miduo_server
悠谷线下返利
parent
53346aff81
commit
0c047b4641
|
@ -0,0 +1,187 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.jmfy.paramBean;
|
||||
|
||||
|
||||
public class MailCache {
|
||||
private String title;
|
||||
private String content;
|
||||
private String from;
|
||||
private String reward;
|
||||
private int time;
|
||||
private int validTime;
|
||||
|
||||
public MailCache() {
|
||||
from = "Rebates";
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public int getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(int time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getReward() {
|
||||
return reward;
|
||||
}
|
||||
|
||||
public void setReward(String reward) {
|
||||
this.reward = reward;
|
||||
}
|
||||
|
||||
public int getValidTime() {
|
||||
return validTime;
|
||||
}
|
||||
|
||||
public void setValidTime(int validTime) {
|
||||
this.validTime = validTime;
|
||||
}
|
||||
}
|
|
@ -21,7 +21,8 @@ public enum PaySdkEnum {
|
|||
ANJIU(11, "ANJIU"),
|
||||
YOUGU(12,"YOUGU"),
|
||||
FENGTI(13,"FENGTI"),
|
||||
U1GAME(14, "U1GAME")
|
||||
U1GAME(14, "U1GAME"),
|
||||
REBATES(15, "REBATES")
|
||||
;
|
||||
|
||||
private int id;
|
||||
|
|
|
@ -7,4 +7,5 @@ public class RedisUserKey {
|
|||
public final static String C_PAYORDER = "C_PAYORDER";
|
||||
public final static String CP_ORDER = "CP_ORDER";
|
||||
public final static String C_PAYORDER_MAP = "C_PAYORDER_MAP";
|
||||
public static final String READY_TO_USER_MAIL = "READY_TO_USER_MAIL";
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.jmfy.util;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ToolUtils {
|
||||
public static final long ONE_MILLSECOND = 1L; //一毫秒
|
||||
public static final long ONE_SECOND = ONE_MILLSECOND * 1000;// 一秒的毫秒数
|
||||
public static final long ONE_MINUTE = ONE_SECOND * 60;//一分的毫秒数
|
||||
public static final long ONE_HOUR = ONE_MINUTE * 60;//一时的毫秒数
|
||||
public static final long ONE_DAY = ONE_HOUR * 24;//一天的毫秒数
|
||||
|
||||
/**
|
||||
* 获取邮件奖励
|
||||
*/
|
||||
public static String getMailReward(Map<String,String> map){
|
||||
StringBuilder reward = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
reward.append(entry.getKey()).append("#").append(entry.getValue()).append("|");
|
||||
}
|
||||
if (String.valueOf(reward).endsWith("|")) {
|
||||
reward.deleteCharAt(reward.length() - 1);
|
||||
}
|
||||
return reward.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间戳
|
||||
*/
|
||||
public static String getTimeStamp(Long time) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
return formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue