Skip to content

Commit 93052ba

Browse files
authored
Merge pull request #71 from segment-oj/add-captcha-ztl
Add captcha ztl
2 parents da0bac5 + 01669b0 commit 93052ba

File tree

7 files changed

+100
-9
lines changed

7 files changed

+100
-9
lines changed

src/components/lib/AjaxTable.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default {
4444
this.loading = false;
4545
})
4646
.catch(err => {
47-
this.$SegmentMessage.error(this, '[Ajax Table] Request Failed.');
47+
this.$SegmentMessage.error(this, '[Ajax Table] Request Failed');
4848
console.log(err);
4949
});
5050
}

src/components/lib/captcha.vue

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<template>
2+
<div class="captcha">
3+
<el-input v-model="captcha_answer" placeholder="Solve Captcha"></el-input>
4+
<img class="captcha-img" v-if="loaded" @click="refresh_captcha();" :src="img_url" />
5+
</div>
6+
</template>
7+
8+
<script>
9+
import apiurl from './../../apiurl';
10+
11+
export default {
12+
name: 'captcha',
13+
data() {
14+
return {
15+
img_url: null,
16+
captcha_answer: '',
17+
loaded: false
18+
};
19+
},
20+
watch: {
21+
captcha_answer(val) {
22+
this.$store.commit('setAnswer', {
23+
val: val
24+
});
25+
}
26+
},
27+
methods: {
28+
refresh_captcha() {
29+
this.img_url = apiurl('/captcha/' + this.$store.state.captcha.captchaKey + '?sfid=' + String(Math.random()));
30+
}
31+
},
32+
mounted() {
33+
this.$store.commit('newCaptcha');
34+
this.img_url = apiurl('/captcha/' + this.$store.state.captcha.captchaKey + '?sfid=' + String(Math.random()));
35+
this.loaded = true;
36+
}
37+
};
38+
</script>
39+
40+
<style scoped>
41+
.captcha {
42+
display: flex;
43+
}
44+
45+
.captcha-img {
46+
margin-left: 10px;
47+
}
48+
49+
.captcha-img:hover {
50+
cursor: pointer;
51+
}
52+
</style>

src/components/problem/list.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default {
6767
this.data_count = data.res;
6868
})
6969
.catch(err => {
70-
this.$SegmentMessage.error(this, '[Problem List] Get List Length Failed.');
70+
this.$SegmentMessage.error(this, '[Problem List] Get List Length Failed');
7171
console.log(err);
7272
});
7373
}

src/components/user/register.vue

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<el-form-item prop="email">
1919
<el-input type="email" v-model="ldata.email"></el-input>
2020
</el-form-item>
21+
<div class="icon-lable form-required"><i class="el-icon-check" /> Captcha</div>
22+
<captcha ref="captchaElement" class="margin-bottom" />
2123
<el-form-item>
2224
<el-button type="primary" v-on:click="onSubmit();" :loading="buttonLoading">Register</el-button>
2325
<el-button v-on:click="$store.state.user.showregister = false;">Cancel</el-button>
@@ -30,6 +32,7 @@
3032

3133
<script>
3234
import apiurl from './../../apiurl';
35+
import captcha from './../lib/captcha.vue';
3336
3437
export default {
3538
name: 'UserRegister',
@@ -93,13 +96,18 @@ export default {
9396
};
9497
},
9598
methods: {
99+
refresh_captcha() {
100+
this.$refs.captchaElement.refresh_captcha();
101+
},
96102
submit() {
97103
this.buttonLoading = true;
98104
this.$axios
99105
.post(apiurl('/account'), {
100106
username: this.ldata.username,
101107
password: this.ldata.password,
102-
email: this.ldata.email
108+
email: this.ldata.email,
109+
captcha_key: this.$store.state.captcha.captchaKey,
110+
captcha_answer: this.$store.state.captcha.captchaAnswer
103111
})
104112
.then(() => {
105113
this.$store.state.user.showregister = false;
@@ -112,6 +120,9 @@ export default {
112120
if (err.request.status === 400) {
113121
// HTTP 400 Bad Request
114122
this.$SegmentMessage.error(this, JSON.parse(err.request.response).detail);
123+
} else if (err.request.status === 406){
124+
// HTTP 406 Not Acceptable
125+
this.$SegmentMessage.error(this, JSON.parse(err.request.response).detail);
115126
} else if (err.request.status === 409) {
116127
// HTTP 409 Conflict
117128
this.$SegmentMessage.error(this, 'Username has been taken');
@@ -122,6 +133,7 @@ export default {
122133
// Unknown error
123134
this.$SegmentMessage.error(this, 'Unknown error');
124135
}
136+
this.refresh_captcha();
125137
this.buttonLoading = false;
126138
});
127139
},
@@ -137,13 +149,19 @@ export default {
137149
reset() {
138150
this.$refs['register'].resetFields();
139151
}
152+
},
153+
components: {
154+
captcha
140155
}
141156
};
142157
</script>
143158

144159
<style scoped>
145-
.form-required::before {
146-
content: "*";
147-
color: #f56c6c;
160+
.form-required {
161+
margin-bottom: 5px;
162+
}
163+
164+
.margin-bottom {
165+
margin-bottom: 20px;
148166
}
149167
</style>

src/sfconfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
22
api: {
3-
server: 'http://172.32.5.25:8000/api'
3+
server: 'http://172.32.1.144:8000/api'
44
},
55
markdown: {
66
gfm: true,
@@ -9,5 +9,6 @@ export default {
99
pedantic: false,
1010
smartLists: true,
1111
smartypants: false,
12-
}
12+
},
13+
captchaKeyMax: 2000000000
1314
};

src/store/captcha.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sfconfig from './../sfconfig';
2+
3+
const captchastore = {
4+
state: {
5+
captchaKey: null,
6+
captchaAnswer: null
7+
},
8+
mutations: {
9+
newCaptcha(state) {
10+
state.captchaKey = Math.floor(Math.random() * sfconfig.captchaKeyMax) + 1;
11+
},
12+
setAnswer(state, data) {
13+
state.captchaAnswer = data.val;
14+
}
15+
}
16+
};
17+
18+
export default captchastore;

src/store/store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import Vuex from 'vuex';
44
Vue.use(Vuex);
55

66
import userstore from './user';
7+
import captchastore from './captcha';
78

89
export default new Vuex.Store({
910
modules: {
10-
user: userstore
11+
user: userstore,
12+
captcha: captchastore
1113
}
1214
});

0 commit comments

Comments
 (0)