登录注册已适配
This commit is contained in:
parent
ea20be6058
commit
1ba1744698
@ -3,16 +3,19 @@ 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;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ 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;
|
||||||
},
|
},
|
||||||
@ -30,7 +33,7 @@ const actions = {
|
|||||||
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;
|
||||||
},
|
},
|
||||||
@ -39,6 +42,7 @@ const actions = {
|
|||||||
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 {
|
||||||
|
|||||||
@ -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.error('发送验证码失败,请稍后再试');
|
|
||||||
// }
|
|
||||||
// } catch (error) {
|
|
||||||
// ElMessage.error('发送验证码失败,请稍后再试');
|
|
||||||
// }
|
|
||||||
ElMessage.success('验证码已发送');
|
ElMessage.success('验证码已发送');
|
||||||
|
} else {
|
||||||
|
ElMessage.error('发送验证码失败,请稍后再试');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
ElMessage.error('发送验证码失败,请稍后再试');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</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>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user