登录注册已适配
This commit is contained in:
parent
ea20be6058
commit
1ba1744698
@ -3,48 +3,52 @@ 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;
|
||||
},
|
||||
};
|
||||
|
||||
const actions = {
|
||||
async login({ commit }, credentials) {
|
||||
const response = await axios.post('/api/login', credentials);
|
||||
if (response.status === 200) {
|
||||
commit('setUser', response.data.username);
|
||||
}
|
||||
return response;
|
||||
},
|
||||
logout({ commit }) {
|
||||
commit('clearUser');
|
||||
},
|
||||
async fetchUserProfile({ commit }) {
|
||||
const response = await axios.get('/api/user-profile');
|
||||
if (response.status === 200) {
|
||||
commit('setUser', response.data);
|
||||
}
|
||||
return response;
|
||||
},
|
||||
async login({commit}, credentials) {
|
||||
const response = await axios.post('/api/login', credentials);
|
||||
if (response.status === 200) {
|
||||
commit('setUser', {user: response.data.username, token: response.data.token});
|
||||
}
|
||||
return response;
|
||||
},
|
||||
logout({commit}) {
|
||||
commit('clearUser');
|
||||
},
|
||||
async fetchUserProfile({commit}) {
|
||||
const response = await axios.get('/api/user-profile');
|
||||
if (response.status === 200) {
|
||||
commit('setUser', {user: response.data});
|
||||
}
|
||||
return response;
|
||||
},
|
||||
};
|
||||
|
||||
const getters = {
|
||||
isAuthenticated: (state) => state.isAuthenticated,
|
||||
user: (state) => state.user,
|
||||
isAuthenticated: (state) => state.isAuthenticated,
|
||||
user: (state) => state.user,
|
||||
token: (state) => state.token,
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
mutations,
|
||||
actions,
|
||||
getters,
|
||||
namespaced: true,
|
||||
state,
|
||||
mutations,
|
||||
actions,
|
||||
getters,
|
||||
};
|
||||
|
||||
@ -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('发送验证码失败,请稍后再试');
|
||||
// }
|
||||
ElMessage.success('验证码已发送');
|
||||
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>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user