package com.yeshi.location.app.controller.client.api;
|
|
import com.google.gson.*;
|
import com.yeshi.location.app.dto.vip.PayWayInfoDTO;
|
import com.yeshi.location.app.entity.user.UserInfo;
|
import com.yeshi.location.app.entity.vip.*;
|
import com.yeshi.location.app.exception.goldcorn.GoldCornException;
|
import com.yeshi.location.app.exception.vip.OrderException;
|
import com.yeshi.location.app.exception.vip.PayException;
|
import com.yeshi.location.app.exception.vip.VIPException;
|
import com.yeshi.location.app.service.inter.user.UserInfoService;
|
import com.yeshi.location.app.service.inter.vip.OrderService;
|
import com.yeshi.location.app.service.inter.vip.VIPPriceService;
|
import com.yeshi.location.app.service.inter.vip.VIPService;
|
import com.yeshi.location.app.utils.Constant;
|
import com.yeshi.location.app.vo.AcceptData;
|
import com.yeshi.location.app.vo.user.UserInfoVO;
|
import com.yeshi.location.app.vo.vip.OrderInfoVO;
|
import net.sf.json.JSONObject;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.yeshi.utils.IPUtil;
|
import org.yeshi.utils.JsonUtil;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.TimeUtil;
|
import org.yeshi.utils.annotation.RequestSerializableByKey;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.lang.reflect.Type;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
@Controller
|
@RequestMapping("api/v1/vip")
|
public class VIPController {
|
|
Logger logger = LoggerFactory.getLogger(VIPController.class);
|
|
@Resource
|
private VIPService vipService;
|
|
@Resource
|
private OrderService orderService;
|
|
@Resource
|
private UserInfoService userInfoService;
|
|
@Resource
|
private VIPPriceService vipPriceService;
|
|
|
@RequestMapping("getVIPPriceList")
|
@ResponseBody
|
public String getVIPPriceList(AcceptData acceptData, Long uid) {
|
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(VIPPriceType.class, new JsonSerializer<VIPPriceType>() {
|
@Override
|
public JsonElement serialize(VIPPriceType value, Type theType, JsonSerializationContext context) {
|
if (value == null) {
|
return new JsonPrimitive("");
|
} else {
|
return new JsonPrimitive(value.getName());
|
}
|
}
|
}).create();
|
JSONObject root = new JSONObject();
|
if (uid != null) {
|
UserInfo user = userInfoService.getAvaiableUser(uid);
|
if (user == null) {
|
return JsonUtil.loadFalseResult("用户不存在");
|
}
|
UserVIPInfo vipInfo = vipService.getVIPInfo(uid);
|
UserInfoVO userInfoVO = new UserInfoVO();
|
userInfoVO.setId(user.getId() + "");
|
userInfoVO.setNickName(user.getNickName());
|
userInfoVO.setPortrait(user.getPortrait());
|
|
if (vipInfo != null && vipInfo.getExpireDate() != null) {
|
userInfoVO.setVipExpireTime(vipInfo.getExpireDate().getTime());
|
}
|
root.put("user", new Gson().toJson(userInfoVO));
|
}
|
|
List<VIPPrice> vipPriceList = vipPriceService.listValidPrice(acceptData.getSystem());
|
root.put("list", gson.toJson(vipPriceList));
|
return JsonUtil.loadTrueResult(root.toString());
|
}
|
|
|
@RequestMapping("getOrderList")
|
@ResponseBody
|
public String getOrderList(AcceptData acceptData, Long uid, String type, int page) {
|
|
if (uid == null) {
|
return JsonUtil.loadFalseResult("尚未登录");
|
}
|
|
Gson gson = new GsonBuilder().registerTypeAdapter(VIPPriceType.class, new JsonSerializer<VIPPriceType>() {
|
@Override
|
public JsonElement serialize(VIPPriceType value, Type theType, JsonSerializationContext context) {
|
if (value == null) {
|
return new JsonPrimitive("");
|
} else {
|
return new JsonPrimitive(value.getName());
|
}
|
}
|
}).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
|
@Override
|
public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
|
if (value == null) {
|
return new JsonPrimitive("");
|
} else {
|
return new JsonPrimitive(TimeUtil.getGernalTime(value.getTime(), "yyyy.MM.dd HH:mm"));
|
}
|
}
|
}).create();
|
JSONObject root = new JSONObject();
|
|
List<OrderRecord> list = orderService.listOrderRecord(uid, type == null ? null : OrderType.valueOf(type), null, page, Constant.PAGE_SIZE);
|
|
List<OrderInfoVO> voList = new ArrayList<>();
|
for (OrderRecord record : list) {
|
voList.add(OrderInfoVO.create(record));
|
}
|
long count = orderService.countOrderRecord(uid, type == null ? null : OrderType.valueOf(type), null);
|
root.put("list", gson.toJson(voList));
|
root.put("count", count);
|
return JsonUtil.loadTrueResult(root.toString());
|
}
|
|
/**
|
* 生成订单
|
*
|
* @param acceptData
|
* @param uid
|
* @return
|
*/
|
@RequestMapping("createOrder")
|
@ResponseBody
|
public String createOrder(AcceptData acceptData, HttpServletRequest request, Long uid, String priceId, Integer goldCorn, int payWay) {
|
|
if (uid == null) {
|
return JsonUtil.loadFalseResult("用户未登录");
|
}
|
|
UserInfo user = userInfoService.getAvaiableUser(uid);
|
if (user == null) {
|
return JsonUtil.loadFalseResult("用户不存在");
|
}
|
|
if (StringUtil.isNullOrEmpty(user.getPhone()) && payWay != OrderRecord.PAY_WAY_IAPP) {
|
return JsonUtil.loadFalseResult(10001, "请绑定电话号码");
|
}
|
|
|
if (goldCorn == null) {
|
goldCorn = 0;
|
}
|
OrderType orderType = OrderType.vip;
|
OrderRecord record = new OrderRecord();
|
VIPPrice vipPrice = null;
|
vipPrice = vipPriceService.selectByPrimaryKey(priceId);
|
if (vipPrice == null) {
|
return JsonUtil.loadFalseResult("套餐不存在");
|
}
|
record.setUid(uid);
|
if (vipPrice != null) {
|
record.setType(vipPrice.getType());
|
record.setMoney(vipPrice.getActualPrice());
|
}
|
record.setOrderType(orderType);
|
record.setGoldCorn(goldCorn);
|
record.setPayWay(payWay);
|
record.setState(OrderRecord.STATE_NOT_PAY);
|
record.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
|
try {
|
orderService.createOrder(record);
|
} catch (OrderException e) {
|
logger.error("生成订单出错", e);
|
return JsonUtil.loadFalseResult("生成订单出错");
|
}
|
PayWayInfoDTO payResult = null;
|
|
try {
|
payResult = orderService.payOrder(record);
|
} catch (OrderException e) {
|
return JsonUtil.loadFalseResult(e.getMessage());
|
} catch (GoldCornException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult(e.getMessage());
|
} catch (PayException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult(e.getMessage());
|
} catch (VIPException e) {
|
return JsonUtil.loadFalseResult(e.getMessage());
|
}
|
if (payResult.getPayWay() == OrderRecord.PAY_WAY_IAPP) {
|
//苹果内购
|
JSONObject root = new JSONObject();
|
root.put("orderNo", record.getId());
|
root.put("productId", vipPrice.getIosProductId());
|
return JsonUtil.loadTrueResult(root.toString());
|
} else {
|
return JsonUtil.loadTrueResult(new Gson().toJson(payResult));
|
}
|
}
|
|
|
/**
|
* 检查是否支付
|
*
|
* @param acceptData
|
* @param uid
|
* @param id
|
* @param receipt -ios支付结果receipt
|
* @return
|
*/
|
@RequestMapping("checkPay")
|
@ResponseBody
|
@RequestSerializableByKey(key = "'vip-checkPay-'+#id")
|
public String checkPay(AcceptData acceptData, Long uid, Long id, String receipt) {
|
OrderRecord record = orderService.getOrderRecord(id);
|
if (record == null || record.getUid().longValue() != uid) {
|
return JsonUtil.loadFalseResult("记录不存在/不是您的订单");
|
}
|
|
if (record.getPayWay() == OrderRecord.PAY_WAY_IAPP) {
|
try {
|
orderService.checkApplePay(record.getId(), receipt);
|
JSONObject data = new JSONObject();
|
return JsonUtil.loadTrueResult(data.toString());
|
} catch (Exception e) {
|
return JsonUtil.loadFalseResult(e.getMessage());
|
}
|
} else {
|
record = orderService.checkOrderPayState(id);
|
//未支付
|
if (record != null && record.getState() != OrderRecord.STATE_PAY) {
|
return JsonUtil.loadFalseResult(1, "支付未完成");
|
}
|
|
JSONObject data = new JSONObject();
|
data.put("money", record.getPayMoney() == null ? record.getMoney() : record.getPayMoney());
|
return JsonUtil.loadTrueResult(data.toString());
|
}
|
}
|
|
|
}
|