Skip to content

Commit 990cee9

Browse files
完成作業
1 parent 5a80016 commit 990cee9

File tree

15 files changed

+702
-280
lines changed

15 files changed

+702
-280
lines changed

src/assets/images/empty.png

10.9 KB
Loading

src/assets/js/api.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const apiBase = 'https://todolist-api.hexschool.io';
44
const apiUsers = `${apiBase}/users`;
55
const apiTodos = `${apiBase}/todos`;
66

7+
78
const getAuthHeader = (token) => {
89
return {
910
headers: {
@@ -23,6 +24,7 @@ const getAuthHeader = (token) => {
2324
*/
2425
const postSignUp = (data) => axios.post(`${apiUsers}/sign_up`, data);
2526

27+
2628
/**
2729
* 登入。
2830
* @param {Object} data
@@ -32,6 +34,7 @@ const postSignUp = (data) => axios.post(`${apiUsers}/sign_up`, data);
3234
*/
3335
const postSignIn = (data) => axios.post(`${apiUsers}/sign_in`, data);
3436

37+
3538
/**
3639
* 登出。
3740
* @param {String} token
@@ -41,67 +44,71 @@ const postSignOut = (token) =>
4144
axios({
4245
method: 'post',
4346
url: `${apiUsers}/sign_out`,
44-
// headers: {
45-
// authorization: token,
46-
// },
4747
...getAuthHeader(token),
4848
});
4949

50+
5051
/**
5152
* 驗證。
5253
* @param {String} token
5354
* @returns {Promise}
5455
*/
5556
const getCheckout = (token) => axios.get(`${apiUsers}/checkout`, getAuthHeader(token));
5657

58+
5759
/**
5860
* 取得所有代辦事項。
5961
* @param {String} token
6062
* @returns {Promise}
6163
*/
6264
const getTodos = (token) => axios.get(`${apiTodos}/`, getAuthHeader(token));
6365

66+
6467
/**
6568
* 新增代辦事項。
69+
* @param {String} token
6670
* @param {Object} data
6771
* @param {String} data.content
68-
* @param {String} token
6972
* @returns {Promise}
7073
*/
71-
const postTodos = (data, token) => axios.post(`${apiTodos}/`, data, getAuthHeader(token));
74+
const postTodos = (token, data) => axios.post(`${apiTodos}/`, data, getAuthHeader(token));
75+
7276

7377
/**
7478
* 更新代辦事項。
79+
* @param {String} token
7580
* @param {String} id - 代辦事項 id
7681
* @param {Object} data
7782
* @param {String} data.content
78-
* @param {String} token
7983
* @returns {Promise}
8084
*/
81-
const putTodos = (id, data, token) =>
85+
const putTodos = (token, id, data) =>
8286
axios.put(`${apiTodos}/${id}`, data, getAuthHeader(token));
8387

88+
8489
/**
8590
* 刪除代辦事項。
86-
* @param {String} id - 代辦事項 id
8791
* @param {String} token
92+
* @param {String} id - 代辦事項 id
8893
* @returns {Promise}
8994
*/
90-
const deleteTodos = (id, token) => axios.delete(`${apiTodos}/${id}`, getAuthHeader(token));
95+
const deleteTodos = (token, id) => axios.delete(`${apiTodos}/${id}`, getAuthHeader(token));
96+
9197

9298
/**
9399
* 切換代辦事項狀態。
94-
* @param {String} id - 代辦事項 id
95100
* @param {String} token
101+
* @param {String} id - 代辦事項 id
96102
* @returns {Promise}
97103
*/
98-
const patchTodosToggle = (id, token) =>
104+
const patchTodosToggle = (token, id) =>
99105
axios({
100106
method: 'patch',
101107
url: `${apiTodos}/${id}/toggle`,
102108
...getAuthHeader(token),
103109
});
104110

111+
105112
/**
106113
* 解析 API 傳回的 error。
107114
* @param {Object} error
@@ -112,6 +119,7 @@ const patchTodosToggle = (id, token) =>
112119
*/
113120
const parseError = (error) => error.response?.data?.message ?? error.message;
114121

122+
115123
export const api = {
116124
postSignUp,
117125
postSignIn,

src/assets/js/auth.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
11
import router from '@/router';
22
import { loginToken } from './loginToken';
3-
import { api } from './api';
43

54
/**
6-
* 執行登入動作。
5+
* 執行登入動作。
76
* @param {String} token - 使用者登入的 token。
87
*/
98
function login (token) {
109
loginToken.add(token); // 加入 token
1110
router.push({ name: 'index' }); // 跳轉主頁面
1211
}
1312

14-
// TODO
15-
// async function check () {
16-
// const token = loginToken.get();
17-
// console.log(`[check] token ==>`, token);
18-
19-
// let result = false;
20-
21-
// if (token) {
22-
// await api.getCheckout(token)
23-
// .then((res) => {
24-
// console.log(`[check] res ==>`, res);
25-
// result = true;
26-
// })
27-
// .catch((error) => {
28-
// console.error(`[check] error ==>`, error);
29-
// })
30-
// .finally(() => {
31-
// console.log(`[check] finally...`);
32-
// });
33-
// }
34-
35-
// console.log(`[check] end. result ==>`, result);
36-
// return result;
37-
// }
38-
39-
4013
export const auth = {
4114
login,
42-
// check,
4315
};

src/assets/js/cookie.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* @param {Date} options.expires - cookie 的 expires。
77
* @param {Number} options.maxAge - cookie 的 max-age。
88
*/
9-
function setCookie (name, val = '', options = {}) {
9+
function setCookie (name, val = '', options = {})
10+
{
1011
const { expires, maxAge } = options;
1112
let strCookie = `${name}=${val};`;
1213

@@ -28,20 +29,22 @@ function setCookie (name, val = '', options = {}) {
2829
* @param {String} name - cookie 名稱。
2930
* @returns 使用 cookie 的各種方法。
3031
*/
31-
export function useCookie (name) {
32-
32+
export function useCookie (name)
33+
{
3334
let settings = {
3435
value: '',
3536
};
3637

38+
3739
/**
3840
* 設定 cookie 內容。
3941
* @param {String} val - cookie 內容
4042
*/
4143
const setValue = (val) => {
4244
settings.value = val;
4345
}
44-
46+
47+
4548
/**
4649
* 設定 expires 日期時間。
4750
* @param {Date} date - expires 日期時間。
@@ -50,6 +53,7 @@ export function useCookie (name) {
5053
settings.expires = date.toUTCString();
5154
}
5255

56+
5357
/**
5458
* 設置 cookie。
5559
* @param {Object} [customSetting = settings] - 自訂的設定。
@@ -58,7 +62,8 @@ export function useCookie (name) {
5862
const setup = (customSetting = settings) => {
5963
setCookie(name, customSetting.value, customSetting);
6064
}
61-
65+
66+
6267
/**
6368
* 檢查此 cookie 是否存在。
6469
* @returns {Boolean} cookie 存在則回傳 true;否則回傳 false。
@@ -68,6 +73,7 @@ export function useCookie (name) {
6873
return reg.test(document.cookie);
6974
}
7075

76+
7177
/**
7278
* 取得 cookie 內容。
7379
* @returns {String} cookie 內容。
@@ -76,14 +82,16 @@ export function useCookie (name) {
7682
const reg = new RegExp(`(?:(?:^|.*;)\\s*${name}\\s*\\=\\s*([^;]*).*$)|^.*$`);
7783
return document.cookie.replace(reg, '$1');
7884
};
79-
85+
86+
8087
/**
8188
* 刪除 cookie。
8289
*/
8390
const remove = () => {
8491
setup({ maxAge: 0 });
8592
}
86-
93+
94+
8795
/**
8896
* 清除所有設定。
8997
* 僅是清除變數內容,不會修改、刪除 cookie 本身。
@@ -95,6 +103,7 @@ export function useCookie (name) {
95103
};
96104
}
97105

106+
98107
/**
99108
* 取得 settings 內容。
100109
* @returns {String} 格式化為 JSON 字串的 settings 內容。

src/assets/js/loginToken.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { useCookie } from "./cookie";
33
const name = 'todos_login';
44
const tokenCookie = useCookie(name);
55

6+
67
/**
78
* 將 token 加入 cookie。
89
* @param {String} token - 使用者登入時產生的 token。
910
*/
10-
const add = (token) => {
11+
const add = (token) =>
12+
{
1113
// 時效設定為第二天的凌晨 1 點
1214
const expirationDate = new Date();
1315
expirationDate.setDate(expirationDate.getDate() + 1);
@@ -20,12 +22,14 @@ const add = (token) => {
2022
tokenCookie.setup();
2123
};
2224

25+
2326
/**
2427
* 取得 token。
2528
* @returns {String} token
2629
*/
2730
const get = () => tokenCookie.getValue();
2831

32+
2933
/**
3034
* 檢查 token 是否存在。
3135
* @returns {Boolean} token 存在就回傳 true;否則回傳 false。
@@ -38,13 +42,15 @@ const isExist = () => {
3842
return false;
3943
}
4044

45+
4146
/**
4247
* 刪除 token。
4348
*/
4449
const remove = () => {
4550
tokenCookie.remove();
4651
};
4752

53+
4854
export const loginToken = {
4955
add,
5056
get,

src/components/LoadingPage.vue

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/components/Login.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<!-- eslint-disable vue/multi-word-component-names -->
22
<script setup>
3+
34
import { RouterLink } from 'vue-router';
45
import { ref } from 'vue';
56
import { api, auth } from '@/assets/js';
67
7-
// 登入資料
8+
// 資料 - 登入
89
const loginData = ref({
910
1011
password: '123456',
1112
});
1213
14+
1315
// 登入
14-
function signin() {
16+
function signin()
17+
{
1518
api.postSignIn(loginData.value)
1619
.then((res) => {
1720
auth.login(res.data.token);
@@ -20,31 +23,31 @@ function signin() {
2023
console.error(`[login] error ==>`, error);
2124
alert(api.parseError(error));
2225
});
23-
// .finally(() => {});
2426
}
27+
2528
</script>
2629

30+
2731
<template>
2832
<form class="formControls" @submit.prevent="signin">
33+
2934
<h2 class="formControls_txt">最實用的線上代辦事項服務</h2>
35+
3036
<label class="formControls_label" for="email">Email</label>
3137
<input
3238
v-model="loginData.email"
3339
class="formControls_input"
34-
type="email"
35-
id="email"
36-
name="email"
40+
type="email" id="email" name="email"
3741
placeholder="請輸入 email"
3842
required
3943
/>
4044
<span>此欄位不可留空</span>
45+
4146
<label class="formControls_label" for="pwd">密碼</label>
4247
<input
4348
v-model="loginData.password"
4449
class="formControls_input"
45-
type="password"
46-
name="pwd"
47-
id="pwd"
50+
type="password" name="pwd" id="pwd"
4851
placeholder="請輸入密碼"
4952
required
5053
/>

0 commit comments

Comments
 (0)