商家版注册与登录接口

This commit is contained in:
Jack 2025-02-25 17:26:46 +08:00
parent f10674ecb1
commit 2ef5202b5c
7 changed files with 240 additions and 16 deletions

View File

@ -181,6 +181,32 @@ public class LoginController extends BaseControllerImpl {
return accountUserBaseService.doMerchSmsRegisterAndLogin(paramJSON.getStr("user_mobile"), paramJSON.getStr("rand_key"), paramJSON.getStr("verify_code"));
}
@ApiOperation(value = "通过绑定手机的账号、手机、email 发送短信、邮件验证码")
@RequestMapping(value = "/sendVerifyCode", method = RequestMethod.POST)
public CommonResult sendVerifyCode(@RequestBody JSONObject paramJSON) {
return accountUserBaseService.sendVerifyCode(paramJSON.getStr("number"));
}
@ApiOperation(value = "忘记密码后修改密码")
@RequestMapping(value = "/doForgetPassword", method = RequestMethod.POST)
public CommonResult doForgetPassword(@RequestBody JSONObject paramJSON) {
return accountUserBaseService.doForgetPassword(paramJSON.getStr("number"), paramJSON.getStr("verify_code"), paramJSON.getStr("new_password"));
}
@ApiOperation(value = "忘记密码-验证短信验证码")
@RequestMapping(value = "/put/forgetPassword", method = RequestMethod.POST)
public CommonResult putForgetPassword(@RequestBody JSONObject paramJSON) {
return accountUserBaseService.doMerchSmsRegisterAndLogin(paramJSON.getStr("user_mobile"), paramJSON.getStr("rand_key"), paramJSON.getStr("verify_code"));
}
@ApiOperation(value = "忘记密码-更改新密码")
@RequestMapping(value = "/edit/forgetPassword", method = RequestMethod.POST)
public CommonResult editForgetPassword(@RequestBody JSONObject paramJSON) {
return accountUserBaseService.doMerchSmsRegisterAndLogin(paramJSON.getStr("user_mobile"), paramJSON.getStr("rand_key"), paramJSON.getStr("verify_code"));
}
@ApiOperation("退出登录")
@RequestMapping(value = "/doLogout", method = RequestMethod.GET)
public CommonResult doLogout() {

View File

@ -79,6 +79,15 @@ public interface AccountUserBaseService extends IBaseService<AccountUserBase> {
boolean getVerifyCode(String number);
/**
* 通过绑定手机的账号手机email 发送短信邮件验证码
*
* @param number 账号绑定过手机号码的账号或手机号码 email
* @return
*/
CommonResult sendVerifyCode(String number);
boolean getEmailMobileVerifyCode(String number);
AccountUserBase getByNickname(String nickname);
@ -168,4 +177,15 @@ public interface AccountUserBaseService extends IBaseService<AccountUserBase> {
Boolean existByNickname(String nickname, String storeId);
Pair<Boolean, AccountUserBase> saveOrUpdate2(AccountUserBase entity);
/**
* 忘记密码操作
*
* @param userAccountOrMobile 账号绑定过手机号码的账号或手机号码
* @param verifyCode 短信验证码
* @param newPassword 新密码
* @return
*/
CommonResult doForgetPassword(String userAccountOrMobile, String verifyCode, String newPassword);
}

View File

@ -15,8 +15,27 @@ import java.util.Map;
*/
public interface AccountUserBindConnectService extends IBaseService<AccountUserBindConnect> {
/**
* 根据用户 ID 和绑定类型获取一条记录
*
* @param user_id
* @param bind_type
* @return
*/
AccountUserBindConnect getBindByUserId(Integer user_id, Integer bind_type);
Map getBind(Integer user_id, int bind_type);
/**
* 根据 bind_id bind_type 获取一条记录
*
* @param bind_id
* @param bind_type
* @return
*/
AccountUserBindConnect getBindByBindId(String bind_id, Integer bind_type);
boolean checkBind(String bind_id, int bind_type, Integer user_id, AccountUserBindConnect user_info_row);
boolean checkAccessToken(Integer bind_type, AccountUserBindConnect bind_data, Integer user_id);
@ -26,8 +45,8 @@ public interface AccountUserBindConnectService extends IBaseService<AccountUserB
*
* @param user_info_row bind_connect数据
* @param reg_flag 是否需要注册新账号
* @source_user_id 父级用户id
* @activity_id 活动id
* @source_user_id 父级用户id
* @activity_id 活动id
* @access public
*/
Integer doUserBind(AccountUserBindConnect user_info_row, String activity_id, boolean reg_flag);

View File

@ -1608,6 +1608,7 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
// 是否为手机号注册
String user_account = (String) userInfo.get("user_account");
String user_mobile = (String) userInfo.get("user_mobile");
user_mobile = PhoneNumberUtils.convZhPhoneNumber(user_mobile);
String rand_key = (String) userInfo.get("rand_key");
String verifyCode = (String) userInfo.get("verify_code");
String verify_token = (String) userInfo.get("verify_token");
@ -1887,20 +1888,20 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
@Override
public boolean doResetPasswd(String user_account, String user_password, String old_password) {
if (StrUtil.isBlank(user_account)) {
throw new ApiException(_("请输入账号"));
throw new ApiException(_("缺少账号信息"));
}
if (StrUtil.isBlank(user_password)) {
throw new ApiException(_("请输入密码"));
throw new ApiException(_("请输入密码"));
}
verifyPwd(user_password);
verifyPwd(user_password); // 密码格式策略验证
// 检测登录状态
AccountUserBase user_row = getByAccount(user_account);
if (ObjectUtil.isNotEmpty(user_row)) {
if (old_password != null) {
if (StrUtil.isNotBlank(old_password)) {
String user_salt = user_row.getUser_salt();
String md5_password = SecureUtil.md5(user_salt + SecureUtil.md5(old_password));
@ -2245,14 +2246,14 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
public AccountUserBase getByAccount(String user_account) {
QueryWrapper<AccountUserBase> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_account", user_account).orderByAsc("user_account").orderByAsc("user_password");
queryWrapper.eq("user_account", user_account).orderByAsc("user_id");
AccountUserBase data = findOne(queryWrapper);
return data;
}
/**
* 获取 手机/邮件 验证码
* 发送 手机/邮件 验证码
*
* @param number
* @return
@ -2348,6 +2349,62 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
return false;
}
/**
* 发送验证码
*
* @param number 账号绑定过手机号码的账号或手机号码 email
* @return
*/
@Override
public CommonResult sendVerifyCode(String number) {
if (StrUtil.isBlank(number)) {
return CommonResult.failed("缺少必要参数!");
}
String mobileOrEmail = "";
boolean isMobile = PhoneNumberUtils.checkPhoneNumber(number);
if (isMobile) {
// number 是手机格式
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByBindId(PhoneNumberUtils.convZhPhoneNumber(number), BindCode.MOBILE);
if (accountUserBindConnect == null) {
return CommonResult.failed(_("账号有异常!"));
}
// 获取绑定的手机号(带国家码)
mobileOrEmail = PhoneNumberUtils.convZhPhoneNumber(accountUserBindConnect.getBind_id());
} else if (CheckUtil.isEmail(number)) {
// number 是邮箱格式
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByBindId(number, BindCode.EMAIL);
if (accountUserBindConnect == null) {
return CommonResult.failed(_("账号有异常!"));
}
// email
mobileOrEmail = accountUserBindConnect.getBind_id();
} else {
// number 是userAccount账号格式
AccountUserBase accountUserBase = accountUserBaseService.getByAccount(number);
if (accountUserBase == null) {
return CommonResult.failed(_("账号有异常!"));
}
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByUserId(accountUserBase.getUser_id(), BindCode.MOBILE);
if (accountUserBindConnect == null) {
return CommonResult.failed(_("账号有异常!"));
}
// 获取绑定的手机号(带国家码)
mobileOrEmail = PhoneNumberUtils.convZhPhoneNumber(accountUserBindConnect.getBind_id());
}
// 发送短信或邮件验证码
if (getVerifyCode(mobileOrEmail)) {
return CommonResult.success(new JSONObject().put("number", mobileOrEmail));
}
return CommonResult.failed();
}
/**
* 获取邮箱修改验证码
@ -2448,10 +2505,13 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
* @return
*/
private boolean checkVerifyCode(String number, String verifyCode) {
if (StrUtil.isBlank(verifyCode)) {
if (StrUtil.isBlank(verifyCode) || StrUtil.isBlank(number)) {
return false;
}
// 带国家码手机号码
number = PhoneNumberUtils.convZhPhoneNumber(number);
//通过lure脚本原子验证令牌和删除令牌
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
Long res = redisService.execute(script, Collections.singletonList(VERIFY_CODE_KEY + number), verifyCode);
@ -3094,4 +3154,69 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
return Pair.of(flag, entity);
}
/**
* 忘记密码操作
*
* @param userAccountOrMobile 账号绑定过手机号码的账号或手机号码
* @param verifyCode 短信验证码
* @param newPassword 新密码
* @return
*/
@Override
public CommonResult doForgetPassword(String userAccountOrMobile, String verifyCode, String newPassword) {
if (StrUtil.isBlank(userAccountOrMobile) || StrUtil.isBlank(verifyCode) || StrUtil.isBlank(newPassword)) {
return CommonResult.failed("缺少必要参数!");
}
AccountUserBase accountUserBase;
String mobileOrEmail = "";
boolean isMobile = PhoneNumberUtils.checkPhoneNumber(userAccountOrMobile);
if (isMobile) {
// number 是手机格式
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByBindId(PhoneNumberUtils.convZhPhoneNumber(userAccountOrMobile), BindCode.MOBILE);
if (accountUserBindConnect == null) {
return CommonResult.failed(_("账号有异常!"));
}
accountUserBase = accountUserBaseService.get(accountUserBindConnect.getUser_id());
if (accountUserBase == null) {
return CommonResult.failed(_("账号有异常!"));
}
// 获取绑定的手机号(带国家码)
mobileOrEmail = PhoneNumberUtils.convZhPhoneNumber(accountUserBindConnect.getBind_id());
} else {
// number 是userAccount账号格式
accountUserBase = accountUserBaseService.getByAccount(userAccountOrMobile);
if (accountUserBase == null) {
return CommonResult.failed(_("账号有异常!"));
}
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByUserId(accountUserBase.getUser_id(), BindCode.MOBILE);
if (accountUserBindConnect == null) {
return CommonResult.failed(_("账号有异常!"));
}
// 获取绑定的手机号(带国家码)
mobileOrEmail = PhoneNumberUtils.convZhPhoneNumber(accountUserBindConnect.getBind_id());
}
// 短信验证
if (!checkVerifyCode(mobileOrEmail, verifyCode)) {
throw new ApiException(_("验证码错误!"));
}
// 直接重置密码
Boolean success = doResetPasswd(accountUserBase.getUser_account(), newPassword, null);
if (success) {
return CommonResult.success();
}
return CommonResult.failed();
}
}

View File

@ -43,6 +43,22 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
@Autowired
private AccountUserBaseService accountUserBaseService;
/**
* 根据用户 ID 和绑定类型获取一条记录
*
* @param user_id
* @param bind_type
* @return
*/
@Override
public AccountUserBindConnect getBindByUserId(Integer user_id, Integer bind_type) {
QueryWrapper<AccountUserBindConnect> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user_id)
.eq("bind_active", CommonConstant.Enable)
.eq("bind_type", bind_type);
return findOne(queryWrapper);
}
/**
* 获取有效绑定
*
@ -52,14 +68,16 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
*/
@Override
public Map getBind(Integer user_id, int bind_type) {
return Convert.toMap(String.class, Object.class, getBindByUserId(user_id, bind_type));
}
@Override
public AccountUserBindConnect getBindByBindId(String bind_id, Integer bind_type) {
QueryWrapper<AccountUserBindConnect> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user_id)
.eq("bind_active", 1)
queryWrapper.eq("bind_id", bind_id)
.eq("bind_active", CommonConstant.Enable)
.eq("bind_type", bind_type);
AccountUserBindConnect bindConnect = findOne(queryWrapper);
return Convert.toMap(String.class, Object.class, bindConnect);
return findOne(queryWrapper);
}
@Override

View File

@ -18,6 +18,8 @@ public class RedisConstant {
public static final String Verifycode_NameSpace = ConstantRedis.Cache_NameSpace + "VERIFYCODE:";
public static final String FORGETPWD_NAMEPACE = ConstantRedis.Cache_NameSpace + "FORGETPWD:";
public static final String Config_Cache_Key = "config_cache_key";
public static final String Config_Cache_Version = "config_cache_version";

View File

@ -40,13 +40,13 @@ public class PhoneNumberUtils {
private final static int COUNTRY_CODE_CHINA = 86;
/**
* 判断手机号是否有效中国
* 判断中国手机号是否有效
*
* @param phoneNumber 手机号码
* @return true-有效 false-无效
*/
public static boolean checkPhoneNumber(String phoneNumber) {
return checkPhoneNumber(COUNTRY_CODE_CHINA, phoneNumber);
return checkPhoneNumber(COUNTRY_CODE_CHINA, cleanPhoneNumber(phoneNumber));
}
/**
@ -59,6 +59,20 @@ public class PhoneNumberUtils {
return StrUtil.startWith(phoneNumber, "+") ? phoneNumber : CommonConstant.IDD_ZH_CN + phoneNumber;
}
/**
* 去掉手机的国家码方法不严谨
*
* @param phoneNumber
* @return
*/
public static String cleanPhoneNumber(String phoneNumber) {
if (phoneNumber.length() <= 3) {
return phoneNumber;
}
return StrUtil.startWith(phoneNumber, "+") ? phoneNumber.substring(2) : phoneNumber;
}
/**
* 判断手机号是否有效国际
*