admin
2021-03-26 351b317c56487676b4f5a60b5bc3710a383d7a7b
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
package com.yeshi.buwan.controller.api;
 
import com.google.gson.*;
import com.yeshi.buwan.domain.system.SystemConfig;
import com.yeshi.buwan.domain.user.LoginUser;
import com.yeshi.buwan.domain.vip.UserVIPInfo;
import com.yeshi.buwan.domain.vip.VIPOrderRecord;
import com.yeshi.buwan.domain.vip.VIPPrice;
import com.yeshi.buwan.domain.vip.VIPPriceType;
import com.yeshi.buwan.exception.vip.VIPException;
import com.yeshi.buwan.service.inter.LoginUserService;
import com.yeshi.buwan.service.inter.system.SystemConfigService;
import com.yeshi.buwan.service.inter.vip.VIPPriceService;
import com.yeshi.buwan.service.inter.vip.VIPService;
import com.yeshi.buwan.util.*;
import com.yeshi.buwan.util.user.VipUtil;
import com.yeshi.buwan.util.vip.VIPOrderUtil;
import com.yeshi.buwan.vo.AcceptData;
import com.yeshi.buwan.vo.client.user.UserInfoVO;
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.annotation.RequestSerializableByKey;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
@Controller
@RequestMapping("api/v2/vip")
public class VIPController {
 
    Logger logger = LoggerFactory.getLogger(VIPController.class);
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private VIPService vipService;
 
    @Resource
    private LoginUserService loginUserService;
 
    @Resource
    private VIPPriceService vipPriceService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
 
    @RequestMapping("getVIPPriceList")
    @ResponseBody
    public String getVIPPriceList(AcceptData acceptData, String loginUid) {
        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 (!StringUtil.isNullOrEmpty(loginUid)) {
            LoginUser user = loginUserService.getLoginUser(loginUid);
            if (user == null) {
                return JsonUtilV2.loadFalseJson("用户不存在");
            }
            UserVIPInfo vipInfo = vipService.getVIPInfo(loginUid);
            UserInfoVO userInfoVO = new UserInfoVO();
            userInfoVO.setId(user.getId());
            userInfoVO.setNickName(user.getName());
            userInfoVO.setPortrait(user.getPortrait());
            if (StringUtil.isNullOrEmpty(userInfoVO.getPortrait())) {
                String portrait = systemConfigService.getConfigValueByKeyCache("default_portrait");
                userInfoVO.setPortrait(portrait);
            }
 
            if (vipInfo != null && vipInfo.getExpireDate() != null)
                userInfoVO.setVipExpireTime(vipInfo.getExpireDate().getTime());
            root.put("user", new Gson().toJson(userInfoVO));
        }
 
        List<VIPPrice> vipPriceList = vipPriceService.listValidPrice();
        root.put("list", gson.toJson(vipPriceList));
        return JsonUtilV2.loadTrueJson(root.toString());
    }
 
 
    @RequestMapping("getVIPOrderList")
    @ResponseBody
    public String getVIPOrderList(AcceptData acceptData, String loginUid, int page) {
        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<VIPOrderRecord> list = vipService.listOrderRecord(loginUid, null, page, Constant.pageCount);
        for (VIPOrderRecord record : list) {
            record.setIpInfo(null);
            record.setUpdateTime(null);
        }
        long count = vipService.countOrderRecord(loginUid, null);
        root.put("list", gson.toJson(list));
        root.put("count", count);
        return JsonUtilV2.loadTrueJson(root.toString());
    }
 
    /**
     * 生成订单
     *
     * @param acceptData
     * @param loginUid
     * @return
     */
    @RequestMapping("createOrder")
    @ResponseBody
    public String createOrder(AcceptData acceptData, HttpServletRequest request, String loginUid, String priceId, int payWay) {
 
        if (StringUtil.isNullOrEmpty(loginUid)) {
            return JsonUtilV2.loadFalseJson("用户未登录");
        }
 
        LoginUser user = loginUserService.getLoginUser(loginUid);
        if (user == null) {
            return JsonUtilV2.loadFalseJson("用户不存在");
        }
 
        if (StringUtil.isNullOrEmpty(user.getPhone())) {
            return JsonUtilV2.loadFalseJson(10001, "请绑定电话号码");
        }
 
 
        String ip = IPUtil.getRemotIP(request);
        VIPPrice vipPrice = vipPriceService.selectByPrimaryKey(priceId);
        if (vipPrice == null) {
            return JsonUtilV2.loadFalseJson("套餐不存在");
        }
 
        VIPOrderRecord record = new VIPOrderRecord();
        record.setUid(loginUid);
        record.setType(vipPrice.getType());
        record.setMoney(vipPrice.getActualPrice());
        record.setPayWay(payWay);
        record.setState(VIPOrderRecord.STATE_NOT_PAY);
        record.setIpInfo(IPUtil.getRemotIP(request) + ":" + IPUtil.getRemotePort(request));
        try {
            vipService.addVIPRecord(record);
        } catch (VIPException e) {
            logger.error("生成订单出错", e);
            return JsonUtilV2.loadFalseJson("生成订单出错");
        }
 
        String orderNo = VIPOrderUtil.getOutOrderNo(record.getId());
        switch (payWay) {
            case VIPOrderRecord
                    .PAY_WAY_ALIPAY: {
                //生成支付宝支付订单
                String form = VipUtil.getVipChargeAlipayForm(record.getId(), orderNo, record.getMoney());
                //暂存2分钟
                String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
                redisManager.cacheCommonString(id, form, 120);
                org.json.JSONObject data = new org.json.JSONObject();
                data.put("payUrl", Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + id);
                data.put("payWay", payWay);
                return JsonUtilV2.loadTrueJson(data.toString());
            }
 
 
            case VIPOrderRecord
                    .PAY_WAY_WX: {
                //生成微信支付订单
                try {
                    String payUrl = VipUtil.createWXOrder(record.getId(), ip, orderNo, vipPrice.getActualPrice(), "影视大全VIP-" + vipPrice.getType().getName());
                    org.json.JSONObject data = new org.json.JSONObject();
                    data.put("payUrl", payUrl);
                    data.put("payWay", payWay);
                    return JsonUtilV2.loadTrueJson(data.toString());
                } catch (Exception e) {
                    logger.error("生成微信支付订单出错", e);
                    return JsonUtilV2.loadFalseJson("生成微信支付订单出错");
                }
            }
        }
 
        return JsonUtilV2.loadFalseJson("请选择支付方式");
    }
 
    /**
     * 检查是否支付
     *
     * @param acceptData
     * @param loginUid
     * @param id
     * @return
     */
    @RequestMapping("checkPay")
    @ResponseBody
    @RequestSerializableByKey(key = "'vip-checkPay-'+#id")
    public String checkPay(AcceptData acceptData, String loginUid, String id) {
        VIPOrderRecord record = vipService.getOrderRecord(id);
        if (record == null || !record.getUid().equalsIgnoreCase(loginUid)) {
            return JsonUtilV2.loadFalseJson("记录不存在/不是您的订单");
        }
 
        record = vipService.checkOrderPayState(id);
        //未支付
        if (record != null && record.getState() != VIPOrderRecord.STATE_PAY) {
            return JsonUtilV2.loadFalseJson(1, "支付未完成");
        }
 
        JSONObject data = new JSONObject();
        data.put("money", record.getPayMoney() == null ? record.getMoney() : record.getPayMoney());
 
        return JsonUtilV2.loadTrueJson(data.toString());
    }
 
 
}