generated from root/miduo_server
312 lines
14 KiB
Java
312 lines
14 KiB
Java
package com.jmfy.controller;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.jmfy.Application;
|
|
import com.jmfy.dao.MongoConnectDao;
|
|
import com.jmfy.dao.PayDao;
|
|
import com.jmfy.model.CoreUserInfo;
|
|
import com.jmfy.model.ServerInfo;
|
|
import com.jmfy.model.User;
|
|
import com.jmfy.model.sdk.RegisterUser;
|
|
import com.jmfy.modelVo.PayVo;
|
|
import com.jmfy.redisProperties.RedisUserKey;
|
|
import com.jmfy.util.*;
|
|
import com.jmfy.util.loginRedis.TimeUtil;
|
|
import com.ljsd.jieling.thrift.idl.Result;
|
|
import com.mongodb.BasicDBObject;
|
|
import org.apache.commons.codec.binary.Base64;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
import org.springframework.data.mongodb.core.query.BasicQuery;
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.security.KeyFactory;
|
|
import java.security.PrivateKey;
|
|
import java.security.PublicKey;
|
|
import java.security.Signature;
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
import java.text.DateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
import static com.jmfy.util.MD5Util.encrypByMd5;
|
|
|
|
@RestController
|
|
public class ICanController {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ICanController.class);
|
|
private static final String DB_NAME_PREFIX = "x1_tw_game_";
|
|
|
|
@Resource
|
|
private MongoConnectDao mongoConnectDao;
|
|
@Resource
|
|
private PayDao payDao;
|
|
|
|
@PostMapping("/ican/item")
|
|
public JSONObject sendItem(String account, String serverId, String platform, String itemId, Integer count,
|
|
String roleId, String time, String verify){
|
|
LOGGER.info("ican send item, roleId:{}, serverId:{}, itemId:{}, count:{}", roleId, serverId, itemId, count);
|
|
JSONObject json = new JSONObject();
|
|
String s = account + serverId + itemId + count + time + Constant.AIKEN_SERVER_KEY;
|
|
String mySign = MD5Util.encrypByMd5(s);
|
|
if (!mySign.equals(verify)){
|
|
json.put("code", "3");
|
|
json.put("msg", "verify error");
|
|
return json;
|
|
}
|
|
MongoTemplate mongoTemplate = null;
|
|
try {
|
|
mongoTemplate = mongoConnectDao.getConnect().getMongoTemplete(Application.coreDb);
|
|
} catch (Exception e) {
|
|
LOGGER.error("get mongoTemplate error");
|
|
json.put("code", "99");
|
|
json.put("msg", "server error");
|
|
return json;
|
|
}
|
|
|
|
BasicQuery query = new BasicQuery(new BasicDBObject("uid", Integer.parseInt(roleId)));
|
|
CoreUserInfo userInfo = mongoTemplate.findOne(query, CoreUserInfo.class, "user_info");
|
|
if (userInfo == null){
|
|
LOGGER.error("user not exist, uid:{}", roleId);
|
|
json.put("code", "1");
|
|
json.put("msg", "account not exist");
|
|
return json;
|
|
}
|
|
BasicQuery query1 = new BasicQuery(new BasicDBObject("server_id", userInfo.getServerid()+""));
|
|
ServerInfo serverInfo = mongoTemplate.findOne(query1, ServerInfo.class, "server_info");
|
|
String rpcString = RedisUtil.getInstence().getObject(RedisUserKey.LOGIC_SERVER_INFO, String.valueOf(serverInfo.getRealServerId()), String.class, -1);
|
|
System.out.println("rpcString " + rpcString);
|
|
if (null == rpcString) {
|
|
LOGGER.error("serverAddress not exist");
|
|
json.put("code", "99");
|
|
json.put("msg", "server error");
|
|
return json;
|
|
}
|
|
String thriftIp = rpcString.split(":")[0];
|
|
String thriftPort = rpcString.split(":")[3];
|
|
if (thriftIp == null || thriftIp.isEmpty() || null == thriftPort || thriftPort.isEmpty()) {
|
|
LOGGER.error("serverAddress not exist");
|
|
json.put("code", "99");
|
|
json.put("msg", "server error");
|
|
return json;
|
|
} else {
|
|
StringBuilder reward = new StringBuilder();
|
|
reward.append(itemId).append("#").append(count);
|
|
String title = "艾肯道具发放";
|
|
String content = "艾肯道具发放";
|
|
Result result = null;
|
|
try {
|
|
result = RPCClient.sendMailToRole(thriftIp, thriftPort, roleId, title, content, reward.toString());
|
|
} catch (Exception e) {
|
|
LOGGER.error("addItem error, player_id:{}", roleId, e);
|
|
json.put("code", "99");
|
|
json.put("msg", "server error");
|
|
return json;
|
|
}
|
|
if (result.getResultCode() != 1) {
|
|
LOGGER.error("addItem error, player_id:{}", roleId);
|
|
json.put("code", "99");
|
|
json.put("msg", "server error");
|
|
} else {
|
|
LOGGER.info("addItem " + roleId + " is ok ");
|
|
json.put("code", "0");
|
|
json.put("msg", "");
|
|
}
|
|
}
|
|
return json;
|
|
}
|
|
|
|
@PostMapping("/ican/user")
|
|
public JSONObject queryUser(String account, String serverId, String time, String verify){
|
|
LOGGER.info("ican query user, account:{}, serverId:{}, verify:{}", account, serverId, verify);
|
|
JSONObject json = new JSONObject();
|
|
String s = account + serverId + time + Constant.AIKEN_SERVER_KEY;
|
|
String mySign = MD5Util.encrypByMd5(s);
|
|
if (!mySign.equals(verify)){
|
|
json.put("code", "3");
|
|
json.put("msg", "verify error");
|
|
return json;
|
|
}
|
|
JSONObject user = new JSONObject();
|
|
try {
|
|
user = getRoleById(account, serverId);
|
|
} catch (Exception e) {
|
|
LOGGER.error("ican query user error, account:{}, serverId:{}", account, serverId, e);
|
|
json.put("code", "99");
|
|
json.put("msg", "server error");
|
|
return json;
|
|
}
|
|
JSONArray jsonArray = new JSONArray();
|
|
jsonArray.add(user);
|
|
json.put("code", "0");
|
|
json.put("roles", jsonArray);
|
|
return json;
|
|
}
|
|
|
|
@PostMapping( "/ican/pay")//艾肯sdk
|
|
public JSONObject iCanWebOrder(@RequestParam("account") String account,
|
|
@RequestParam("serverId") String serverId,
|
|
@RequestParam("orderId") String orderId,
|
|
@RequestParam("itemId") String itemId,
|
|
@RequestParam("productId") String productId,
|
|
@RequestParam("iMoney") Integer iMoney,
|
|
@RequestParam("gMoney") Integer gMoney,
|
|
@RequestParam("roleId") String roleId,
|
|
@RequestParam("time") String time,
|
|
@RequestParam("verify") String verify,
|
|
@RequestParam("platform") String platform
|
|
) {
|
|
LOGGER.info("iCan web pay order, account:{}, serverId:{}, orderId:{}, itemId:{}, productId:{}, iMoney:{}, gMoney:{}, roleId:{}, time:{}",
|
|
account, serverId, orderId, itemId, productId, iMoney, gMoney, roleId, time);
|
|
JSONObject returnJb = new JSONObject();
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(account).append(itemId).append(orderId).append(iMoney).append(Constant.AIKEN_SERVER_KEY).append(time).append(roleId);
|
|
try {
|
|
String mySign = encrypByMd5(sb.toString());
|
|
if (!mySign.equals(verify)){
|
|
LOGGER.error("iCan order sign verify fail, orderId:{}, roleId:{}",
|
|
orderId, roleId);
|
|
returnJb.put("code", "3");
|
|
returnJb.put("msg", "verify error");
|
|
return returnJb;
|
|
}
|
|
} catch (Exception e) {
|
|
LOGGER.error("iCan order sign error, orderId:{}, roleId:{}", orderId, roleId, e);
|
|
returnJb.put("code", "3");
|
|
returnJb.put("msg", "verify error");
|
|
return returnJb;
|
|
}
|
|
|
|
MongoTemplate mongoTemplate = null;
|
|
try {
|
|
mongoTemplate = mongoConnectDao.getConnect().getMongoTemplete(Application.coreDb);
|
|
} catch (Exception e) {
|
|
LOGGER.error("get mongoTemplate error");
|
|
returnJb.put("status", "fail");
|
|
returnJb.put("code", "99");
|
|
returnJb.put("msg", "server error");
|
|
return returnJb;
|
|
}
|
|
if (!StringUtil.isEmpty(productId)){
|
|
itemId = productId;
|
|
}
|
|
BasicQuery query = new BasicQuery(new BasicDBObject("server_id", serverId));
|
|
ServerInfo serverInfo = mongoTemplate.findOne(query, ServerInfo.class, "server_info");
|
|
PayVo payVo = null;
|
|
try {
|
|
payVo = payDao.getOder(orderId);
|
|
if (payVo != null){
|
|
if (payVo.getStatus() == 1){
|
|
LOGGER.error("order already paid, orderNo:{}", orderId);
|
|
returnJb.put("status", "fail");
|
|
returnJb.put("code", "0");
|
|
returnJb.put("msg", "order duplicate");
|
|
return returnJb;
|
|
}
|
|
}else {
|
|
CoreUserInfo userInfo = mongoTemplate.findById(Integer.parseInt(roleId), CoreUserInfo.class);
|
|
payVo = new PayVo(userInfo.getOpenId(), Integer.parseInt(roleId), serverInfo.getRealServerId(), platform, itemId, TimeUtil.getTimeStamp(System.currentTimeMillis()), iMoney * 100 ,"NT");
|
|
payVo.setBillno(orderId);
|
|
mongoTemplate.insert(payVo);
|
|
}
|
|
} catch (Exception e) {
|
|
LOGGER.error("get order error, orderNo:{}", orderId, e);
|
|
returnJb.put("status", "fail");
|
|
returnJb.put("code", "99");
|
|
returnJb.put("message", "server error");
|
|
return returnJb;
|
|
}
|
|
|
|
String rpcString = RedisUtil.getInstence().getObject(RedisUserKey.LOGIC_SERVER_INFO, String.valueOf(serverInfo.getRealServerId()), String.class, -1);
|
|
System.out.println("rpcString " + rpcString);
|
|
if (null == rpcString) {
|
|
LOGGER.error("serverAddress not exist");
|
|
returnJb.put("status", "fail");
|
|
returnJb.put("code", "99");
|
|
returnJb.put("msg", "server error");
|
|
return returnJb;
|
|
}
|
|
String thriftIp = rpcString.split(":")[0];
|
|
String thriftPort = rpcString.split(":")[3];
|
|
if (thriftIp == null || thriftIp.isEmpty() || null == thriftPort || thriftPort.isEmpty()) {
|
|
LOGGER.error("serverAddress not exist");
|
|
returnJb.put("status", "fail");
|
|
returnJb.put("code", "99");
|
|
returnJb.put("msg", "server error");
|
|
} else {
|
|
Result result = null;
|
|
try {
|
|
result = RPCClient.deliveryRecharge(thriftIp, thriftPort, Integer.parseInt(roleId), itemId, "", orderId, System.currentTimeMillis(), iMoney * 100, platform);
|
|
} catch (Exception e) {
|
|
LOGGER.error("pay error, orderNo:{}", orderId, e);
|
|
returnJb.put("code", "99");
|
|
returnJb.put("msg", "server error");
|
|
return returnJb;
|
|
}
|
|
|
|
if (result.getResultCode() == 1){
|
|
payVo.setStatus(1);
|
|
try {
|
|
payDao.upOder(payVo);
|
|
} catch (Exception e) {
|
|
LOGGER.error("update order status error, orderNo:{}", orderId, e);
|
|
}
|
|
LOGGER.info("orderNo:{} pay success", orderId);
|
|
returnJb.put("status", "ok");
|
|
returnJb.put("code", "0");
|
|
returnJb.put("msg", "success");
|
|
}else {
|
|
LOGGER.error("orderNo:{} pay fail", orderId);
|
|
returnJb.put("status", "fail");
|
|
returnJb.put("code", "99");
|
|
returnJb.put("msg", "server error");
|
|
}
|
|
}
|
|
return returnJb;
|
|
}
|
|
|
|
|
|
private JSONObject getRoleById(String account, String serverId) throws Exception {
|
|
MongoTemplate mongoTemplate = mongoConnectDao.getConnect().getMongoTemplete(Application.coreDb);
|
|
RegisterUser registerUser = mongoTemplate.findById(account, RegisterUser.class);
|
|
String[] split = serverId.split("Y");
|
|
BasicDBObject basicDBObject = new BasicDBObject();
|
|
basicDBObject.put("openId", registerUser.getOpenId());
|
|
basicDBObject.put("area", Integer.parseInt(split[1]));
|
|
basicDBObject.put("serverId", split[0]);
|
|
CoreUserInfo userInfo = mongoTemplate.findOne(new BasicQuery(basicDBObject), CoreUserInfo.class, "user_info");
|
|
|
|
BasicQuery query = new BasicQuery(new BasicDBObject("server_id", userInfo.getServerid()+""));
|
|
ServerInfo serverInfo = mongoTemplate.findOne(query, ServerInfo.class, "server_info");
|
|
MongoTemplate gameMongoTemplate = mongoConnectDao.getConnect().getMongoTemplete(DB_NAME_PREFIX + serverInfo.getRealServerId());
|
|
User user = gameMongoTemplate.findById(userInfo.getUid(), User.class, "user");
|
|
if (user == null){
|
|
return new JSONObject();
|
|
}
|
|
return getJsonUser(user);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
String s = "10002Y3";
|
|
String[] ys = s.split("Y");
|
|
System.out.println(Arrays.toString(ys));
|
|
}
|
|
|
|
private JSONObject getJsonUser(User user) {
|
|
JSONObject jsonObject = new JSONObject();
|
|
jsonObject.put("roleId", user.getId());
|
|
jsonObject.put("roleName", user.getPlayerManager().getNickName());
|
|
jsonObject.put("level", user.getPlayerManager().getLevel());
|
|
jsonObject.put("gMoneyTotal", user.getPlayerManager().getRechargeInfo().getSaveAmt());
|
|
jsonObject.put("createTime", user.getPlayerManager().getCreateTime());
|
|
return jsonObject;
|
|
}
|
|
}
|