登录注册已适配

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,48 +3,52 @@ import axios from 'axios';
const state = { const state = {
user: null, user: null,
isAuthenticated: false, isAuthenticated: false,
token: null,
}; };
const mutations = { const mutations = {
setUser(state, user) { setUser(state, {user, token}) {
state.user = user; state.user = user;
state.isAuthenticated = true; state.isAuthenticated = true;
state.token = token;
}, },
clearUser(state) { clearUser(state) {
state.user = null; state.user = null;
state.isAuthenticated = false; state.isAuthenticated = false;
state.token = null;
}, },
}; };
const actions = { const actions = {
async login({ commit }, credentials) { async login({commit}, credentials) {
const response = await axios.post('/api/login', credentials); const response = await axios.post('/api/login', credentials);
if (response.status === 200) { if (response.status === 200) {
commit('setUser', response.data.username); commit('setUser', {user: response.data.username, token: response.data.token});
} }
return response; return response;
}, },
logout({ commit }) { logout({commit}) {
commit('clearUser'); commit('clearUser');
}, },
async fetchUserProfile({ commit }) { async fetchUserProfile({commit}) {
const response = await axios.get('/api/user-profile'); const response = await axios.get('/api/user-profile');
if (response.status === 200) { if (response.status === 200) {
commit('setUser', response.data); commit('setUser', {user: response.data});
} }
return response; return response;
}, },
}; };
const getters = { const getters = {
isAuthenticated: (state) => state.isAuthenticated, isAuthenticated: (state) => state.isAuthenticated,
user: (state) => state.user, user: (state) => state.user,
token: (state) => state.token,
}; };
export default { export default {
namespaced: true, namespaced: true,
state, state,
mutations, mutations,
actions, actions,
getters, getters,
}; };

View File

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