complete model

main
old易 2024-10-08 14:00:50 +08:00
parent 0c257a2a4c
commit c4d421c410
1 changed files with 123 additions and 62 deletions

View File

@ -8,15 +8,6 @@
<div class="tab" :class="{ active: selectedTab === 'phone' }" @click="switchTab('phone')"></div>
<div class="tab" :class="{ active: selectedTab === 'password' }" @click="switchTab('password')"></div>
</div>
<div v-if="selectedTab === 'password'">
<input type="text" placeholder="手机号" v-model="username" />
<div v-if="usernameError" class="error-message">{{ usernameError }}</div>
<input type="password" placeholder="账号密码" v-model="password" />
<div v-if="passwordError" class="error-message">{{ passwordError }}</div>
<button @click="login"></button>
</div>
<div v-if="selectedTab === 'phone'">
<input type="text" placeholder="手机号" v-model="phone" />
<div v-if="phoneError" class="error-message">{{ phoneError }}</div>
@ -27,7 +18,22 @@
</button>
</div>
<button @click="verifyCodeAndLogin"
:disabled="!isPhoneValid || !isVerificationCodeValid || countdown !== null">登录</button>
:disabled="!isPhoneValid || !isVerificationCodeValid">登录</button>
</div>
<div v-if="selectedTab === 'password'">
<input type="text" placeholder="手机号" v-model="username" />
<div v-if="usernameError" class="error-message">{{ usernameError }}</div>
<input type="password" placeholder="密码" v-model="password" />
<div v-if="passwordError" class="error-message">{{ passwordError }}</div>
<!-- 图形验证码部分 -->
<div class="captcha-row">
<input type="text" placeholder="验证码" v-model="captchaCode" />
<!-- 显示一个默认的空白图像或设置 v-if 检查 -->
<img alt="验证码" @click="refreshCaptcha" :src="state.captchaImage" style="cursor: pointer" width="130px" height="38px" />
</div>
<div v-if="captchaError" class="error-message">{{ captchaError }}</div>
<button @click="login"></button>
</div>
<div v-if="selectedTab === 'wechat'">
@ -38,7 +44,7 @@
</template>
<script>
import { ref, onMounted, watch, nextTick, computed } from 'vue';
import { ref, onMounted, watch, nextTick, computed,reactive } from 'vue';
import axios from 'axios';
export default {
setup() {
@ -53,7 +59,12 @@ export default {
const verificationCodeError = ref(''); //
const usernameError = ref(''); //
const passwordError = ref(''); //
const captchaCode = ref(''); //
const captchaError = ref(''); //
const state = reactive({
captchaImage: '',
codeId:""
});
//
const switchTab = (tab) => {
selectedTab.value = tab;
@ -63,6 +74,24 @@ export default {
passwordError.value = ''; // Reset error message on tab switch
};
//
const refreshCaptcha = () => {
axios.get('https://api.sso.ycymedu.com/api/sysauth/captcha?timestamp=' + new Date().getTime())
.then(response => {
if (response.data.code === 200 && response.data.result?.img) {
state.captchaImage = 'data:image/png;base64,' + response.data.result.img;
state.codeId = response.data.result.id;
console.log('Response data:', response.data);
console.log(state.captchaImage);
} else {
console.error('Invalid response data:', response.data);
}
})
.catch(error => {
console.error('登录失败:', error);
});
};
//
const startCountdown = () => {
const duration = 60;
@ -97,8 +126,6 @@ export default {
const url = new URL(ref);
console.log('url:', url);
referer.value = url.origin; //
} catch (error) {
console.error('解析来源网址时出错:', error);
}
@ -113,7 +140,6 @@ export default {
const appid = 'wxf8db44d5ec082dfc';
const redirectUri = encodeURIComponent('https://api.sso.ycymedu.com/api/syswechat/snlogin?redirect_uri=' + referer.value);
const state = Math.random().toString(36).substr(2);
new WxLogin({
self_redirect: false,
id: "login_container",
@ -150,7 +176,7 @@ export default {
phoneNumber: phone.value
})
.then(response => {
if (response.data.code === '200') {
if (response.data.code === 200) {
console.log('验证码发送成功:', response.data);
startCountdown(); //
} else {
@ -190,17 +216,18 @@ export default {
//
const isPasswordValid = computed(() => {
const passwordRegex = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).*$/; //
//const passwordRegex = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).*$/; //
if (!password.value) {
passwordError.value = '请输入密码';
return false;
} else if (password.value.includes(' ')) {
passwordError.value = '密码不能包含空格';
return false;
} else if (!passwordRegex.test(password.value)) {
passwordError.value = '密码必须包含字母、数字和特殊字符';
return false;
}
}
// else if (!passwordRegex.test(password.value)) {
// passwordError.value = '';
// return false;
// }
passwordError.value = ''; //
return true;
});
@ -209,13 +236,18 @@ export default {
onMounted(() => {
getReferer(); //
initWeChatLogin();
refreshCaptcha();
});
//
watch(selectedTab, (newTab) => {
if (newTab === 'wechat') {
if (newTab === 'wechat')
{
initWeChatLogin();
}
if(newTab === 'password'){
refreshCaptcha();
}
});
//
@ -228,50 +260,59 @@ export default {
alert('请检查验证码');
return;
}
alert('验证码登录功能尚未实现');
axios.post('https://api.sso.ycymedu.com/api/syswechat/smslogin', {
phoneNumber: phone.value,
code: verificationCode.value,
redirect_uri: referer.value
})
.then(response => {
if (response.data.code === 200) {
console.log('登录失败成功:', response.data);
window.location.href=response.data.result;
// startCountdown(); //
} else {
console.log('登录失败:', response.data);
phoneError.value = response.data.message;
}
})
.catch(error => {
console.error('登录失败:', error);
phoneError.value = '登录失败,请稍后再试';
});
};
//
const login = () => {
switch (selectedTab.value) {
case 'wechat':
//
break;
case 'password':
if (!isUsernameValid.value) {
alert('请检查手机号');
return;
}
if (!isPasswordValid.value) {
alert('请检查密码');
return;
}
alert('账号密码登录功能尚未实现');
//
break;
case 'phone':
axios.post('https://api.sso.ycymedu.com/api/syswechat/smslogin', {
phoneNumber: phone.value,
code: verificationCode.value,
redirect_uri: referer.value
})
.then(response => {
if (response.data.code === '200') {
console.log('验证码发送成功:', response.data);
startCountdown(); //
} else {
console.log('验证码发送失败:', response.data);
phoneError.value = response.data.message;
}
})
.catch(error => {
console.error('发送验证码失败:', error);
phoneError.value = '发送验证码失败,请稍后再试';
});
break;
if (!isUsernameValid.value) {
alert('请检查手机号');
return;
}
if (!isPasswordValid.value) {
alert('请检查密码');
return;
}
axios.post('https://api.sso.ycymedu.com/api/syswechat/pwdlogin', {
phoneNumber: phone.value,
password: password.value,
redirect_uri: referer.value,
code: captchaCode.value,
codeId: state.codeId,
})
.then(response => {
if (response.data.code === 200) {
console.log('登录失败成功:', response.data);
window.location.href=response.data.result;
// startCountdown(); //
} else {
console.log('登录失败:', response.data);
captchaError.value = response.data.message;
}
})
.catch(error => {
console.error('登录失败:', error);
captchaError.value = '登录失败,请稍后再试';
});
};
@ -294,7 +335,9 @@ export default {
passwordError,
isUsernameValid,
isPasswordValid,
login
login,
refreshCaptcha,
state
};
},
};
@ -431,4 +474,22 @@ button:disabled {
margin-bottom: 10px;
}
}
.captcha-row {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.captcha-row input {
width: calc(100% - 120px);
margin-right: 10px;
}
.captcha-row img {
cursor: pointer;
height: 40px;
width: 100px;
border-radius: 4px;
border: 1px solid #ccc;
}
</style>