收紧店务端验证码登录准入

This commit is contained in:
2026-07-06 17:35:12 +08:00
parent 3c041e0895
commit 7d7d23a3d2
8 changed files with 124 additions and 6 deletions
+2 -2
View File
@@ -26,8 +26,8 @@
<div v-if="activeTab === 'login'" class="login-type-toggle">
<a-radio-group v-model:value="loginType" button-style="solid">
<a-radio-button value="password">账号密码登录</a-radio-button>
<a-radio-button value="phone">手机号验证码登录</a-radio-button>
<a-radio-button value="password">账号密码登录</a-radio-button>
</a-radio-group>
</div>
@@ -479,7 +479,7 @@ const activeTab = ref<'login' | 'register'>('login')
* Purpose: loginType supports AuthView.vue by holding or executing the login type page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const loginType = ref<'password' | 'phone'>('password')
const loginType = ref<'password' | 'phone'>('phone')
/** loading is a reactive state variable used by the template and event handlers. */
const loading = ref(false)
/** loadingShops is a reactive state variable used by the template and event handlers. */
@@ -410,6 +410,11 @@ public class UserController {
return Result.success(userService.getLoginShopSelectionByPhone(phone));
}
@GetMapping("/check/manager-login-shops/phone/{phone}")
public Result<LoginShopSelectionVO> getManagerLoginShopsByPhone(@PathVariable String phone) {
return Result.success(userService.getManagerLoginShopSelectionByPhone(phone));
}
@GetMapping("/check/phone/{phone}")
/**
* Run business method `checkPhoneAvailability`.
@@ -148,6 +148,14 @@ public interface UserService extends IService<UserDO> {
*/
LoginShopSelectionVO getLoginShopSelectionByPhone(String phone);
/**
* 查询店务端手机号验证码登录可选店铺。
*
* @param phone 手机号
* @return 可登录店铺选择结果
*/
LoginShopSelectionVO getManagerLoginShopSelectionByPhone(String phone);
/**
* Load data by the supplied parameter.
* Call guide: pass the named parameters below from the controller or caller; the method validates them and returns the business result.
@@ -956,6 +956,33 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
return buildLoginShopSelection(List.of(), false, "当前账号没有可登录的店铺,请联系门店处理");
}
@Override
public LoginShopSelectionVO getManagerLoginShopSelectionByPhone(String phone) {
String normalizedPhone = phone == null ? "" : phone.trim();
if (!StringUtils.hasText(normalizedPhone)) {
return buildLoginShopSelection(List.of(), false, "请输入手机号后再选择店铺");
}
UserDO userDO = userMapper.selectOne(
Wrappers.lambdaQuery(UserDO.class)
.eq(UserDO::getPhone, normalizedPhone)
.eq(UserDO::getIsDelete, 0)
.last("limit 1")
);
if (userDO == null || !isManagerLoginUser(userDO)) {
return buildLoginShopSelection(List.of(), false, "仅店长或美甲师可登录店务端");
}
if (userDO.getStatus() != null && !Integer.valueOf(1).equals(userDO.getStatus())) {
return buildLoginShopSelection(List.of(), false, "该账号已被禁用");
}
List<ShopSimpleVO> shops = listEnabledShopOptionsForUser(userDO.getId());
if (!shops.isEmpty()) {
return buildLoginShopSelection(shops, false, buildLoginShopMessage(shops));
}
return buildLoginShopSelection(List.of(), false, "当前账号没有可登录的店铺,请联系平台管理员");
}
@Override
/**
* Load data by the supplied parameter.
@@ -1086,6 +1113,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
);
if (userDO == null) {
if (isManagerWebClient(phoneLoginRequestDTO.getClientType())) {
throw new RuntimeException("仅店长或美甲师可登录店务端");
}
if (phoneLoginRequestDTO.getShopId() == null || phoneLoginRequestDTO.getShopId() == 0) {
/**
* Run business method `RuntimeException`.
@@ -1103,10 +1133,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
* @return result consumed by the caller or returned to the frontend
*/
assertUserAvailable(userDO);
if (isManagerWebClient(phoneLoginRequestDTO.getClientType()) && !isManagerLoginUser(userDO)) {
throw new RuntimeException("仅店长或美甲师可登录店务端");
}
clearLoginFailureCount(userDO.getAccount());
}
Long resolvedShopId = resolveUserLoginShopId(userDO, phoneLoginRequestDTO.getShopId());
Long resolvedShopId = isManagerWebClient(phoneLoginRequestDTO.getClientType())
? resolveAdminLoginShopId(userDO, phoneLoginRequestDTO.getShopId())
: resolveUserLoginShopId(userDO, phoneLoginRequestDTO.getShopId());
return buildLoginResponse(userDO, resolvedShopId, ENCODED_UNSET_PASSWORD.equals(userDO.getPassword()), Boolean.TRUE.equals(phoneLoginRequestDTO.getRememberMe()));
}
@@ -1184,6 +1219,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
* Usage: reference this field from the owning layer and pass validated inputs before reading or updating related state.
*/
private static final String ENCODED_UNSET_PASSWORD = "$2a$10$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW"; // 加密后的特殊标识
private static final String MANAGER_WEB_CLIENT_TYPE = "MANAGER_WEB";
/**
* Run the registration flow.
@@ -1423,6 +1459,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
return shopId != null && shopId > 0 ? shopId : null;
}
private boolean isManagerWebClient(String clientType) {
return MANAGER_WEB_CLIENT_TYPE.equalsIgnoreCase(clientType == null ? "" : clientType.trim());
}
private boolean isManagerLoginUser(UserDO userDO) {
if (userDO == null) {
return false;
}
Integer userType = userDO.getType();
return Integer.valueOf(2).equals(userType) || Integer.valueOf(3).equals(userType);
}
@Override
/**
* Run the registration flow.
@@ -9,8 +9,10 @@ import com.xiaowang.jayucloud.core.modules.user.mapper.UserMapper;
import com.xiaowang.jayucloud.core.modules.security.mapper.UserPermissionMapper;
import com.xiaowang.jayucloud.core.modules.user.mapper.UserRelationMapper;
import com.xiaowang.jayucloud.core.modules.security.mapper.UserShopRoleMapper;
import com.xiaowang.jayucloud.core.modules.auth.dto.PhoneLoginRequestDTO;
import com.xiaowang.jayucloud.core.modules.integration.mapper.WechatMiniBindingMapper;
import com.xiaowang.jayucloud.core.modules.auth.dto.RegisterRequestDTO;
import com.xiaowang.jayucloud.core.modules.auth.vo.LoginShopSelectionVO;
import com.xiaowang.jayucloud.core.modules.security.entity.PermissionDO;
import com.xiaowang.jayucloud.core.modules.user.entity.UserDO;
import com.xiaowang.jayucloud.core.modules.artist.service.NailArtistInfoService;
@@ -40,6 +42,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doAnswer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class UserServiceImplPermissionAssignmentTest {
@@ -183,6 +186,47 @@ class UserServiceImplPermissionAssignmentTest {
verifyNoInteractions(permissionMapper, userPermissionMapper);
}
@Test
void managerWebPhoneLoginRejectsPlainCustomer() {
PhoneLoginRequestDTO request = new PhoneLoginRequestDTO();
request.setPhone("13900000002");
request.setVerificationCode("123456");
request.setClientType("MANAGER_WEB");
UserDO customer = new UserDO();
customer.setId(1002L);
customer.setAccount("plaincustomer");
customer.setPhone("13900000002");
customer.setType(0);
customer.setStatus(1);
customer.setIsDelete(0);
when(verificationCodeService.verifyCode("13900000002", "123456")).thenReturn(true);
when(userMapper.selectOne(org.mockito.ArgumentMatchers.any())).thenReturn(customer);
assertThatThrownBy(() -> service.phoneLogin(request))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("仅店长或美甲师可登录店务端");
}
@Test
void managerLoginShopSelectionRejectsPlainCustomerPhone() {
UserDO customer = new UserDO();
customer.setId(1003L);
customer.setPhone("13900000003");
customer.setType(0);
customer.setStatus(1);
customer.setIsDelete(0);
when(userMapper.selectOne(org.mockito.ArgumentMatchers.any())).thenReturn(customer);
LoginShopSelectionVO selection = service.getManagerLoginShopSelectionByPhone("13900000003");
assertThat(selection.isAllowShoplessLogin()).isFalse();
assertThat(selection.getShops()).isEmpty();
assertThat(selection.getMessage()).contains("仅店长或美甲师");
}
@Test
@SuppressWarnings("unchecked")
void exactAccountQueryUsesCaseSensitiveComparison() throws Exception {
@@ -2,6 +2,7 @@ const API_AUTH_FREE_PATHS = [
'/user/send-verification-code',
'/user/phone-login',
'/user/check/login-shops/phone/',
'/user/check/manager-login-shops/phone/',
'/legal-agreement/current'
]
+3
View File
@@ -50,6 +50,9 @@ export const authApi = {
getPhoneLoginShops(phone: string): Promise<LoginShopSelectionResponse> {
return api.get(`/user/check/login-shops/phone/${encodeURIComponent(phone)}`)
},
getManagerPhoneLoginShops(phone: string): Promise<LoginShopSelectionResponse> {
return api.get(`/user/check/manager-login-shops/phone/${encodeURIComponent(phone)}`)
},
getCurrentAgreements(): Promise<LegalAgreement[]> {
return api.get('/legal-agreement/current')
},
+12 -3
View File
@@ -9,8 +9,8 @@
<section class="card auth-card">
<div class="mode-tabs">
<button type="button" :class="{ active: loginMode === 'password' }" @click="switchMode('password')">密码登录</button>
<button type="button" :class="{ active: loginMode === 'phone' }" @click="switchMode('phone')">验证码登录</button>
<button type="button" :class="{ active: loginMode === 'password' }" @click="switchMode('password')">密码登录</button>
</div>
<div v-if="loginMode === 'password'" class="field">
@@ -117,7 +117,7 @@ const session = useSessionStore()
const toast = useToastStore()
const appIconUrl = `${import.meta.env.BASE_URL}assets/images/app-icon.png`
const preference = readJson<LoginPreference>(LOGIN_PREFERENCE_KEY, {})
const loginMode = ref<LoginMode>(preference.loginMode || 'password')
const loginMode = ref<LoginMode>(preference.loginMode || 'phone')
const form = reactive({
account: preference.account || '',
password: '',
@@ -213,6 +213,12 @@ const sendLoginCode = async (): Promise<void> => {
}
codeLoading.value = true
try {
const selection = await authApi.getManagerPhoneLoginShops(form.phone)
const shops = Array.isArray(selection.shops) ? selection.shops : []
if (!selection.allowShoplessLogin && shops.length === 0) {
toast.error(selection.message || '仅店长或美甲师可登录店务端')
return
}
await authApi.sendLoginCode(form.phone)
toast.success('验证码已发送')
startCountdown()
@@ -259,8 +265,11 @@ const showLoginError = (error: unknown): void => {
const openShopSelector = async (): Promise<boolean> => {
const selection = loginMode.value === 'password'
? await authApi.getPasswordLoginShops(form.account)
: await authApi.getPhoneLoginShops(form.phone)
: await authApi.getManagerPhoneLoginShops(form.phone)
const shops = Array.isArray(selection.shops) ? selection.shops : []
if (!selection.allowShoplessLogin && shops.length === 0) {
throw new Error(selection.message || '仅店长或美甲师可登录店务端')
}
if (shops.length <= 1) {
return false
}