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;
/**
* 用户办卡绑定手机号.
*
* 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);
/**
* 跳转用户绑定页面.
*/
@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");
}
/**
* 通过手机号发送验证码.
*/
@Clear(BindInterceptor.class)
public void sendRandCode() {
String phoneNum = this.getPara("phone_number");
logger.info("[开始发送验证码,入参="+ phoneNum);
Map map = new HashMap();
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);
}
/**
* 绑定手机号.
*/
@Clear(BindInterceptor.class)
public void bindingPhone(){
BizResult bizResult = new BizResult();
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 queryCustPersonResult =custPersonService.queryCustPerson(phoneNum);
BizResult result = new BizResult();
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 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秒的话,不容许发送验证码.
* @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;
}
}
}
}