mirror of
https://github.com/Silentely/eSIM-Tools.git
synced 2026-09-03 06:24:20 +08:00
♻️ refactor(simyo): 移除模块化流程的 sendSmsCode 路径
This commit is contained in:
@@ -134,7 +134,6 @@
|
||||
|
||||
**核心方法:**
|
||||
- `applyNewEsim()` - 申请新eSIM
|
||||
- `sendSmsCode()` - 兼容旧流程的验证码发送(新版邮箱流程通常不需要单独发送)
|
||||
- `verifyCode()` - 验证验证码
|
||||
|
||||
**依赖:**
|
||||
|
||||
@@ -37,10 +37,6 @@ export function getApiEndpoints() {
|
||||
applyNewEsim: isNetlify
|
||||
? "/api/simyo/settings/simcard"
|
||||
: "http://localhost:3000/api/simyo/settings/simcard",
|
||||
// 兼容旧版短信验证码流程(仅在服务端仍支持时使用)
|
||||
sendSmsCode: isNetlify
|
||||
? "/api/simyo/esim/send-sms-code"
|
||||
: "http://localhost:3000/api/simyo/esim/send-sms-code",
|
||||
verifyCode: isNetlify
|
||||
? "/api/simyo/esim/verify-code"
|
||||
: "http://localhost:3000/api/simyo/esim/verify-code",
|
||||
|
||||
@@ -13,41 +13,6 @@ export class DeviceChangeHandler {
|
||||
this.apiEndpoints = getApiEndpoints();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询可用的验证方式
|
||||
* @returns {Promise<Object>} 可用的验证方式列表
|
||||
*/
|
||||
async getAvailableValidationMethods() {
|
||||
const sessionToken = stateManager.get('sessionToken');
|
||||
|
||||
if (!sessionToken) {
|
||||
throw new Error(t('simyo.errors.requireLogin'));
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(this.apiEndpoints.availableValidationMethods, {
|
||||
method: 'POST',
|
||||
headers: createHeaders(true, sessionToken),
|
||||
body: JSON.stringify(null)
|
||||
});
|
||||
|
||||
const data = await handleApiResponse(response);
|
||||
|
||||
if (!data.success || !data.result) {
|
||||
throw new Error(data.message || t('simyo.device.queryFailed'));
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
methods: data.result.availableMethods || [],
|
||||
message: t('simyo.device.querySuccess')
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(t('simyo.device.log.queryFailed'), error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请新eSIM(支持邮箱验证)
|
||||
* @param {string} validationMethod - 验证方式 (默认: EMAIL)
|
||||
@@ -93,82 +58,6 @@ export class DeviceChangeHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码(兼容新版邮箱验证与旧版短信验证)
|
||||
* @returns {Promise<Object>} 发送结果
|
||||
*/
|
||||
async sendSmsCode() {
|
||||
const sessionToken = stateManager.get('sessionToken');
|
||||
|
||||
if (!sessionToken) {
|
||||
throw new Error(t('simyo.errors.requireLogin'));
|
||||
}
|
||||
|
||||
let methodInfo = null;
|
||||
|
||||
try {
|
||||
const methodResult = await this.getAvailableValidationMethods();
|
||||
methodInfo = this.detectValidationMethodSupport(methodResult.methods);
|
||||
} catch (_) {}
|
||||
|
||||
// 回退旧版短信接口(兼容旧流程)
|
||||
if (!this.apiEndpoints.sendSmsCode) {
|
||||
throw new Error(t('simyo.device.smsFailed'));
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(this.apiEndpoints.sendSmsCode, {
|
||||
method: 'POST',
|
||||
headers: createHeaders(true, sessionToken),
|
||||
});
|
||||
|
||||
const data = await handleApiResponse(response);
|
||||
|
||||
if (!data.success || !data.result) {
|
||||
throw new Error(data.message || t('simyo.device.smsFailed'));
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: data.message || data.result.message || t('simyo.device.smsSuccess'),
|
||||
nextStep: data.result.nextStep,
|
||||
};
|
||||
} catch (error) {
|
||||
// 新版流程(邮箱验证)下,发送短信接口可能不存在,此时给出兼容提示
|
||||
if (methodInfo && methodInfo.hasEmail && !methodInfo.hasSms) {
|
||||
return {
|
||||
success: true,
|
||||
message: t('simyo.device.emailSent'),
|
||||
};
|
||||
}
|
||||
console.error(t('simyo.device.log.smsFailed'), error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析可用验证方式
|
||||
* @param {Array} methods - 后端返回的验证方式列表
|
||||
* @returns {{hasEmail: boolean, hasSms: boolean}}
|
||||
*/
|
||||
detectValidationMethodSupport(methods = []) {
|
||||
const normalizedMethods = (Array.isArray(methods) ? methods : [])
|
||||
.map((method) => {
|
||||
if (typeof method === 'string') return method;
|
||||
if (method && typeof method === 'object') {
|
||||
return method.type || method.method || method.name || '';
|
||||
}
|
||||
return '';
|
||||
})
|
||||
.map(method => String(method).toUpperCase())
|
||||
.filter(Boolean);
|
||||
|
||||
return {
|
||||
hasEmail: normalizedMethods.some(method => method.includes('EMAIL')),
|
||||
hasSms: normalizedMethods.some(method => method.includes('SMS')),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证验证码
|
||||
* @param {string} validationCode - 验证码
|
||||
|
||||
@@ -51,65 +51,4 @@ describe('Simyo DeviceChangeHandler 集成覆盖', () => {
|
||||
expect.objectContaining({ method: 'POST' })
|
||||
);
|
||||
});
|
||||
|
||||
it('sendSmsCode 在 SMS 可用时应调用官方发送接口', async () => {
|
||||
global.fetch
|
||||
.mockResolvedValueOnce({
|
||||
json: async () => ({
|
||||
success: true,
|
||||
result: {
|
||||
availableMethods: ['SMS']
|
||||
}
|
||||
})
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
json: async () => ({
|
||||
success: true,
|
||||
result: {
|
||||
message: 'sms sent',
|
||||
nextStep: 'next'
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const result = await deviceChangeHandler.sendSmsCode();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(global.fetch).toHaveBeenCalledTimes(2);
|
||||
expect(global.fetch).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'http://localhost:3000/api/simyo/esim.availableValidationMethods',
|
||||
expect.objectContaining({ method: 'POST' })
|
||||
);
|
||||
expect(global.fetch).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'http://localhost:3000/api/simyo/esim/send-sms-code',
|
||||
expect.objectContaining({ method: 'POST' })
|
||||
);
|
||||
});
|
||||
|
||||
it('sendSmsCode 在邮箱流程且短信接口失败时应回退为邮箱提示', async () => {
|
||||
global.fetch
|
||||
.mockResolvedValueOnce({
|
||||
json: async () => ({
|
||||
success: true,
|
||||
result: {
|
||||
availableMethods: ['EMAIL']
|
||||
}
|
||||
})
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
json: async () => ({
|
||||
success: false,
|
||||
message: 'sms endpoint unsupported'
|
||||
})
|
||||
});
|
||||
|
||||
const result = await deviceChangeHandler.sendSmsCode();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.message).toBeTruthy();
|
||||
expect(global.fetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user