登录注册已适配

This commit is contained in:
高子兴 2024-07-01 20:44:13 +08:00
parent ea20be6058
commit 1ba1744698
2 changed files with 56 additions and 52 deletions

View File

@ -3,16 +3,19 @@ import axios from 'axios';
const state = {
user: null,
isAuthenticated: false,
token: null,
};
const mutations = {
setUser(state, user) {
setUser(state, {user, token}) {
state.user = user;
state.isAuthenticated = true;
state.token = token;
},
clearUser(state) {
state.user = null;
state.isAuthenticated = false;
state.token = null;
},
};
@ -20,7 +23,7 @@ const actions = {
async login({commit}, credentials) {
const response = await axios.post('/api/login', credentials);
if (response.status === 200) {
commit('setUser', response.data.username);
commit('setUser', {user: response.data.username, token: response.data.token});
}
return response;
},
@ -30,7 +33,7 @@ const actions = {
async fetchUserProfile({commit}) {
const response = await axios.get('/api/user-profile');
if (response.status === 200) {
commit('setUser', response.data);
commit('setUser', {user: response.data});
}
return response;
},
@ -39,6 +42,7 @@ const actions = {
const getters = {
isAuthenticated: (state) => state.isAuthenticated,
user: (state) => state.user,
token: (state) => state.token,
};
export default {

View File

@ -5,54 +5,54 @@ import 'element-plus/dist/index.css';
import axios from 'axios';
import {useRouter} from 'vue-router';
const companyName = ref('');
const contactInfo = ref('');
const username = ref('');
const phoneNumber = ref('');
const password = ref('');
const verificationCode = ref('');
const router = useRouter();
const handleRegister = async () => {
console.log('Registering with', {
companyName: companyName.value,
contactInfo: contactInfo.value,
username: username.value,
phoneNumber: phoneNumber.value,
password: password.value,
verificationCode: verificationCode.value
});
try {
const response = await axios.post('/api/register', {
companyName: companyName.value,
contactInfo: contactInfo.value,
username: username.value,
phoneNumber: phoneNumber.value,
password: password.value,
verificationCode: verificationCode.value,
});
if (response.status === 200) {
ElMessage.success('注册成功');
ElMessage.success(response.data.message);
router.push('/login');
} else {
ElMessage.error('注册失败,请稍后再试');
ElMessage.error(response.data.message);
}
} catch (error) {
ElMessage.error('注册失败,请稍后再试');
ElMessage.error(error.response.data.message || '注册失败,请稍后再试');
}
};
const sendVerificationCode = async () => {
// try {
// const response = await axios.post('/api/send-verification-code', {
// contactInfo: contactInfo.value,
// });
//
// if (response.status === 200) {
// ElMessage.success('');
// } else {
// ElMessage.error('');
// }
// } catch (error) {
// ElMessage.error('');
// }
try {
const response = await axios.post('/api/sendVerificationCode', {
phoneNumber: phoneNumber.value,
});
if (response.status === 200) {
verificationCode.value = response.data.code;
ElMessage.success('验证码已发送');
} else {
ElMessage.error('发送验证码失败,请稍后再试');
}
} catch (error) {
ElMessage.error('发送验证码失败,请稍后再试');
}
};
</script>
@ -62,12 +62,12 @@ const sendVerificationCode = async () => {
<h2>企业租户注册</h2>
<ElForm @submit.prevent="handleRegister">
<ElFormItem>
<label for="companyName">企业名称</label>
<ElInput v-model="companyName" type="text" id="companyName" placeholder="请输入企业名称" required/>
<label for="userName">用户名称</label>
<ElInput v-model="username" type="text" id="userName" placeholder="请输入企业名称" required/>
</ElFormItem>
<ElFormItem>
<label for="contactInfo">企业联系方式</label>
<ElInput v-model="contactInfo" type="text" id="contactInfo" placeholder="请输入企业联系方式" required/>
<label for="phoneNumber">手机号码</label>
<ElInput v-model="phoneNumber" type="text" id="phoneNumber" placeholder="请输入企业联系方式" required/>
</ElFormItem>
<ElFormItem>
<label for="password">密码</label>