Skip to content

Commit beea44a

Browse files
author
biao.chen
committed
feat: 用组合式api重构登录与注册
1 parent 1e2e103 commit beea44a

File tree

7 files changed

+1059
-1088
lines changed

7 files changed

+1059
-1088
lines changed

src/App.vue

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,11 @@ export default defineComponent({
3434
Footer: defineAsyncComponent(() => import("./components/Footer.vue")),
3535
ArrowUp: defineAsyncComponent(() => import("./components/Footer.vue")),
3636
},
37-
// data() {
38-
// return {
39-
// isShowNav: false,
40-
// isShowSlider: false,
41-
// };
42-
// },
4337
watch: {
4438
$route: function (val: any, oldVal: any) {
4539
this.routeChange(val, oldVal);
4640
},
4741
},
48-
methods: {
49-
// routeChange(val: any, oldVal: any): void {
50-
// const referrer: any = document.getElementById("referrer");
51-
// if (val.path === "/") {
52-
// this.isShowNav = false;
53-
// referrer.setAttribute("content", "always");
54-
// } else {
55-
// this.isShowNav = true;
56-
// referrer.setAttribute("content", "never");
57-
// }
58-
// if (
59-
// val.path === "/articles" ||
60-
// val.path === "/archive" ||
61-
// val.path === "/project" ||
62-
// val.path === "/timeline" ||
63-
// val.path === "/message"
64-
// ) {
65-
// this.isShowSlider = true;
66-
// } else {
67-
// this.isShowSlider = false;
68-
// }
69-
// if (isMobileOrPc()) {
70-
// this.isShowSlider = false;
71-
// }
72-
// },
73-
},
74-
// mounted(): void {
75-
// this.routeChange(this.$route, this.$route);
76-
// },
7742
setup() {
7843
console.log("setup ");
7944
@@ -82,7 +47,7 @@ export default defineComponent({
8247
isShowSlider: false,
8348
});
8449
85-
const router = useRouter();
50+
// const router = useRouter();
8651
const route = useRoute();
8752
8853
const routeChange = (val: any, oldVal: any): void => {
@@ -126,7 +91,6 @@ export default defineComponent({
12691
return {
12792
state,
12893
routeChange,
129-
// click
13094
};
13195
},
13296
});
@@ -142,8 +106,7 @@ export default defineComponent({
142106
-moz-osx-font-smoothing: grayscale;
143107
text-align: left;
144108
color: #2c3e50;
145-
width: 1200px;
146-
margin: 0 auto;
109+
// width: 1200px;
147110
padding-top: 61px;
148111
}
149112
img {

src/components/Nav.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,11 @@
191191

192192
<script lang="ts">
193193
import { defineComponent, defineAsyncComponent } from "vue";
194+
import { useStore } from 'vuex'
194195
import { isMobileOrPc, getQueryStringByName } from "../utils/utils";
195196
import { UserInfo, NavListItem } from "../types/index";
197+
import service from "../utils/https";
198+
import urls from "./utils/urls";
196199
197200
export default defineComponent({
198201
name: "Nav",

src/components/RegisterAndLogin.vue

Lines changed: 89 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
11
<template>
22
<el-dialog
33
title="登录"
4-
:width="isMobile ? '90%' : '50%'"
5-
v-model="dialogDodel"
4+
:width="state.isMobile ? '90%' : '50%'"
5+
v-model="state.dialogDodel"
66
@close="cancel"
77
:show-close="true"
88
>
99
<el-form>
1010
<el-formItem
1111
label="邮箱"
12-
:label-width="formLabelWidth"
12+
:label-width="state.formLabelWidth"
1313
>
1414
<el-input
15-
v-model="params.email"
15+
v-model="state.params.email"
1616
placeholder="邮箱"
1717
autocomplete="off"
1818
>
1919
</el-input>
2020
</el-formItem>
2121
<el-formItem
2222
label="密码"
23-
:label-width="formLabelWidth"
23+
:label-width="state.formLabelWidth"
2424
>
2525
<el-input
2626
type="password"
2727
placeholder="密码"
28-
v-model="params.password"
28+
v-model="state.params.password"
2929
autocomplete="off"
3030
></el-input>
3131
</el-formItem>
3232
<el-formItem
3333
v-if="handleFlag === 'register'"
3434
label="昵称"
35-
:label-width="formLabelWidth"
35+
:label-width="state.formLabelWidth"
3636
>
3737
<el-input
38-
v-model="params.name"
38+
v-model="state.params.name"
3939
placeholder="用户名或昵称"
4040
autocomplete="off"
4141
></el-input>
4242
</el-formItem>
4343
<el-formItem
4444
v-if="handleFlag === 'register'"
4545
label="手机"
46-
:label-width="formLabelWidth"
46+
:label-width="state.formLabelWidth"
4747
>
4848
<el-input
49-
v-model="params.phone"
49+
v-model="state.params.phone"
5050
placeholder="手机号"
5151
autocomplete="off"
5252
></el-input>
5353
</el-formItem>
5454
<el-formItem
5555
v-if="handleFlag === 'register'"
5656
label="简介"
57-
:label-width="formLabelWidth"
57+
:label-width="state.formLabelWidth"
5858
>
5959
<el-input
60-
v-model="params.desc"
60+
v-model="state.params.desc"
6161
placeholder="个人简介"
6262
autocomplete="off"
6363
></el-input>
@@ -73,13 +73,13 @@
7373
>github 授权登录</el-button>
7474
<el-button
7575
v-if="handleFlag === 'login'"
76-
:loading="btnLoading"
76+
:loading="state.btnLoading"
7777
type="primary"
7878
@click="handleOk"
7979
>登 录</el-button>
8080
<el-button
8181
v-if="handleFlag === 'register'"
82-
:loading="btnLoading"
82+
:loading="state.btnLoading"
8383
type="primary"
8484
@click="handleOk"
8585
>注 册</el-button>
@@ -88,9 +88,15 @@
8888
</template>
8989

9090
<script lang="ts">
91-
import { defineComponent } from "vue";
91+
import { defineComponent, reactive, watch, onMounted } from "vue";
92+
import { useStore } from "vuex";
93+
import { useRoute } from "vue-router";
94+
import { ElMessage } from "element-plus";
95+
import { key } from '../store'
9296
import config from "../utils/config";
9397
import { RegAndLogParams, UserInfo } from "../types/index";
98+
import service from "../utils/https";
99+
import urls from "../utils/urls";
94100
95101
export default defineComponent({
96102
name: "RegisterAndLogin",
@@ -109,131 +115,120 @@ export default defineComponent({
109115
},
110116
},
111117
emits: ["ok", "cancel"],
112-
data() {
113-
return {
114-
dialogDodel: this.visible,
118+
setup(props, context) {
119+
const store = useStore(key);
120+
const state = reactive({
121+
dialogDodel: props.visible,
115122
btnLoading: false,
116123
loading: false,
117-
formLabelWidth: this.isMobile ? "40px" : "60px",
124+
formLabelWidth: props.isMobile ? "40px" : "60px",
118125
params: {
119126
email: "",
120127
name: "",
121128
password: "",
122129
phone: "",
123130
desc: "",
124131
} as RegAndLogParams,
125-
};
126-
},
127-
watch: {
128-
dialogDodel: {
129-
handler(val: any, oldVal: any) {
130-
this.$emit("cancel", val);
131-
},
132-
immediate: true,
133-
},
134-
visible: {
135-
handler(val: any, oldVal: any) {
136-
this.dialogDodel = val;
137-
},
138-
immediate: true,
139-
},
140-
},
141-
// computed: {
142-
// dialogVisible(): boolean {
143-
// return this.visible;
144-
// },
145-
// },
146-
methods: {
147-
handleOAuth(): void {
132+
});
133+
134+
const route = useRoute();
135+
const handleOAuth = (): void => {
148136
// 保存授权前的页面链接内容
149137
let preventHistory: object = {
150-
name: this.$route.name,
151-
query: this.$route.query,
138+
name: route.name,
139+
query: route.query,
152140
};
153141
window.sessionStorage.preventHistory = JSON.stringify(preventHistory);
154142
// window.location.href = 'https://github.com/login/oauth/authorize?client_id=6de90ab270aea2bdb01c&redirect_uri=http://biaochenxuying.cn/login'
155143
window.location.href = `${config.oauth_uri}?client_id=${config.client_id}&redirect_uri=${config.redirect_uri}`;
156-
},
157-
handleOk(): void {
144+
};
145+
146+
const submit = async (): Promise<void> => {
147+
let data: any = "";
148+
state.btnLoading = true;
149+
if (props.handleFlag === "register") {
150+
data = await service.post(urls.register, state.params);
151+
} else {
152+
data = await service.post(urls.login, state.params);
153+
}
154+
state.btnLoading = false;
155+
156+
const userInfo: UserInfo = {
157+
_id: data._id,
158+
name: data.name,
159+
avatar: data.avatar,
160+
};
161+
store.commit("SAVE_USER", {
162+
userInfo,
163+
});
164+
window.sessionStorage.userInfo = JSON.stringify(userInfo);
165+
context.emit("ok", false);
166+
ElMessage({
167+
message: "操作成功",
168+
type: "success",
169+
});
170+
};
171+
172+
const handleOk = (): void => {
158173
const reg = new RegExp(
159174
"^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"
160175
); //正则表达式
161-
if (!this.params.email) {
162-
// Message.warning("邮箱不能为空!");
163-
(this as any).$message({
176+
if (!state.params.email) {
177+
ElMessage({
164178
message: "邮箱不能为空!",
165179
type: "warning",
166180
});
167181
return;
168-
} else if (!reg.test(this.params.email)) {
169-
(this as any).$message({
182+
} else if (!reg.test(state.params.email)) {
183+
ElMessage({
170184
message: "请输入格式正确的邮箱!",
171185
type: "warning",
172186
});
173187
return;
174188
}
175-
if (this.handleFlag === "register") {
176-
if (!this.params.password) {
177-
(this as any).$message({
189+
if (props.handleFlag === "register") {
190+
if (!state.params.password) {
191+
ElMessage({
178192
message: "密码不能为空!",
179193
type: "warning",
180194
});
181195
return;
182-
} else if (!this.params.name) {
183-
(this as any).$message({
196+
} else if (!state.params.name) {
197+
ElMessage({
184198
message: "用户名不能为空!",
185199
type: "warning",
186200
});
187201
return;
188202
}
189203
const re = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
190-
if (this.params.phone && !re.test(this.params.phone)) {
191-
(this as any).$message({
204+
if (state.params.phone && !re.test(state.params.phone)) {
205+
ElMessage({
192206
message: "请输入正确的手机号!",
193207
type: "warning",
194208
});
195209
return;
196210
}
197211
}
198-
this.submit();
199-
},
200-
cancel(): boolean {
201-
this.$emit("cancel", false);
212+
submit();
213+
};
214+
215+
const cancel = (): boolean => {
216+
context.emit("cancel", false);
202217
return false;
203-
},
204-
async submit(): Promise<void> {
205-
let data: any = "";
206-
this.btnLoading = true;
207-
if (this.handleFlag === "register") {
208-
data = await (this as any).$https.post(
209-
(this as any).$urls.register,
210-
this.params
211-
);
212-
} else {
213-
data = await (this as any).$https.post(
214-
(this as any).$urls.login,
215-
this.params
216-
);
217-
}
218-
this.btnLoading = false;
218+
};
219219
220-
const userInfo: UserInfo = {
221-
_id: data._id,
222-
name: data.name,
223-
avatar: data.avatar,
224-
};
225-
(this as any).$store.commit("SAVE_USER", {
226-
userInfo,
227-
});
228-
window.sessionStorage.userInfo = JSON.stringify(userInfo);
229-
this.$emit("ok", false);
230-
(this as any).$message({
231-
message: "操作成功",
232-
type: "success",
233-
});
234-
},
220+
watch(props, (val, oldVal) => {
221+
state.dialogDodel = val.visible;
222+
});
223+
224+
return {
225+
state,
226+
handleOAuth,
227+
handleOk,
228+
submit,
229+
cancel,
230+
};
235231
},
236-
setup() {},
237232
});
238233
</script>
239234
<style scoped>

0 commit comments

Comments
 (0)