实现前端对学号和用户名的异步判断,核心技术为axios

This commit is contained in:
F嘉阳
2018-02-24 16:00:23 +08:00
parent e61800efb9
commit 99aca7d6c8
5 changed files with 128 additions and 10 deletions

View File

@@ -1,15 +1,20 @@
package com.fjy.spring.controller; package com.fjy.spring.controller;
import com.fjy.spring.domain.TbStudentlist;
import com.fjy.spring.domain.TbUser; import com.fjy.spring.domain.TbUser;
import com.fjy.spring.enums.ResultEnum; import com.fjy.spring.enums.ResultEnum;
import com.fjy.spring.exception.UserException; import com.fjy.spring.exception.UserException;
import com.fjy.spring.properties.ServerProperties; import com.fjy.spring.properties.ServerProperties;
import com.fjy.spring.service.StudentService;
import com.fjy.spring.service.UserService; import com.fjy.spring.service.UserService;
import com.fjy.spring.untils.CodingUtil; import com.fjy.spring.untils.CodingUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -22,6 +27,9 @@ public class RegisterController {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private StudentService studentService;
@Autowired @Autowired
private ServerProperties serverProperties; private ServerProperties serverProperties;
@@ -45,4 +53,23 @@ public class RegisterController {
throw new UserException(ResultEnum.UNKOWN_ERROR); throw new UserException(ResultEnum.UNKOWN_ERROR);
} }
@GetMapping("/CheckStudentNo")
@ResponseBody
public boolean doCheckStudentNo(@RequestParam(value = "studentno") String studentno){
TbStudentlist studentlist = studentService.findStudentByNo(studentno);
if (studentlist!=null)
return true;
return false;
}
@GetMapping("/CheckStudent")
@ResponseBody
public boolean doCheckStudent(@RequestParam(value = "studentno") String studentno,
@RequestParam(value = "realname") String realname){
TbStudentlist studentlist = studentService.findByColstudentnoAndColrealname(studentno,realname);
if (studentlist!=null)
return true;
return false;
}
} }

View File

@@ -0,0 +1,11 @@
package com.fjy.spring.repository;
import com.fjy.spring.domain.TbStudentlist;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TbStudentListRepository extends JpaRepository<TbStudentlist,Integer> {
public TbStudentlist findByColstudentno(String colstudentno);
public TbStudentlist findByColstudentnoAndColrealname(String colstudentno,String colrealname);
}

View File

@@ -1,7 +1,9 @@
package com.fjy.spring.service; package com.fjy.spring.service;
import com.fjy.spring.domain.TbStudent; import com.fjy.spring.domain.TbStudent;
import com.fjy.spring.domain.TbStudentlist;
import com.fjy.spring.domain.TbUser; import com.fjy.spring.domain.TbUser;
import com.fjy.spring.repository.TbStudentListRepository;
import com.fjy.spring.repository.TbStudentRepository; import com.fjy.spring.repository.TbStudentRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -13,4 +15,15 @@ public class StudentService {
@Autowired @Autowired
private TbStudentRepository tbStudentRepository; private TbStudentRepository tbStudentRepository;
@Autowired
private TbStudentListRepository tbStudentListRepository;
public TbStudentlist findStudentByNo(String studentno){
return tbStudentListRepository.findByColstudentno(studentno);
}
public TbStudentlist findByColstudentnoAndColrealname(String studentno,String realname){
return tbStudentListRepository.findByColstudentnoAndColrealname(studentno,realname);
}
} }

View File

@@ -3,22 +3,57 @@ var Main = {
var checkName = (rule, value, callback) => { var checkName = (rule, value, callback) => {
if (!value) { if (!value) {
return callback(new Error('用户名不能为空')); return callback(new Error('用户名不能为空'));
}else { } else {
callback() callback()
} }
}; };
var checkNo = (rule, value, callback) => { var checkNo = (rule, value, callback) => {
if (!value) { if (!value) {
return callback(new Error('学号不能为空')); return callback(new Error('学号不能为空'));
}else { } else {
callback() //判断是否为指定班级的合法用户
axios.get(getRootPath_web() + '/CheckStudentNo', {
params: {
studentno: value
}
})
.then(function (response) {
console.log(response.data);
if (response.data === true) {
callback()
} else {
return callback(new Error('学号非法'));
}
})
.catch(function (error) {
console.log(error);
this.errorNotify(error.message);
});
} }
}; };
var checkRealName = (rule, value, callback) => { var checkRealName = (rule, value, callback) => {
if (!value) { if (!value) {
return callback(new Error('真实姓名不能为空')); return callback(new Error('真实姓名不能为空'));
}else { } else {
callback() //判断用户名与学号是否匹配
axios.get(getRootPath_web() + '/CheckStudent', {
params: {
realname: value,
studentno: this.ruleForm2.colstudentno
}
})
.then(function (response) {
console.log(response.data);
if (response.data === false) {
return callback(new Error('姓名与学号不匹配'));
} else {
callback()
}
})
.catch(function (error) {
console.log(error);
this.errorNotify(error.message);
});
} }
}; };
var validatePass = (rule, value, callback) => { var validatePass = (rule, value, callback) => {
@@ -44,17 +79,17 @@ var Main = {
ruleForm2: { ruleForm2: {
colname: '', colname: '',
colpassword: '', colpassword: '',
checkPass:'', checkPass: '',
colstudentno: '', colstudentno: '',
colrealname: '', colrealname: '',
colemail: '' colemail: ''
}, },
rules2: { rules2: {
colpassword: [ colpassword: [
{required: true,validator: validatePass, trigger: 'blur'} {required: true, validator: validatePass, trigger: 'blur'}
], ],
checkPass: [ checkPass: [
{required: true,validator: validatePass2, trigger: 'blur'} {required: true, validator: validatePass2, trigger: 'blur'}
], ],
colstudentno: [ colstudentno: [
{ {
@@ -71,13 +106,19 @@ var Main = {
} }
], ],
colname: [ colname: [
{required: true,validator: checkName, trigger: 'blur'} {required: true, validator: checkName, trigger: 'blur'}
], ],
}, },
activeName:'login', activeName: 'login',
}; };
}, },
methods: { methods: {
errorNotify(content) {
this.$notify.error({
title: '错误',
message: content
})
},
submitForm(formName) { submitForm(formName) {
this.$refs[formName].validate((valid) => { this.$refs[formName].validate((valid) => {
if (valid) { if (valid) {

View File

@@ -35,4 +35,30 @@ public class RegisterControllerTest {
" \"data\": null\n" + " \"data\": null\n" +
"}")); "}"));
} }
@Test
public void doCheckStudentNo()throws Exception {
//测试非法学号检查
mvc.perform(MockMvcRequestBuilders.post("/CheckStudentNo")
.param("studentno","0003"))
.andExpect(MockMvcResultMatchers.content().string("false"));
//测试合法学号检查
mvc.perform(MockMvcRequestBuilders.post("/CheckStudentNo")
.param("studentno","15251101238"))
.andExpect(MockMvcResultMatchers.content().string("true"));
}
@Test
public void doCheckStudent()throws Exception {
//测试学号与姓名不匹配
mvc.perform(MockMvcRequestBuilders.post("/CheckStudent")
.param("studentno","15251101238")
.param("realname","符嘉"))
.andExpect(MockMvcResultMatchers.content().string("false"));
//测试学号与姓名匹配
mvc.perform(MockMvcRequestBuilders.post("/CheckStudent")
.param("studentno","15251101238")
.param("realname","符嘉阳"))
.andExpect(MockMvcResultMatchers.content().string("true"));
}
} }