admin
2021-11-19 7511509c68bd2892aad48a0612d497387660214d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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());
        }
    }
 
 
}