yujian
2019-11-29 444da68b958e53c7fa122db62723fce25368ee1d
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package com.yeshi.fanli.controller.client.v2;
 
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.SMSHistory;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPInfo;
import com.yeshi.fanli.entity.push.DeviceActive;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.exception.user.UserAccountException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarUserService;
import com.yeshi.fanli.service.inter.money.UserMoneyService;
import com.yeshi.fanli.service.inter.order.LostOrderService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.push.DeviceActiveService;
import com.yeshi.fanli.service.inter.push.DeviceTokenHWService;
import com.yeshi.fanli.service.inter.push.DeviceTokenOPPOService;
import com.yeshi.fanli.service.inter.push.DeviceTokenVIVOService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.user.BindRemindService;
import com.yeshi.fanli.service.inter.user.ForbiddenUserIdentifyCodeService;
import com.yeshi.fanli.service.inter.user.ShamUserService;
import com.yeshi.fanli.service.inter.user.SpreadUserImgService;
import com.yeshi.fanli.service.inter.user.TBPidService;
import com.yeshi.fanli.service.inter.user.UserAccountBindingHistoryService;
import com.yeshi.fanli.service.inter.user.UserAccountService;
import com.yeshi.fanli.service.inter.user.UserInfoDeleteRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoModifyRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserShareGoodsRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.inter.user.integral.IntegralDetailService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
import com.yeshi.fanli.service.inter.user.vip.UserVipConfigService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.util.annotation.RequestSerializableByKey;
import com.yeshi.fanli.vo.user.UserVipRateVO;
 
import net.sf.json.JSONObject;
 
/**
 * 账户系统
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/v2/user")
public class UserAccountControllerV2 {
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private BusinessSystemService businessSystemService;
 
    @Resource
    private ShamUserService shamUserService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private LostOrderService lostOrderService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private UserAccountService userAccountService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private TBPidService tbPidService;
 
    @Resource
    private DeviceTokenHWService deviceTokenHWService;
 
    @Resource
    private DeviceTokenOPPOService deviceTokenOPPOService;
 
    @Resource
    private DeviceTokenVIVOService deviceTokenVIVOService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private SpreadUserImgService spreadUserImgService;
 
    @Resource
    private UserShareGoodsRecordService userShareGoodsRecordService;
 
    @Resource
    private ForbiddenUserIdentifyCodeService forbiddenUserIdentifyCodeService;
 
    @Resource
    private UserSystemCouponService userSystemCouponService;
 
    @Resource
    private UserMoneyService userMoneyService;
 
    @Resource
    private UserAccountBindingHistoryService userAccountBindingHistoryService;
 
    @Resource
    private HomeNavbarUserService homeNavbarUserService;
 
    @Resource
    private UserInfoDeleteRecordService userInfoDeleteRecordService;
 
    @Resource
    private UserTaoLiJinOriginService userTaoLiJinOriginService;
 
    @Resource
    private UserInfoModifyRecordService userInfoModifyRecordService;
 
    @Resource
    private DeviceActiveService deviceActiveService;
 
    @Resource
    private BindRemindService bindRemindService;
    
    @Resource
    private UserVIPInfoService userVIPInfoService;
    
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
    
    @Resource
    private IntegralDetailService integralDetailService;
    
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
 
    @Resource
    private ConfigService configService;
    
    @Resource
    private UserVipConfigService userVipConfigService;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
    
    /**
     * 新版登录 V1.5.3
     * 
     * @param acceptData
     * @param vcode
     * @param phone
     * @param code
     * @param loginType
     *            登录方式: 1-手机登录 2-微信登录
     * @param request
     * @param out
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public void login(AcceptData acceptData, int loginType, String vcode, String phone, String code,
            HttpServletRequest request, PrintWriter out) {
        try {
            BusinessSystem system = businessSystemService.getBusinessSystemCache(acceptData.getPlatform(),
                    acceptData.getPackages());
            if (system == null) {
                out.print(JsonUtil.loadFalseResult("系统不存在"));
                return;
            }
 
            UserInfo userInfo = null;
            // 手机登录
            if (loginType == 1) {
                userInfo = userAccountService.loginPhone(request, loginType, vcode, phone, system.getAppid());
            }
 
            // 微信登录
            if (loginType == 2) {
                userInfo = userAccountService.loginWeiXinNew(request, acceptData, loginType, code, system.getAppid());
            }
 
            if (userInfo == null) {
                out.print(JsonUtil.loadFalseResult("登录失败"));
            } else {
                JSONObject data = new JSONObject();
                data.put("userInfo", UserUtil.filterForClientUser(userInfo));
                out.print(JsonUtil.loadTrueResult(data));
 
                final UserInfo uuser = userInfo;
                ThreadUtil.run(new Runnable() {
                    public void run() {
                        String device = acceptData.getDevice();
                        try {
                            // 同步自定义导航
                            homeNavbarUserService.synchroDeviceToUser(uuser.getId(), device);
                        } catch (Exception e) {
                            LogHelper.errorDetailInfo(e);
                        }
 
                        // 绑定oppo,VIVO推送
                        DeviceActive active = deviceActiveService.getFirstActiveInfo(acceptData.getDevice());
                        if (active != null) {
                            deviceTokenOPPOService.bindUid(uuser.getId(), active.getId());
                            deviceTokenVIVOService.bindUid(uuser.getId(), active.getId());
                        }
                    }
                });
            }
 
        } catch (UserAccountException e) {
            int errorCode = e.getCode();
            if (errorCode == 101) { // 需要绑定手机号
                JSONObject data = new JSONObject();
                data.put("key", e.getMsg());
                JSONObject object = new JSONObject();
                object.put("code", errorCode);
                object.put("data", data);
                out.print(object.toString());
            } else {
                out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
                try {
                    LogHelper.cookieLog("登录出错:" + e.getCode() + "-" + e.getMessage());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
 
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult("登录失败"));
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
 
    /**
     * 绑定电话号码 V2.0.1 微信登录
     * 
     * @param acceptData
     * @param vcode
     * @param phone
     * @param out
     */
    @RequestSerializableByKey(key = "'bindPhoneLogin-'+#phone")
    @RequestMapping(value = "bindPhoneLogin")
    public void bindPhoneLogin(AcceptData acceptData, String vcode, String phone, String key,
            HttpServletRequest request, PrintWriter out) {
        BusinessSystem system = businessSystemService.getBusinessSystemCache(acceptData.getPlatform(),
                acceptData.getPackages());
        if (system == null) {
            out.print(JsonUtil.loadFalseResult("系统不存在"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(vcode)) {
            out.print(JsonUtil.loadFalseResult("验证码不能为空"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(phone)) {
            out.print(JsonUtil.loadFalseResult("电话号码不能为空"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(key)) {
            out.print(JsonUtil.loadFalseResult("key值不能为空"));
            return;
        }
 
        phone = phone.replaceAll(" ", "");
 
        String oldVCode = redisManager.getSMSVCode(phone, SMSHistory.TYPE_BIND);
        if (Constant.IS_OUTNET) {
            if (!vcode.equalsIgnoreCase(oldVCode)) {
                out.print(JsonUtil.loadFalseResult(9001, "验证码错误,重新输入"));
                return;
            }
            redisManager.clearSMSFrequencyLimit(phone, SMSHistory.TYPE_BIND);
        }
 
        try {
            UserInfo userInfo = userAccountService.bindPhoneToLogin(phone, key, system.getAppid(), request);
 
            JSONObject data = new JSONObject();
            data.put("userInfo", UserUtil.filterForClientUser(userInfo));
            out.print(JsonUtil.loadTrueResult(data));
        } catch (UserAccountException e) {
            out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult(1, "绑定失败"));
        }
    }
 
    /**
     * 绑定电话号码 V2.0.1 微信登录
     * 
     * @param acceptData
     * @param vcode
     * @param phone
     * @param out
     */
    @RequestMapping(value = "recordBind")
    public void recordBind(AcceptData acceptData, Long uid, PrintWriter out) {
        if (uid == null || uid <= 0) {
            out.print(JsonUtil.loadFalseResult("用户id为空"));
            return;
        }
 
        bindRemindService.addBindPhone(uid);
        out.print(JsonUtil.loadTrueResult("记录成功"));
    }
 
    
    /**
     * 获取vip信息
     * @param acceptData
     * @param uid
     * @param out
     */
    @RequestMapping(value = "getVIPInfo")
    public void getVIPInfo(AcceptData acceptData, Long uid, PrintWriter out) {
        if (uid == null || uid <= 0) {
            out.print(JsonUtil.loadFalseResult("用户id为空"));
            return;
        }
        
        UserInfo userInfo = userInfoService.selectByPKey(uid);
        if (userInfo == null) {
            out.print(JsonUtil.loadFalseResult("该用户不存在"));
            return;
        }
        
        UserVipRateVO vo = new UserVipRateVO();
        vo.setId(uid);
        vo.setNickName(userInfo.getNickName());
        vo.setPortrait(userInfo.getPortrait());
        vo.setHelpLink(userVipConfigService.getValueByKey("help_link_vip_h5"));
    
        // 省钱 -自购产生返利   
        BigDecimal finishMoney = hongBaoV2CountService.getRewardMoneyBySelf(uid);
        // 分享赚
        BigDecimal rewardShare = hongBaoV2CountService.getRewardMoneyByShare(uid);
        // 邀请赚
        BigDecimal rewardInvite = hongBaoV2CountService.getRewardMoneyByInvite(uid);        
                
        UserVIPInfo userVIPInfo = userVIPInfoService.selectByUid(uid);
        if (userVIPInfo != null && userVIPInfo.getState() != null 
                && userVIPInfo.getState() == UserVIPInfo.STATE_SUCCESS) { // VIP
            SimpleDateFormat format = new SimpleDateFormat("yyyy.mm.dd");
            vo.setVip(true);
            vo.setSuccessTime(format.format(userVIPInfo.getSuccessTime()));
            vo.setConserveMoney(finishMoney.setScale(2));
            vo.setEarnMoney(MoneyBigDecimalUtil.add(rewardShare, rewardInvite).setScale(2));
        } else {
            BigDecimal rate1 = new BigDecimal(5);
            BigDecimal rate2 = new BigDecimal(12);
            vo.setConserveMoney(MoneyBigDecimalUtil.mul2(finishMoney,  MoneyBigDecimalUtil.div(rate1, rate2)).setScale(2));
            vo.setEarnMoney( MoneyBigDecimalUtil.mul2(rewardShare,  MoneyBigDecimalUtil.div(rate1, rate2)).setScale(2));
        }
        vo.setFinishMoney(finishMoney.setScale(2));
        
        // 会员规则执行时间
        String limtDate = userVipConfigService.getValueByKey("vip_execute_time");
        Date executeDate = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
            executeDate = format.parse(limtDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Integer> list = new ArrayList<Integer>();
        list.add(BanLiShopOrder.STATE_SUCCESS);
        BigDecimal payMoney = new BigDecimal(userVipConfigService.getValueByKey("require_order_pay"));
        
        vo.setFinishGoldCoin(integralDetailService.getCumulativeMoney(uid));
        vo.setFinishTeam(hongBaoV2CountService.countValidOrderTeamUserByUid(uid, executeDate.getTime(), payMoney));
        vo.setFinishHongBao(banLiShopOrderService.countByUidAndState(uid, list));
        
        UserInfoExtra extra = userInfoExtraService.getUserInfoExtra(uid);
        if (extra !=null && (extra.getFirstLoginTime() == null || extra.getFirstLoginTime().getTime() < executeDate.getTime())) {
            vo.setLimitTeam(Long.parseLong(userVipConfigService.getValueByKey("require_invite_num_old_user")));
        } else {
            vo.setLimitTeam(Long.parseLong(userVipConfigService.getValueByKey("require_invite_num_new_user")));
        }
        vo.setLimitHongBao(Long.parseLong(userVipConfigService.getValueByKey("require_shop_buy")));
        vo.setLimitMoney(new BigDecimal(userVipConfigService.getValueByKey("require_fan_money")));
        vo.setLimitgoldCoin(new BigDecimal(userVipConfigService.getValueByKey("require_gold_coin")));
        out.print(JsonUtil.loadTrueResult(vo));
    }
}