Commit cca29df1 authored by 廖旭伟's avatar 廖旭伟

手机号码短信登录

parent 90b70ac8
package com.mortals.xhx.base.login.sms;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.HashMap;
import java.util.Map;
public class SMSLoginController extends BaseJsonBodyController {
@Autowired
private UserService userService;
@Autowired
private IAuthTokenService authTokenService;
@PostMapping({"send"})
@UnAuth
public Rest<Object> sendSmsVerifyCode(@RequestBody SMSLoginPdu smsLoginPdu){
Rest<Object> ret = new Rest();
Map<String, Object> model = new HashMap();
Context context = this.getContext();
String busiDesc = "发送手机验证码";
int code=1;
try {
userService.sendSmsVerifyCode(smsLoginPdu.getMobileNumber());
model.put("message_info", busiDesc + "成功");
this.recordSysLog(this.request, busiDesc + " 【成功】");
} catch (Exception var9) {
code = -1;
this.doException(this.request, busiDesc, model, var9);
}
ret.setCode(code);
ret.setData(model);
ret.setDict(model.get("dict"));
ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
return ret;
}
@PostMapping("login")
@UnAuth
public String login(@RequestBody SMSLoginPdu smsLoginPdu) throws Exception {
JSONObject ret = new JSONObject();
Map<String, Object> data = new HashMap<>();
String ip = super.getRequestIP(request);
UserEntity userEntity = null;
try {
userEntity = userService.doSmsLogin(smsLoginPdu.getMobileNumber(), smsLoginPdu.getVerifyCode(), ip);
userEntity.setLastLoginAddress(ip);
userEntity.setLoginTime(System.currentTimeMillis());
userEntity.setToken(IdUtil.fastSimpleUUID());
userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
String token = authTokenService.createToken(userEntity);
data.put("token", token);
data.put("customer", userEntity);
recordSysLog(request, userEntity, "登录系统成功!");
ret.put(KEY_RESULT_DATA, data);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "用户登录系统成功!");
return ret.toJSONString();
} catch (Exception e) {
log.error("login error ", e);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, super.convertException(e));
return ret.toJSONString();
}
}
}
package com.mortals.xhx.base.login.sms;
import lombok.Data;
@Data
public class SMSLoginPdu {
/** 手机号码 */
private String mobileNumber;
/** 验证码 */
private String verifyCode;
}
......@@ -117,4 +117,21 @@ public interface UserService extends ICRUDCacheService<UserEntity,Long> {
UserDao getUserDao();
/**
* 发送手机验证码
* @param mobileNumber
* @throws AppException
*/
void sendSmsVerifyCode(String mobileNumber) throws AppException;
/**
* 手机验证码登录
* @param mobileNumber
* @param verifyCode
* @param loginIp
* @return
* @throws AppException
*/
UserEntity doSmsLogin(String mobileNumber, String verifyCode, String loginIp) throws AppException;
}
\ No newline at end of file
......@@ -8,6 +8,9 @@
package com.mortals.xhx.base.system.user.service.impl;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.SysConstains;
import com.mortals.framework.common.code.UserType;
import com.mortals.framework.exception.AppException;
......@@ -16,6 +19,7 @@ import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.IUser;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.framework.util.HttpUtil;
import com.mortals.framework.util.SecurityUtil;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.menu.model.MenuEntity;
......@@ -31,6 +35,7 @@ import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.*;
......@@ -55,6 +60,16 @@ public class UserServiceImpl extends AbstractCRUDCacheServiceImpl<UserDao, UserE
@Autowired
private RoleUserDao roleUserDao;
@Value("${sms.smsSendUrl:http://sms.wx3.com.cn/api/index/index}")
private String smsApiUrl;
@Value("${sms.apiId:ADsUXLrS81vZDU95}")
private String appid;
/** 短信模板ID**/
private static String SMS_TPYE = "30";
private static String SMS_VERIFY_CODE_KEY ="login:sms:verify:";
@Override
protected String getExtKey(UserEntity data) {
return data.getLoginName();
......@@ -347,4 +362,74 @@ public class UserServiceImpl extends AbstractCRUDCacheServiceImpl<UserDao, UserE
return this.getDao();
}
@Override
public void sendSmsVerifyCode(String mobileNumber) throws AppException {
if(StringUtils.isEmpty(mobileNumber)){
throw new AppException("手机号不能为空");
}
if(!PhoneUtil.isPhone(mobileNumber)){
throw new AppException("手机号码格式不正确");
}
UserEntity user = this.selectOne(new UserQuery().mobile(mobileNumber));
if(user==null){
throw new AppException("手机号码:" + mobileNumber + "没有注册用户");
}
String verifyCode = cacheService.get(SMS_VERIFY_CODE_KEY + mobileNumber);
if(StringUtils.isNotEmpty(verifyCode)){
throw new AppException("当前手机号码已发送验证码,请稍后重试");
}
try {
Map<String, String> params = new HashMap<>();
params.put("appid",appid);
params.put("phone",mobileNumber);
params.put("type",SMS_TPYE);
String[] json = new String[2];
String vCode = RandomUtil.randomNumbers(6);
json[0] = vCode;
json[1] = "1";
params.put("json", JSONObject.toJSON(json).toString());
String resp = HttpUtil.doPost(smsApiUrl,params);
JSONObject respJson = JSONObject.parseObject(resp);
if(respJson.getIntValue("code")==0){
throw new AppException("短信发送失败:" + respJson.getString("message"));
}
//有效期60秒
cacheService.setnx(SMS_VERIFY_CODE_KEY+mobileNumber,vCode,60);
}catch (Exception e){
log.error("短信发送异常",e);
throw new AppException("短信发送异常");
}
}
@Override
public UserEntity doSmsLogin(String mobileNumber, String verifyCode, String loginIp) throws AppException {
if(StringUtils.isEmpty(mobileNumber)){
throw new AppException("手机号不能为空");
}
if(StringUtils.isEmpty(verifyCode)){
throw new AppException("验证码不能为空");
}
if(!PhoneUtil.isPhone(mobileNumber)){
throw new AppException("手机号码格式不正确");
}
UserEntity customer = this.selectOne(new UserQuery().mobile(mobileNumber));
if(customer==null){
throw new AppException("手机号码:" + mobileNumber + "没有注册用户");
}
String vCode = cacheService.get(SMS_VERIFY_CODE_KEY + mobileNumber);
if(StringUtils.isEmpty(vCode)){
throw new AppException("验证码已失效");
}
if(!verifyCode.equals(vCode)){
throw new AppException("验证码不正确");
}
UserEntity update = new UserEntity();
update.setId(customer.getId());
update.setLastLoginAddress(loginIp);
update.setLastLoginTime(new Date());
this.update(update);
return customer;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment