Browse Source

update api

deason 1 year ago
parent
commit
91a6b04eae

+ 3 - 0
src/main/java/com/qmth/exam/reserve/bean/login/LoginUser.java

@@ -32,6 +32,9 @@ public class LoginUser implements AccessEntity, IModel {
     @ApiModelProperty(value = "角色")
     @ApiModelProperty(value = "角色")
     private Role role;
     private Role role;
 
 
+    @ApiModelProperty(value = "微信OID")
+    private String openId;
+
     @ApiModelProperty(value = "鉴权信息")
     @ApiModelProperty(value = "鉴权信息")
     private String sessionId;
     private String sessionId;
 
 

+ 3 - 1
src/main/java/com/qmth/exam/reserve/service/StudentService.java

@@ -7,7 +7,9 @@ import com.qmth.exam.reserve.entity.StudentEntity;
 
 
 public interface StudentService extends IService<StudentEntity> {
 public interface StudentService extends IService<StudentEntity> {
 
 
-    StudentEntity findStudentByStudentCode(Long orgId, String studentCode);
+    StudentEntity findByStudentCode(Long orgId, String studentCode);
+
+    StudentEntity findByOpenIdAndUid(String openId, String uid);
 
 
     StudentInfo findInfoByStudentId(Long studentId);
     StudentInfo findInfoByStudentId(Long studentId);
 
 

+ 23 - 2
src/main/java/com/qmth/exam/reserve/service/impl/AuthServiceImpl.java

@@ -86,7 +86,7 @@ public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthSer
         }
         }
 
 
         log.debug("[STUDENT_LOGIN] verifying, account:{}", req.getAccount());
         log.debug("[STUDENT_LOGIN] verifying, account:{}", req.getAccount());
-        StudentEntity student = studentService.findStudentByStudentCode(req.getOrgId(), req.getAccount());
+        StudentEntity student = studentService.findByStudentCode(req.getOrgId(), req.getAccount());
         if (student == null) {
         if (student == null) {
             throw new StatusException("登录用户不存在");
             throw new StatusException("登录用户不存在");
         }
         }
@@ -103,6 +103,7 @@ public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthSer
         loginUser.setAccount(student.getStudentCode());
         loginUser.setAccount(student.getStudentCode());
         loginUser.setName(student.getName());
         loginUser.setName(student.getName());
         loginUser.setRole(Role.STUDENT);
         loginUser.setRole(Role.STUDENT);
+        loginUser.setOpenId(student.getOpenId());
 
 
         loginUser.setSessionId(CacheConstants.CACHE_STUDENT_LOGIN + student.getId());
         loginUser.setSessionId(CacheConstants.CACHE_STUDENT_LOGIN + student.getId());
         loginUser.setToken(FastUUID.get());
         loginUser.setToken(FastUUID.get());
@@ -118,7 +119,27 @@ public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthSer
             throw new StatusException("微信OID不能为空");
             throw new StatusException("微信OID不能为空");
         }
         }
 
 
-        return null;
+        log.debug("[WECHAT_LOGIN] verifying, openId:{} uid:{}", req.getOpenId(), req.getUid());
+        StudentEntity student = studentService.findByOpenIdAndUid(req.getOpenId(), req.getUid());
+        if (student == null) {
+            throw new StatusException("登录用户不存在");
+        }
+
+        LoginUser loginUser = new LoginUser();
+        loginUser.setId(student.getId());
+        loginUser.setOrgId(student.getOrgId());
+        loginUser.setCategoryId(student.getCategoryId());
+        loginUser.setAccount(student.getStudentCode());
+        loginUser.setName(student.getName());
+        loginUser.setRole(Role.STUDENT);
+        loginUser.setOpenId(student.getOpenId());
+
+        loginUser.setSessionId(CacheConstants.CACHE_STUDENT_LOGIN + student.getId());
+        loginUser.setToken(FastUUID.get());
+        loginSessionManager.addLoginSession(loginUser);
+
+        log.info("[WECHAT_LOGIN] success! account:{} {}", loginUser.getAccount(), loginUser.getName());
+        return loginUser;
     }
     }
 
 
     @Override
     @Override

+ 48 - 3
src/main/java/com/qmth/exam/reserve/service/impl/StudentServiceImpl.java

@@ -8,6 +8,7 @@ import com.qmth.exam.reserve.bean.student.WechatBindReq;
 import com.qmth.exam.reserve.dao.StudentDao;
 import com.qmth.exam.reserve.dao.StudentDao;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.service.StudentService;
 import com.qmth.exam.reserve.service.StudentService;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -18,12 +19,32 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
     private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);
     private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);
 
 
     @Override
     @Override
-    public StudentEntity findStudentByStudentCode(Long orgId, String studentCode) {
+    public StudentEntity findByStudentCode(Long orgId, String studentCode) {
+        if (StringUtils.isEmpty(studentCode)) {
+            throw new StatusException("学号不能为空");
+        }
+
         QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
         QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
+        wrapper.lambda().eq(StudentEntity::getStudentCode, studentCode);
         if (orgId != null) {
         if (orgId != null) {
             wrapper.lambda().eq(StudentEntity::getOrgId, orgId);
             wrapper.lambda().eq(StudentEntity::getOrgId, orgId);
         }
         }
-        wrapper.lambda().eq(StudentEntity::getStudentCode, studentCode);
+
+        return baseMapper.selectOne(wrapper);
+    }
+
+    @Override
+    public StudentEntity findByOpenIdAndUid(String openId, String uid) {
+        if (StringUtils.isEmpty(openId)) {
+            throw new StatusException("微信OID不能为空");
+        }
+
+        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
+        wrapper.lambda().eq(StudentEntity::getOpenId, openId);
+        if (StringUtils.isNotEmpty(uid)) {
+            wrapper.lambda().eq(StudentEntity::getUid, uid);
+        }
+
         return baseMapper.selectOne(wrapper);
         return baseMapper.selectOne(wrapper);
     }
     }
 
 
@@ -31,7 +52,7 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
     public StudentInfo findInfoByStudentId(Long studentId) {
     public StudentInfo findInfoByStudentId(Long studentId) {
         StudentEntity entity = baseMapper.selectById(studentId);
         StudentEntity entity = baseMapper.selectById(studentId);
         if (entity == null) {
         if (entity == null) {
-            throw new StatusException("学生不存在");
+            throw new StatusException("学生信息不存在");
         }
         }
 
 
         StudentInfo info = new StudentInfo();
         StudentInfo info = new StudentInfo();
@@ -52,12 +73,36 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
 
 
     @Override
     @Override
     public void bindingWechat(WechatBindReq req) {
     public void bindingWechat(WechatBindReq req) {
+        StudentEntity stu = baseMapper.selectById(req.getStudentId());
+        if (stu == null) {
+            throw new StatusException("学生信息不存在");
+        }
 
 
+        if (StringUtils.isNotEmpty(stu.getOpenId())) {
+            throw new StatusException("微信账号已绑定");
+        }
+
+        stu.setOpenId(req.getOpenId());
+        stu.setUid(req.getUid());
+        baseMapper.updateById(stu);
+        log.info("[WECHAT_BINDING] studentId:{} openId:{} uid:{}", stu.getId(), stu.getOpenId(), stu.getUid());
     }
     }
 
 
     @Override
     @Override
     public void unbindWechatByStudentId(Long studentId) {
     public void unbindWechatByStudentId(Long studentId) {
+        StudentEntity stu = baseMapper.selectById(studentId);
+        if (stu == null) {
+            throw new StatusException("学生信息不存在");
+        }
+
+        log.info("[WECHAT_UNBIND] studentId:{} openId:{} uid:{}", stu.getId(), stu.getOpenId(), stu.getUid());
+        if (StringUtils.isEmpty(stu.getOpenId())) {
+            return;
+        }
 
 
+        stu.setOpenId(null);
+        stu.setUid(null);
+        baseMapper.updateById(stu);
     }
     }
 
 
 }
 }

+ 10 - 9
src/test/java/com/qmth/exam/reserve/test/AuthTest.java

@@ -18,16 +18,16 @@ public class AuthTest {
 
 
     @Test
     @Test
     public void demo() throws Exception {
     public void demo() throws Exception {
-        // LoginUser loginUser = this.doLogin();
-        // this.doLogout(loginUser.getSessionId(), loginUser.getToken());
-        this.doLogout("S_1", "000000001efe1d02000000000dce14e5");
+        // LoginUser loginUser = this.loginTest("1500010120019", "123456");
+        // this.logoutTest(loginUser.getSessionId(), loginUser.getToken());
+        this.apiTest("xxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
     }
     }
 
 
-    private LoginUser doLogin() {
+    private LoginUser loginTest(String account, String password) {
         String apiUrl = "/api/student/login";
         String apiUrl = "/api/student/login";
         Map<String, Object> params = new HashMap<>();
         Map<String, Object> params = new HashMap<>();
-        params.put("account", "1500010120019");
-        params.put("password", "123456");
+        params.put("account", account);
+        params.put("password", password);
 
 
         String jsonParams = JsonHelper.toJson(params);
         String jsonParams = JsonHelper.toJson(params);
         RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=UTF-8"), jsonParams);
         RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=UTF-8"), jsonParams);
@@ -49,9 +49,10 @@ public class AuthTest {
         throw new RuntimeException("登录失败!");
         throw new RuntimeException("登录失败!");
     }
     }
 
 
-    private void doLogout(String sessionId, String token) {
-        String apiUrl = "/api/student/logout";
-        // String apiUrl = "/api/student/info";
+    private void apiTest(String sessionId, String token) {
+        // String apiUrl = "/api/student/logout";
+        String apiUrl = "/api/student/info";
+
         long timestamp = System.currentTimeMillis();
         long timestamp = System.currentTimeMillis();
         String signature = SignatureEntity.build(SignatureType.TOKEN, "POST", apiUrl,
         String signature = SignatureEntity.build(SignatureType.TOKEN, "POST", apiUrl,
                 timestamp, sessionId, token);
                 timestamp, sessionId, token);