问题:验证码插入不成功?
找出问题 :在天翼调用方法时验证码VerificationCode vcode先插入数据库,所以保存在数据库,如果这个是调用的 update方法,是不能保存在数据库里面的。
解决方法:所以要根据vcode来查,如果数据库存在该数据,就用update如果不存在就用insert方法。id type VerificationCode VerificationPhone vcode status create_date
85 register 513294 15507559401 Rl0104 1 2015-09-02 16:38:00
165 register null 18370662763 yx5535 0 2015-12-24 16:37:16
/**
* 注册帐号,获取验证码
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = "GetCode", method = RequestMethod.GET)
@ResponseBody
public void GetCode(HttpServletRequest request) throws IOException {
JSONMessage jMessage = new JSONMessage();
try {
String phone = request.getParameter("username").toString();
int num = 0; // User_Logic.GetCurDayCodeNum(phone);
if (num > 10)
{
jMessage = new JSONMessage(1, "您获取的验证码已超过限制,请明天再获取验证码");
} else {
Open189SDK openSms = Open189SDK.getInstance();
AccessTokenStatus tokenObj = openSms.getToken();
if (tokenObj.getToken() != null) {
String callback = "http://apis.i-comm.cn:8080/KSO/PutCode";
// String callback =
// "http://120.24.57.64/homeapi/api/User/PutCode";
SendSmsStatus sendVcodeObj = openSms.sendRandCode(tokenObj.getToken(), phone, callback);
if (sendVcodeObj.getIdentifier().length() > 0) {
String identifier = sendVcodeObj.getIdentifier();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = dateFormat.format(date);
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("type", "register");
maps.put("VerificationPhone", phone);
maps.put("vcode", identifier);
maps.put("status", 0);
maps.put("create_date", timestamp);
// 插入手机验证码
int resultId = 0; // User_Logic.UpUserVcode("register",identifier,
// phone, create2);
resultId = memberService.insertSmsVerification(maps);
if (resultId > 0) {
jMessage = new JSONMessage(1, "验证码获取成功");
} else {
jMessage = new JSONMessage(1, "验证码获取成功");
}
} else {
jMessage = new JSONMessage(0, "验证码获取失败");
}
} else {
jMessage = new JSONMessage(0, "验证码获取失败");
}
}
} catch (Exception ex) {
jMessage = new JSONMessage(0, "验证码获取失败");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//插入手机验证码
public int insertSmsVerification(Map<String, Object> maps) {
Sms_verification sv = new Sms_verification();
String type = String.valueOf(maps.get("type"));
String vcode = String.valueOf(maps.get("vcode"));
String VerificationPhone = String.valueOf(maps.get("VerificationPhone"));
int status = Integer.valueOf((int) maps.get("status")) ;
String create_date = String.valueOf(maps.get("create_date"));
sv.setType(type);
sv.setVcode(vcode);
sv.setVerificationPhone(VerificationPhone);
sv.setStatus(status);
sv.setCreate_date(create_date);
String select = "FROM Sms_verification s WHERE s.vcode=" + "‘" + vcode+"‘";
String update = "UPDATE Sms_verification s SET s.type="
+"‘"+ type+ "‘"
+", s.VerificationPhone ="
+"‘"+VerificationPhone+"‘"+", s.status ="
+"‘"+status+"‘"+", s.create_date ="
+"‘"+create_date+"‘"
+ " WHERE s.vcode=" +"‘"+ vcode+"‘";
Query query = null;
int resultId = 0;
query = session().createQuery(select);
List list= query.list();
if(list.size()>0){
query = session().createQuery(update);
resultId = query.executeUpdate();
return resultId;
}else{
session().saveOrUpdate(sv);
resultId = sv.getId();
return resultId;
}
}
// 插入验证码
public void updateSmsVerification(Map<String, Object> maps) {
Sms_verification sv = new Sms_verification();
String VerificationCode = String.valueOf(maps.get("VerificationCode"));
String vcode = String.valueOf(maps.get("vcode"));
String select = "FROM Sms_verification s WHERE s.vcode=" + "‘" + vcode+"‘";
String update = "UPDATE Sms_verification s SET s.VerificationCode=" + VerificationCode
+ " WHERE s.vcode=" +"‘"+ vcode+"‘";
Query query = null;
query = session().createQuery(select);
List list= query.list();
if(list.size()>0){
query = session().createQuery(update);
query.executeUpdate();
}else{
sv.setVerificationCode(VerificationCode);
sv.setVcode(vcode);
session().saveOrUpdate(sv);
}
}
本文出自 “pcaijian” 博客,请务必保留此出处http://pcaijian.blog.51cto.com/7401276/1734793
原文地址:http://pcaijian.blog.51cto.com/7401276/1734793