改造登录体系,增加小程序一键登录接口

This commit is contained in:
Jack 2025-04-05 00:26:52 +08:00
parent 2be8344181
commit 5699cafbbb
4 changed files with 115 additions and 55 deletions

View File

@ -205,7 +205,7 @@ public class LoginController extends BaseControllerImpl {
return accountUserBaseService.sendVerifyCode(paramJSON.getStr("number"));
}
@ApiOperation(value = "忘记密码后修改密码")
@ApiOperation(value = "忘记密码后修改密码(仅限于商家版app)")
@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"));
@ -215,7 +215,7 @@ public class LoginController extends BaseControllerImpl {
@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"));
return accountUserBaseService.doForgetPassword(paramJSON.getStr("user_mobile"), paramJSON.getStr("rand_key"), paramJSON.getStr("verify_code"));
}
@ApiOperation(value = "忘记密码-更改新密码")

View File

@ -73,4 +73,15 @@ public interface AccountUserBindConnectService extends IBaseService<AccountUserB
* @return
*/
AccountUserBindConnect bindMobileAndOpenId(WxUserInfoReq wxUserInfoReq, Integer userId, Integer userType);
/**
* 初始化基本绑定关系
*
* @param bindId
* @param bindType
* @param userId
* @param userType
* @return
*/
AccountUserBindConnect initAccountUserBindConnect(String bindId, Integer bindType, Integer userId, Integer userType);
}

View File

@ -134,6 +134,8 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
private AccountUserChannelCodeService accountUserChannelCodeService;
@Autowired
private AccountUserBaseMapper accountUserBaseMapper;
@Autowired
private I18nUtil i18nUtil;
@Override
public CommonResult login(Map<String, String> params) {
@ -1624,16 +1626,22 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
public AccountUserBase register(Map userInfo) {
AccountUserBase user_base_reg_row = new AccountUserBase();
Date cur_date = new Date();
Date today = new Date();
// 是否为手机号注册
String user_account = (String) userInfo.get("user_account");
String user_mobile = (String) userInfo.get("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");
String rand_key = (String) userInfo.get("rand_key");
Integer userIsAdmin = Convert.toInt(userInfo.getOrDefault("user_is_admin", CommonConstant.USER_TYPE_NORMAL)); // 用户类型:0-普通买家; 1-管理员2-入驻商家
Integer userType = userIsAdmin; // 用户身份:0-普通买家; 1-管理员2-入驻商家(有歧义废弃)
String user_email = (String) userInfo.get("user_email");
Integer bind_type = getParameter("bind_type", BindCode.MOBILE);
if (StrUtil.isBlank(user_account)) {
throw new ApiException(_("请输入账号"));
}
logger.debug("### 注册参数:{}###", JSONUtil.toJsonStr(userInfo));
@ -1649,10 +1657,10 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
user_mobile = PhoneNumberUtils.convWithIDDCodePhoneNumber(user_mobile, user_intl);
// 有手机参数优先手机注册
if ((StrUtil.isNotBlank(user_mobile) && StrUtil.isNotBlank(verifyCode))
if ((StrUtil.isNotBlank(user_mobile))
|| (CheckUtil.isMobile(user_account, user_intl) && StrUtil.equals(user_account, user_mobile))) {
if (StrUtil.isNotBlank(rand_key)) {
if (StrUtil.isNotBlank(rand_key) && StrUtil.isNotBlank(verifyCode)) {
// 手机验证校验
if (!checkVerifyCode(user_mobile, verifyCode)) {
throw new ApiException(_("验证码校验失败!"));
@ -1665,13 +1673,15 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
logger.error("##手机号:{}##", user_mobile);
throw new ApiException(_("手机号已经绑定过,不可以使用此手机号注册"));
}
bind_type = BindCode.MOBILE;
}
// 是否为账号的注册(好像包含了微信授权openid的)
// 是否为账号的注册(主要微信授权openid的)
if (StrUtil.isNotBlank(user_account) && StrUtil.isNotBlank(verifyCode)
&& StrUtil.isNotBlank(verify_token) && ObjectUtil.isNull(userInfo.get("user_email"))) {
if (StrUtil.isNotBlank(verifyCode)) {
if (StrUtil.isNotBlank(rand_key) && StrUtil.isNotBlank(verifyCode)) {
// 验证码校验
Boolean hasKey = redisService.hasKey(VERIFY_CODE_KEY + user_account);
if (!hasKey) {
@ -1688,24 +1698,18 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
}
}
}
}
if (StrUtil.isBlank(user_intl)) {
user_intl = Convert.toStr(userInfo.get("user_intl"), "");
bind_type = BindCode.WEIXIN_XCX;
}
String user_email = (String) userInfo.get("user_email");
Integer bind_type = getParameter("bind_type", 0);
// 是否为邮箱注册
if (StrUtil.isNotBlank(user_email)
|| (CheckUtil.isEmail(user_account) && StrUtil.equals(user_account, user_email))
|| bind_type.equals(BindCode.EMAIL)) {
if (StrUtil.isNotBlank(rand_key)) {
if (StrUtil.isNotBlank(rand_key) && StrUtil.isNotBlank(verifyCode)) {
// 邮箱验证码校验
String verify_code = (String) userInfo.get("verify_code");
if (!checkVerifyCode(user_email, verify_code)) {
if (!checkVerifyCode(user_email, verifyCode)) {
throw new ApiException(_("验证码校验失败!"));
}
}
@ -1715,11 +1719,10 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
if (bind_check_row != null) {
throw new ApiException("Email已经绑定过不可以使用此Email注册");
}
bind_type = BindCode.EMAIL;
}
if (StrUtil.isBlank(user_account)) {
throw new ApiException(_("请输入账号"));
}
// 原始明文密码
String user_password_src = (String) userInfo.get("user_password");
@ -1732,7 +1735,14 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
AccountUserBase user_base_row = getByAccount(user_account, userIsAdmin);
if (user_base_row != null) {
throw new ApiException(_("用户已经存在,请更换用户名"));
// 检测到 account_user_base 用户已经存在是因为账号没有绑定这时绑定账号即可
// 绑定基本关系
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.initAccountUserBindConnect(user_mobile, bind_type, user_base_row.getUser_id(), userIsAdmin);
if (accountUserBindConnect == null) {
throw new ApiException(_("绑定账号失败"));
}
return user_base_row;
// throw new ApiException(_("用户已经存在,请更换用户名"));
} else {
// 检测到用户尚未注册立即新增用户基本信息和用户附加信息
String user_nickname = Convert.toStr(userInfo.get("user_nickname"), user_account);
@ -1768,19 +1778,6 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
Integer user_id = user_base_reg_row.getUser_id();
AccountUserLogin user_login_reg_row = new AccountUserLogin();
String ip = HttpServletUtils.getClientIpAddr();
user_login_reg_row.setUser_id(user_id);
user_login_reg_row.setUser_reg_datetime(cur_date);
user_login_reg_row.setUser_reg_date(DateUtil.parse(DateUtil.today()));
user_login_reg_row.setUser_reg_time(cur_date.getTime());
user_login_reg_row.setUser_reg_ip(ip);
user_login_reg_row.setUser_lastlogin_time(cur_date);
user_login_reg_row.setUser_lastlogin_ip(ip);
if (!accountUserLoginService.saveOrUpdate(user_login_reg_row)) {
throw new ApiException(ResultCode.FAILED);
}
AccountUserInfo user_info_reg_row = new AccountUserInfo();
Integer user_gender = Convert.toInt(userInfo.get("user_gender"), 1);
@ -1799,14 +1796,30 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
user_info_reg_row.setUser_avatar(user_avatar);
user_info_reg_row.setUser_birthday(DateUtil.parse("2000-01-01"));
user_info_reg_row.setUser_notename(user_notename);
// todo 会员卡号
user_info_reg_row.setUser_level_card("");
if (!accountUserInfoService.saveOrUpdate(user_info_reg_row)) {
throw new ApiException(ResultCode.FAILED);
}
// 绑定基本关系
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.initAccountUserBindConnect(user_mobile, bind_type, user_id, userIsAdmin);
if (accountUserBindConnect == null) {
throw new ApiException(_("绑定账号失败"));
}
AccountUserLogin user_login_reg_row = new AccountUserLogin();
String ip = HttpServletUtils.getClientIpAddr();
user_login_reg_row.setUser_id(user_id);
user_login_reg_row.setUser_reg_datetime(today);
user_login_reg_row.setUser_reg_date(DateUtil.parse(DateUtil.today()));
user_login_reg_row.setUser_reg_time(today.getTime());
user_login_reg_row.setUser_reg_ip(ip);
user_login_reg_row.setUser_lastlogin_time(today);
user_login_reg_row.setUser_lastlogin_ip(ip);
if (!accountUserLoginService.saveOrUpdate(user_login_reg_row)) {
throw new ApiException(ResultCode.FAILED);
}
// 是否为手机号注册
if (StrUtil.isNotBlank(user_mobile)) {
// connect绑定操作
@ -1826,16 +1839,13 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
bindConnect.setBind_openid(user_email);
bindConnect.setBind_active(CommonConstant.Enable);
String bind_id = StrUtil.isNotBlank(user_email) ? user_email : user_account;
if (!bindConnectService.checkBind(bind_id, BindCode.EMAIL, user_id, user_base_row.getUser_is_admin(), bindConnect)) {
if (!bindConnectService.checkBind(user_email, BindCode.EMAIL, user_id, userIsAdmin, bindConnect)) {
throw new ApiException(ResultCode.FAILED);
}
}
// 远程调用异常忽略掉
try {
// todo trigger
// User_Resource初始化 用户积分
if (!payService.initUserPoints(user_id)) {
// throw new ApiException(ResultCode.FAILED);
@ -2417,14 +2427,14 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
return CommonResult.failed("缺少必要参数!");
}
UserDto userDto = getCurrentUser();
// UserDto userDto = getCurrentUser();
String mobileOrEmail = "";
AccountUserBindConnect accountUserBindConnect = null;
boolean isMobile = PhoneNumberUtils.checkPhoneNumber(number);
if (isMobile) {
// number 是手机格式
accountUserBindConnect = accountUserBindConnectService.getBindByBindId(PhoneNumberUtils.convZhPhoneNumber(number), BindCode.MOBILE, userDto.getUserType());
accountUserBindConnect = accountUserBindConnectService.getBindByBindId(PhoneNumberUtils.convZhPhoneNumber(number), BindCode.MOBILE, CommonConstant.USER_TYPE_MCH);
if (accountUserBindConnect != null) {
// 获取绑定的手机号(带国家码)
mobileOrEmail = PhoneNumberUtils.convZhPhoneNumber(accountUserBindConnect.getBind_id());
@ -2432,7 +2442,7 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
} else if (CheckUtil.isEmail(number)) {
// number 是邮箱格式
accountUserBindConnect = accountUserBindConnectService.getBindByBindId(number, BindCode.EMAIL, userDto.getUserType());
accountUserBindConnect = accountUserBindConnectService.getBindByBindId(number, BindCode.EMAIL, CommonConstant.USER_TYPE_MCH);
if (accountUserBindConnect != null) {
// email
mobileOrEmail = accountUserBindConnect.getBind_id();
@ -2853,7 +2863,7 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
// 带国家编码的手机号
String mobile = PhoneNumberUtils.convWithIDDCodePhoneNumber(wxUserInfoReq.getPhoneNumber(), iddCode);
AccountUserBase accountUserBase = getByAccount(mobile);
AccountUserBase accountUserBase = getByAccount(mobile, CommonConstant.USER_TYPE_NORMAL);
if (accountUserBase == null) {
// 检测到用户尚未注册立即新增用户基本信息和用户附加信息
Date today = new Date();
@ -2919,8 +2929,6 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
user_info_reg_row.setUser_avatar(wxUserInfoReq.getAvatarUrl());
user_info_reg_row.setUser_birthday(DateUtil.parse("2000-01-01"));
user_info_reg_row.setUser_notename(wxUserInfoReq.getNickName());
// todo 会员卡号
user_info_reg_row.setUser_level_card("");
if (!accountUserInfoService.saveOrUpdate(user_info_reg_row)) {
return CommonResult.failed(_("用户初始化失败!"));
@ -3054,10 +3062,10 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
return CommonResult.failed("获取不到用户信息!");
}
if (ObjectUtil.notEqual(userType, accountUserBase.getUser_is_admin())) {
// 不是入驻商家的情况,已经有普通用户使用了手机号了提示已经被注册
return CommonResult.failed("已被注册,请换一个手机号码!");
}
// if (ObjectUtil.notEqual(userType, accountUserBase.getUser_is_admin())) {
// 不是入驻商家的情况,已经有普通用户使用了手机号了提示已经被注册
// return CommonResult.failed("已被注册,请换一个手机号码!");
// }
} else {
// 手机号码未绑定的情况直接去注册一个账号
Map<String, Object> userInfo = new HashMap<>();
@ -3400,7 +3408,7 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
/**
* 忘记密码操作
* 忘记密码操作(仅适用于商家版app)
*
* @param userAccountOrMobile 账号绑定过手机号码的账号或手机号码
* @param verifyCode 短信验证码
@ -3413,14 +3421,17 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
return CommonResult.failed("缺少必要参数!");
}
UserDto userDto = getCurrentUser();
// UserDto userDto = getCurrentUser();
// if (userDto == null) {
// return CommonResult.failed("请先登录再试!");
// }
AccountUserBase accountUserBase;
String mobileOrEmail = "";
boolean isMobile = PhoneNumberUtils.checkPhoneNumber(userAccountOrMobile);
if (isMobile) {
// number 是手机格式
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByBindId(PhoneNumberUtils.convZhPhoneNumber(userAccountOrMobile), BindCode.MOBILE, userDto.getUserType());
AccountUserBindConnect accountUserBindConnect = accountUserBindConnectService.getBindByBindId(PhoneNumberUtils.convZhPhoneNumber(userAccountOrMobile), BindCode.MOBILE, CommonConstant.USER_TYPE_MCH);
if (accountUserBindConnect == null) {
return CommonResult.failed(_("账号有异常!"));
}

View File

@ -368,7 +368,45 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
record.setBind_level(0);
record.setBind_vip(0);
if (save(record)) {
if (add(record)) {
return record;
}
return null;
}
public AccountUserBindConnect initAccountUserBindConnect(String bindId, Integer bindType, Integer userId, Integer userType) {
if (StrUtil.isBlank(bindId) || bindType == null || userId == null || userType == null) {
return null;
}
QueryWrapper<AccountUserBindConnect> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("bind_id", bindId)
.eq("bind_type", bindType)
.eq("user_type", userType)
.eq("user_id", userId)
.eq("bind_active", CommonConstant.Enable);
AccountUserBindConnect accountUserBindConnect = findOne(queryWrapper);
if (accountUserBindConnect != null) {
return accountUserBindConnect;
}
// 新增一条绑定数据
AccountUserBindConnect record = new AccountUserBindConnect();
record.setBind_id(bindId);
record.setBind_type(bindType);
record.setUser_id(userId);
record.setUser_type(userType);
record.setBind_active(CommonConstant.Enable);
record.setBind_time(new Date());
record.setBind_expires_in(0);
record.setBind_token_ttl(0);
record.setBind_level(0);
record.setBind_vip(0);
if (add(record)) {
return record;
}