ParkCustBindingController.java
4.92 KB
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
package com.rnt.controller;
import java.util.HashMap;
import java.util.Map;
import com.jfinal.aop.Before;
import com.rnt.commo.interceptor.BindInterceptor;
import com.rnt.commo.interceptor.CorsInterceptor;
import org.beetl.sql.core.kit.StringKit;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.jfinal.plugin.redis.Redis;
import com.rnt.commo.enums.ErrorType;
import com.rnt.model.zf.CustPerson;
import com.rnt.service.CustBindingService;
import com.rnt.service.CustPersonService;
import com.rnt.utils.MessageCodeUtil;
import com.rnt.vo.BizResult;
/**
* 用户办卡绑定手机号.<br/>
*
* Copyright: Copyright (c) 2017 zteits
*
* @ClassName: ParkCustBindingController.java
* @Description:
* @version: v1.0.0
* @author: wangfs
* @date: 2017年6月7日 上午11:00:36 Modification History: Date Author Version
* Description ---------------------------------------------------------*
* 2017年6月7日 wangfs v1.0.0 创建
*/
public class ParkCustBindingController extends Controller {
private static final Log logger = Log.getLog(ParkCustBindingController.class);
/**验证码分割符号.*/
private static final String RAND_CODE_SPLIT = "_";
private CustPersonService custPersonService = Duang.duang(CustPersonService.class);
private CustBindingService custBindingService = Duang.duang(CustBindingService.class);
/**
* 跳转用户绑定页面.<br/>
*/
@Clear(BindInterceptor.class)
public void bindingView() {
String openId = this.getPara("openId");
//需要跳转到的的URL
String target = getPara("target");
this.getRequest().setAttribute("appid", openId);
setAttr("target",target);
render("binding.html");
}
/**
* 通过手机号发送验证码.<br/>
*/
@Clear(BindInterceptor.class)
public void sendRandCode() {
String phoneNum = this.getPara("phone_number");
logger.info("[开始发送验证码,入参="+ phoneNum);
Map<String, String> map = new HashMap<String, String>();
String result = "";
try {
if (phoneNum != null && phoneNum != "") {
String value = Redis.use().get("randCode_" + phoneNum);
if(checkRedisRandCode(value)){
// 1.获取要发送的验证码
String randCode = MessageCodeUtil.createCode();
System.out.println("手机号="+phoneNum+"发送验证码为:["+randCode+"]");
// 将验证码存入缓存
Redis.use().setex("randCode_" + phoneNum, 5 * 60, randCode + RAND_CODE_SPLIT + System.currentTimeMillis());
// 发送验证码
result = MessageCodeUtil.sendSms(phoneNum, randCode);
logger.info("发送验证码结果="+JSONObject.toJSONString(result));
}
}
} catch (Exception e) {
e.printStackTrace();
}
logger.info("[结束发送验证码");
map.put("data", result);
this.renderJson(map);
}
/**
* 绑定手机号.<br/>
*/
@Clear(BindInterceptor.class)
public void bindingPhone(){
BizResult<String> bizResult = new BizResult<String>();
String phoneNum = this.getPara("phone"); //手机号
String appid = this.getPara("appid");
String randCode = this.getPara("randCode");
String value = Redis.use().get("randCode_" + phoneNum);
if(value !=null && value!=""){
String redisRandCode = value.split(RAND_CODE_SPLIT)[0];
if(redisRandCode !=null && redisRandCode.equals(randCode)){
//查询客户是否已经创建
BizResult<CustPerson> queryCustPersonResult =custPersonService.queryCustPerson(phoneNum);
BizResult<String> result = new BizResult<String>();
if(queryCustPersonResult == null || null == queryCustPersonResult.getData()||StringKit.isEmpty(queryCustPersonResult.getData().getCustId())){
//1.创建客户信息
result = custPersonService.saveCustPerson(phoneNum);
}else{
result.setData(queryCustPersonResult.getData().getCustId());
}
if(result !=null && StringKit.isNotBlank(result.getData())){
//绑定客户
BizResult<Boolean> resultForBinding = custBindingService.saveCustBinding(result.getData(), appid);
if(resultForBinding ==null || resultForBinding.getData() ==null || resultForBinding.getData() ==false){
bizResult.setErrorMessage(ErrorType.SYSTEM_ERROR, "绑定失败!");
}else{
bizResult.setData(null);//成功
}
}else{
bizResult.setErrorMessage(ErrorType.SYSTEM_ERROR, "创建个人客户失败!");
}
}else{//不相等
bizResult.setErrorMessage(ErrorType.RAND_CODE_ERROE, "验证码验证失败!");
}
}
this.renderJson(JSONObject.toJSONString(bizResult));
}
/**
* 判断缓存中有验证码,有:距离上次<60秒的话,不容许发送验证码.<br/>
* @param value
* @return
*/
private Boolean checkRedisRandCode(String value){
if(StringKit.isEmpty(value)){
return true;
}else{
String[] str = value.split(RAND_CODE_SPLIT);
if((System.currentTimeMillis() - Long.parseLong(str[1]))/1000 < 60){
return false;
}else{
return true;
}
}
}
}