Skip to content

Add captcha ztl #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/lib/AjaxTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default {
this.loading = false;
})
.catch(err => {
this.$SegmentMessage.error(this, '[Ajax Table] Request Failed.');
this.$SegmentMessage.error(this, '[Ajax Table] Request Failed');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

console.log(err);
});
}
Expand Down
52 changes: 52 additions & 0 deletions src/components/lib/captcha.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<template>
<div class="captcha">
<el-input v-model="captcha_answer" placeholder="Solve Captcha"></el-input>
<img class="captcha-img" v-if="loaded" @click="refresh_captcha();" :src="img_url" />
</div>
</template>

<script>
import apiurl from './../../apiurl';

export default {
name: 'captcha',
data() {
return {
img_url: null,
captcha_answer: '',
loaded: false
};
},
watch: {
captcha_answer(val) {
this.$store.commit('setAnswer', {
val: val
});
}
},
methods: {
refresh_captcha() {
this.img_url = apiurl('/captcha/' + this.$store.state.captcha.captchaKey + '?sfid=' + String(Math.random()));
}
},
mounted() {
this.$store.commit('newCaptcha');
this.img_url = apiurl('/captcha/' + this.$store.state.captcha.captchaKey + '?sfid=' + String(Math.random()));
this.loaded = true;
}
};
</script>

<style scoped>
.captcha {
display: flex;
}

.captcha-img {
margin-left: 10px;
}

.captcha-img:hover {
cursor: pointer;
}
</style>
2 changes: 1 addition & 1 deletion src/components/problem/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default {
this.data_count = data.res;
})
.catch(err => {
this.$SegmentMessage.error(this, '[Problem List] Get List Length Failed.');
this.$SegmentMessage.error(this, '[Problem List] Get List Length Failed');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

console.log(err);
});
}
Expand Down
26 changes: 22 additions & 4 deletions src/components/user/register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<el-form-item prop="email">
<el-input type="email" v-model="ldata.email"></el-input>
</el-form-item>
<div class="icon-lable form-required"><i class="el-icon-check" /> Captcha</div>
<captcha ref="captchaElement" class="margin-bottom" />
<el-form-item>
<el-button type="primary" v-on:click="onSubmit();" :loading="buttonLoading">Register</el-button>
<el-button v-on:click="$store.state.user.showregister = false;">Cancel</el-button>
Expand All @@ -30,6 +32,7 @@

<script>
import apiurl from './../../apiurl';
import captcha from './../lib/captcha.vue';

export default {
name: 'UserRegister',
Expand Down Expand Up @@ -93,13 +96,18 @@ export default {
};
},
methods: {
refresh_captcha() {
this.$refs.captchaElement.refresh_captcha();
},
submit() {
this.buttonLoading = true;
this.$axios
.post(apiurl('/account'), {
username: this.ldata.username,
password: this.ldata.password,
email: this.ldata.email
email: this.ldata.email,
captcha_key: this.$store.state.captcha.captchaKey,
captcha_answer: this.$store.state.captcha.captchaAnswer
})
.then(() => {
this.$store.state.user.showregister = false;
Expand All @@ -112,6 +120,9 @@ export default {
if (err.request.status === 400) {
// HTTP 400 Bad Request
this.$SegmentMessage.error(this, JSON.parse(err.request.response).detail);
} else if (err.request.status === 406){
// HTTP 406 Not Acceptable
this.$SegmentMessage.error(this, JSON.parse(err.request.response).detail);
} else if (err.request.status === 409) {
// HTTP 409 Conflict
this.$SegmentMessage.error(this, 'Username has been taken');
Expand All @@ -122,6 +133,7 @@ export default {
// Unknown error
this.$SegmentMessage.error(this, 'Unknown error');
}
this.refresh_captcha();
this.buttonLoading = false;
});
},
Expand All @@ -137,13 +149,19 @@ export default {
reset() {
this.$refs['register'].resetFields();
}
},
components: {
captcha
}
};
</script>

<style scoped>
.form-required::before {
content: "*";
color: #f56c6c;
.form-required {
margin-bottom: 5px;
}

.margin-bottom {
margin-bottom: 20px;
}
</style>
5 changes: 3 additions & 2 deletions src/sfconfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
api: {
server: 'http://172.32.5.25:8000/api'
server: 'http://172.32.1.144:8000/api'
},
markdown: {
gfm: true,
Expand All @@ -9,5 +9,6 @@ export default {
pedantic: false,
smartLists: true,
smartypants: false,
}
},
captchaKeyMax: 2000000000
};
18 changes: 18 additions & 0 deletions src/store/captcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sfconfig from './../sfconfig';

const captchastore = {
state: {
captchaKey: null,
captchaAnswer: null
},
mutations: {
newCaptcha(state) {
state.captchaKey = Math.floor(Math.random() * sfconfig.captchaKeyMax) + 1;
},
setAnswer(state, data) {
state.captchaAnswer = data.val;
}
}
};

export default captchastore;
4 changes: 3 additions & 1 deletion src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import Vuex from 'vuex';
Vue.use(Vuex);

import userstore from './user';
import captchastore from './captcha';

export default new Vuex.Store({
modules: {
user: userstore
user: userstore,
captcha: captchastore
}
});