Skip to content

Commit 7e4786d

Browse files
committed
新增login api
遇到cors問題
1 parent 60397ce commit 7e4786d

File tree

8 files changed

+214
-40
lines changed

8 files changed

+214
-40
lines changed

.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_APP_BASE_URL='http://localhost:8081/spring-admin'

.env.production

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_APP_BASE_URL=''

package-lock.json

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12+
"axios": "^1.7.7",
1213
"element-plus": "^2.8.4",
1314
"pinia": "^2.1.7",
1415
"vue": "^3.4.29",

src/composables/apis.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { GET, POST, PUT, DELETE } from '@/composables/base'
2+
3+
const login = async (data) => {
4+
const res = await POST('/public/login', data)
5+
}
6+
7+
export { login }

src/composables/base.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import axios from 'axios';
2+
3+
const { VITE_APP_BASE_URL } = import.meta.env
4+
const apiClient = axios.create({
5+
baseURL: `${VITE_APP_BASE_URL}`,
6+
// timeout: 1000,
7+
});
8+
9+
10+
apiClient.interceptors.request.use(
11+
config => {
12+
return config;
13+
},
14+
error => {
15+
return Promise.reject(error);
16+
}
17+
);
18+
19+
apiClient.interceptors.response.use(
20+
response => {
21+
return response;
22+
},
23+
error => {
24+
return Promise.reject(error);
25+
}
26+
);
27+
28+
const GET = async (url, params = {}, config = {}) => {
29+
try {
30+
const response = await apiClient.get(url, { params, ...config });
31+
return response;
32+
} catch (error) {
33+
throw error;
34+
}
35+
};
36+
37+
const POST = async (url, data, config = {}) => {
38+
try {
39+
const response = await apiClient.post(url, data, config);
40+
return response;
41+
} catch (error) {
42+
throw error;
43+
}
44+
};
45+
46+
const PUT = async (url, data, config = {}) => {
47+
try {
48+
const response = await apiClient.put(url, data, config);
49+
return response;
50+
} catch (error) {
51+
throw error;
52+
}
53+
};
54+
55+
const DELETE = async (url, config = {}) => {
56+
try {
57+
const response = await apiClient.delete(url, config);
58+
return response;
59+
} catch (error) {
60+
throw error;
61+
}
62+
};
63+
64+
65+
export { GET, POST, PUT, DELETE }

src/views/LoginView.vue

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<p class="text-sm text-gray-600 mt-1">請登入您的帳戶</p>
66
</div>
77
<el-form :model="form" :rules="rules" ref="loginForm" @submit.prevent="onSubmit" class="space-y-4">
8-
<el-form-item prop="username">
8+
<el-form-item prop="email">
99
<el-input
10-
v-model="form.username"
10+
v-model="form.email"
1111
placeholder="用戶名"
1212
prefix-icon="el-icon-user"
1313
class="w-full">
@@ -35,46 +35,44 @@
3535
</div>
3636
</template>
3737

38-
<script setup>
39-
import { ref, reactive } from 'vue'
40-
import { ElMessage } from 'element-plus'
38+
<script setup>
39+
import { ref } from 'vue'
40+
import { ElMessage } from 'element-plus'
41+
import { login } from '@/composables/apis'
42+
43+
const loginForm = ref(null)
44+
const form = ref({
45+
email: '',
46+
password: ''
47+
})
4148
42-
const loginForm = ref(null)
43-
const form = reactive({
44-
username: '',
45-
password: ''
46-
})
47-
48-
const rules = {
49-
username: [
50-
{ required: true, message: '請輸入用戶名', trigger: 'blur' }
51-
],
52-
password: [
53-
{ required: true, message: '請輸入密碼', trigger: 'blur' }
54-
]
55-
}
49+
const rules = {
50+
email: [
51+
{ required: true, message: '請輸入用戶名', trigger: 'blur' }
52+
],
53+
password: [
54+
{ required: true, message: '請輸入密碼', trigger: 'blur' }
55+
]
56+
}
5657
57-
const onSubmit = () => {
58-
loginForm.value.validate((valid) => {
59-
if (valid) {
60-
// 這裡處理登入邏輯
58+
const onSubmit = () => {
59+
loginForm.value.validate(async(valid) => {
60+
if (valid) {
61+
try {
62+
const res = await login(form.value)
63+
console.log(loginForm.value)
64+
console.log(form.value)
65+
console.log(res)
6166
ElMessage.success('登入成功!')
62-
// 登入成功後可以進行路由跳轉等操作
63-
} else {
64-
ElMessage.error('請正確填寫登入信息')
67+
} catch (error) {
68+
console.log(error)
6569
}
66-
})
67-
}
68-
69-
</script>
70-
71-
<style>
72-
/* 可以在這裡添加任何額外的樣式覆蓋 */
73-
/* .el-input__inner {
74-
bg-gray-50 border-gray-300;
75-
}
70+
71+
// 登入成功後可以進行路由跳轉等操作
72+
} else {
73+
ElMessage.error('請正確填寫登入信息')
74+
}
75+
})
76+
}
7677
77-
.el-button--primary {
78-
border-none;
79-
} */
80-
</style>
78+
</script>

vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import vue from '@vitejs/plugin-vue'
55

66
// https://vitejs.dev/config/
77
export default defineConfig({
8+
base:'/spring-admin/public/site/',
89
plugins: [
910
vue(),
1011
],

0 commit comments

Comments
 (0)