xiatian 2 năm trước cách đây
mục cha
commit
a3d962c9e4

+ 4 - 1
src/main/java/cn/com/qmth/mps/controller/AuthController.java

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 import com.qmth.boot.api.constant.ApiConstant;
 
+import cn.com.qmth.mps.bean.User;
 import cn.com.qmth.mps.service.AuthService;
 import cn.com.qmth.mps.vo.AdminLoginVo;
 import io.swagger.annotations.Api;
@@ -23,7 +24,7 @@ public class AuthController extends BaseController {
 	@ApiOperation(value = "管理端登录")
 	@PostMapping("login/admin")
 	public AdminLoginVo loginAdmin(@RequestParam String loginName, @RequestParam String password) {
-		return null;
+		return authService.loginAdmin(loginName,password);
 	}
 
 	@ApiOperation(value = "微信小程序登录")
@@ -35,6 +36,8 @@ public class AuthController extends BaseController {
 	@ApiOperation(value = "登出")
 	@PostMapping("logout")
 	public void logout() {
+		User user = getAccessUser();
+        authService.logout(user);
 	}
 
 }

+ 3 - 3
src/main/java/cn/com/qmth/mps/controller/SchoolController.java

@@ -12,9 +12,9 @@ import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.collection.PageResult;
 
-import cn.com.qmth.mps.entity.SchoolEntity;
 import cn.com.qmth.mps.vo.school.SchollDomain;
 import cn.com.qmth.mps.vo.school.SchoolQuery;
+import cn.com.qmth.mps.vo.school.SchoolVo;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 
@@ -31,13 +31,13 @@ public class SchoolController extends BaseController {
 
     @ApiOperation(value = "获取分页")
     @RequestMapping(value = "/page", method = RequestMethod.POST)
-    public PageResult<SchoolEntity> page(SchoolQuery query) {
+    public PageResult<SchoolVo> page(SchoolQuery query) {
         return null;
     }
     
     @ApiOperation(value = "获取信息")
     @RequestMapping(value = "/info", method = RequestMethod.POST)
-    public SchoolEntity info(@RequestParam Long id) {
+    public SchoolVo info(@RequestParam Long id) {
         return null;
     }
     

+ 5 - 0
src/main/java/cn/com/qmth/mps/service/AuthService.java

@@ -1,11 +1,16 @@
 package cn.com.qmth.mps.service;
 
+import cn.com.qmth.mps.bean.User;
 import cn.com.qmth.mps.vo.AdminLoginVo;
 
 public interface AuthService {
 
 	AdminLoginVo loginWxApp(String wxappCode, String phone);
 
+	AdminLoginVo loginAdmin(String loginName, String password);
+
+	void logout(User user);
+
 
 
 }

+ 49 - 8
src/main/java/cn/com/qmth/mps/service/impl/AuthServiceImpl.java

@@ -16,7 +16,9 @@ import cn.com.qmth.mps.enums.Role;
 import cn.com.qmth.mps.service.AuthService;
 import cn.com.qmth.mps.service.UserService;
 import cn.com.qmth.mps.util.ActiveDataUtil;
+import cn.com.qmth.mps.util.ByteUtil;
 import cn.com.qmth.mps.util.HttpUtil;
+import cn.com.qmth.mps.util.SHA256;
 import cn.com.qmth.mps.vo.AdminLoginVo;
 import net.sf.json.JSONObject;
 
@@ -45,21 +47,21 @@ public class AuthServiceImpl implements AuthService {
 		if(jo.containsKey("errmsg")) {
 			throw new StatusException("登录失败,"+jo.getString("errmsg"));
 		}
-		UserEntity userE=userService.getByLoginName(phone);
-		if(userE==null) {
+		UserEntity userEntity=userService.getByLoginName(phone);
+		if(userEntity==null) {
 			throw new StatusException("该手机号不存在");
 		}
-		if(!userE.getEnable()) {
+		if(!userEntity.getEnable()) {
 			throw new StatusException("该用户已禁用");
 		}
-		if(!userE.getRoleId().equals(Role.SECTION_LEADER.getId())) {
+		if(!userEntity.getRoleId().equals(Role.SECTION_LEADER.getId())) {
 			throw new StatusException("该用户不是科组长");
 		}
 		User user = new User();
-		user.setName(userE.getName());
-		user.setSchoolId(userE.getSchoolId());
-		user.setId(userE.getId());
-		user.setRole(Role.getById(userE.getRoleId()));
+		user.setName(userEntity.getName());
+		user.setSchoolId(userEntity.getSchoolId());
+		user.setId(userEntity.getId());
+		user.setRole(Role.getById(userEntity.getRoleId()));
 		user.setAccessToken(FastUUID.get());
 		user.buildKey();
 		ActiveDataUtil.userLogin(user);
@@ -71,6 +73,45 @@ public class AuthServiceImpl implements AuthService {
 		vo.setRole(user.getRole());
 		return vo;
 	}
+	
+	@Override
+	public AdminLoginVo loginAdmin(String loginName, String password) {
+		UserEntity userEntity=userService.getByLoginName(loginName);
+		if(userEntity==null) {
+			throw new StatusException("账号不存在");
+		}
+		if(!userEntity.getEnable()) {
+			throw new StatusException("该用户已禁用");
+		}
+		if(userEntity.getRoleId().equals(Role.SECTION_LEADER.getId())) {
+			throw new StatusException("科组长无权限登录");
+		}
+		byte[] bytes = SHA256.encode(password);
+		String encodePassword = ByteUtil.toHexAscii(bytes);
+		if (!encodePassword.equals(userEntity.getPassword())) {
+			throw new StatusException("密码错误");
+		}
+		User user = new User();
+		user.setName(userEntity.getName());
+		user.setSchoolId(userEntity.getSchoolId());
+		user.setId(userEntity.getId());
+		user.setRole(Role.getById(userEntity.getRoleId()));
+		user.setAccessToken(FastUUID.get());
+		user.buildKey();
+		ActiveDataUtil.userLogin(user);
+		AdminLoginVo vo=new AdminLoginVo();
+		vo.setAccessToken(user.getAccessToken());
+		vo.setName(user.getName());
+		vo.setSessionId(user.getSessionId());
+		vo.setSchoolId(user.getSchoolId());
+		vo.setRole(user.getRole());
+		return vo;
+	}
+
+	@Override
+	public void logout(User user) {
+		ActiveDataUtil.userLogout(user.getId());
+	}
 
 
 }

+ 116 - 0
src/main/java/cn/com/qmth/mps/util/ByteUtil.java

@@ -0,0 +1,116 @@
+package cn.com.qmth.mps.util;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+/**
+ * 字节转换工具
+ *
+ * @author 
+ * @date 2018年4月27日
+ */
+public class ByteUtil {
+	public final static short UNSIGNED_MAX_VALUE = (Byte.MAX_VALUE * 2) + 1;
+
+	private ByteUtil() {
+	}
+
+	public static int unsignedPromote(byte b) {
+		return b & 0xff;
+	}
+
+	public static String toHexAscii(byte b) {
+		StringWriter sw = new StringWriter(2);
+		addHexAscii(b, sw);
+		return sw.toString();
+	}
+
+	public static String toLowercaseHexAscii(byte b) {
+		StringWriter sw = new StringWriter(2);
+		addLowercaseHexAscii(b, sw);
+		return sw.toString();
+	}
+
+	public static String toHexAscii(byte[] bytes) {
+		int len = bytes.length;
+		StringWriter sw = new StringWriter(len * 2);
+		for (int i = 0; i < len; ++i)
+			addHexAscii(bytes[i], sw);
+		return sw.toString();
+	}
+
+	public static String toLowercaseHexAscii(byte[] bytes) {
+		int len = bytes.length;
+		StringWriter sw = new StringWriter(len * 2);
+		for (int i = 0; i < len; ++i)
+			addLowercaseHexAscii(bytes[i], sw);
+		return sw.toString();
+	}
+
+	public static byte[] fromHexAscii(String s) throws NumberFormatException {
+		try {
+			int len = s.length();
+			if ((len % 2) != 0)
+				throw new NumberFormatException("Hex ascii must be exactly two digits per byte.");
+
+			int out_len = len / 2;
+			byte[] out = new byte[out_len];
+			int i = 0;
+			StringReader sr = new StringReader(s);
+			while (i < out_len) {
+				int val = (16 * fromHexDigit(sr.read())) + fromHexDigit(sr.read());
+				out[i++] = (byte) val;
+			}
+			return out;
+		} catch (IOException e) {
+			throw new InternalError("IOException reading from StringReader?!?!");
+		}
+	}
+
+	static void addHexAscii(byte b, StringWriter sw) {
+		int ub = unsignedPromote(b);
+		int h1 = ub / 16;
+		int h2 = ub % 16;
+		sw.write(toHexDigit(h1));
+		sw.write(toHexDigit(h2));
+	}
+
+	static void addLowercaseHexAscii(byte b, StringWriter sw) {
+		int ub = unsignedPromote(b);
+		int h1 = ub / 16;
+		int h2 = ub % 16;
+		sw.write(toLowercaseHexDigit(h1));
+		sw.write(toLowercaseHexDigit(h2));
+	}
+
+	private static int fromHexDigit(int c) throws NumberFormatException {
+		if (c >= 0x30 && c < 0x3A)
+			return c - 0x30;
+		else if (c >= 0x41 && c < 0x47)
+			return c - 0x37;
+		else if (c >= 0x61 && c < 0x67)
+			return c - 0x57;
+		else
+			throw new NumberFormatException('\'' + c + "' is not a valid hexadecimal digit.");
+	}
+
+	private static char toHexDigit(int h) {
+		char out;
+		if (h <= 9)
+			out = (char) (h + 0x30);
+		else
+			out = (char) (h + 0x37);
+		return out;
+	}
+
+	private static char toLowercaseHexDigit(int h) {
+		char out;
+		if (h <= 9)
+			out = (char) (h + 0x30);
+		else
+			out = (char) (h + 0x57);
+		return out;
+	}
+
+}

+ 43 - 0
src/main/java/cn/com/qmth/mps/util/SHA256.java

@@ -0,0 +1,43 @@
+package cn.com.qmth.mps.util;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * SHA256加密
+ * 
+ * @author 
+ *
+ */
+public class SHA256 {
+
+	/**
+	 * main
+	 *
+	 * @author 
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		String s = "";
+		System.out.println(ByteUtil.toHexAscii(SHA256.encode(s)));
+	}
+
+	/**
+	 * 加密
+	 *
+	 * @author 
+	 * @param str
+	 * @return
+	 */
+	public static byte[] encode(String str) {
+		MessageDigest messageDigest;
+		try {
+			messageDigest = MessageDigest.getInstance("SHA-256");
+		} catch (NoSuchAlgorithmException e) {
+			throw new RuntimeException(e);
+		}
+		messageDigest.update(str.getBytes());
+		return messageDigest.digest();
+	}
+
+}

+ 70 - 0
src/main/java/cn/com/qmth/mps/vo/school/SchoolVo.java

@@ -0,0 +1,70 @@
+package cn.com.qmth.mps.vo.school;
+
+import cn.com.qmth.mps.entity.base.AuditingEntity;
+import io.swagger.annotations.ApiModelProperty;
+public class SchoolVo extends AuditingEntity {
+
+	private static final long serialVersionUID = -592353272256492483L;
+	@ApiModelProperty("二维码")
+	private String qrCode;
+	@ApiModelProperty("学校名称")
+	private String name;
+	@ApiModelProperty("启用禁用")
+	private Boolean enable;
+	@ApiModelProperty("联系方式")
+	private String telephone;
+	@ApiModelProperty("负责人")
+	private String contacts;
+	@ApiModelProperty("区域")
+	private String region;
+	
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Boolean getEnable() {
+		return enable;
+	}
+
+	public void setEnable(Boolean enable) {
+		this.enable = enable;
+	}
+
+	public String getTelephone() {
+		return telephone;
+	}
+
+	public void setTelephone(String telephone) {
+		this.telephone = telephone;
+	}
+
+	public String getContacts() {
+		return contacts;
+	}
+
+	public void setContacts(String contacts) {
+		this.contacts = contacts;
+	}
+
+	public String getRegion() {
+		return region;
+	}
+
+	public void setRegion(String region) {
+		this.region = region;
+	}
+
+	public String getQrCode() {
+		return qrCode;
+	}
+
+	public void setQrCode(String qrCode) {
+		this.qrCode = qrCode;
+	}
+
+}

+ 4 - 1
src/main/resources/application-test.properties

@@ -30,4 +30,7 @@ com.qmth.fss.server=http://localhost:7101/file
 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
 spring.jackson.time-zone=GMT+8
 
-session-timeout=7200
+session-timeout=7200
+
+wxapp-appid=xxx
+wxapp-secret=xxxx