|
@@ -10,6 +10,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import com.qmth.boot.core.exception.StatusException;
|
|
import com.qmth.boot.core.exception.StatusException;
|
|
import com.qmth.boot.core.security.annotation.AuthorizationComponent;
|
|
import com.qmth.boot.core.security.annotation.AuthorizationComponent;
|
|
@@ -21,11 +22,14 @@ import cn.com.qmth.mps.bean.User;
|
|
import cn.com.qmth.mps.config.SysProperty;
|
|
import cn.com.qmth.mps.config.SysProperty;
|
|
import cn.com.qmth.mps.entity.UserEntity;
|
|
import cn.com.qmth.mps.entity.UserEntity;
|
|
import cn.com.qmth.mps.entity.WxappAccessTokenEntity;
|
|
import cn.com.qmth.mps.entity.WxappAccessTokenEntity;
|
|
|
|
+import cn.com.qmth.mps.entity.WxappInfoEntity;
|
|
import cn.com.qmth.mps.enums.Role;
|
|
import cn.com.qmth.mps.enums.Role;
|
|
|
|
+import cn.com.qmth.mps.exception.ParameterExceptions;
|
|
import cn.com.qmth.mps.service.AuthService;
|
|
import cn.com.qmth.mps.service.AuthService;
|
|
import cn.com.qmth.mps.service.SessionService;
|
|
import cn.com.qmth.mps.service.SessionService;
|
|
import cn.com.qmth.mps.service.UserService;
|
|
import cn.com.qmth.mps.service.UserService;
|
|
import cn.com.qmth.mps.service.WxappAccessTokenService;
|
|
import cn.com.qmth.mps.service.WxappAccessTokenService;
|
|
|
|
+import cn.com.qmth.mps.service.WxappInfoService;
|
|
import cn.com.qmth.mps.util.ByteUtil;
|
|
import cn.com.qmth.mps.util.ByteUtil;
|
|
import cn.com.qmth.mps.util.HttpUtil;
|
|
import cn.com.qmth.mps.util.HttpUtil;
|
|
import cn.com.qmth.mps.util.SHA256;
|
|
import cn.com.qmth.mps.util.SHA256;
|
|
@@ -44,24 +48,18 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
|
|
private WxappAccessTokenService wxappAccessTokenService;
|
|
private WxappAccessTokenService wxappAccessTokenService;
|
|
@Autowired
|
|
@Autowired
|
|
private SysProperty sysProperty;
|
|
private SysProperty sysProperty;
|
|
|
|
+ @Autowired
|
|
|
|
+ private WxappInfoService wxappInfoService;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public AdminLoginVo loginWxAppCode(String wxappCode) {
|
|
|
|
-
|
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
|
- params.put("code", wxappCode);
|
|
|
|
- String ret;
|
|
|
|
- try {
|
|
|
|
- ret = HttpUtil.httpActionPost("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="
|
|
|
|
- + wxappAccessTokenService.getWxappAccessToken().getAccessToken(), null, params);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new StatusException("登录失败", e);
|
|
|
|
|
|
+ public AdminLoginVo loginWxAppCode(String loginCode) {
|
|
|
|
+ JSONObject auth=getAuthorization(loginCode);
|
|
|
|
+ String openid=auth.getString("openid");
|
|
|
|
+ WxappInfoEntity wi=wxappInfoService.getByOpenId(openid);
|
|
|
|
+ if(wi==null) {
|
|
|
|
+ throw ParameterExceptions.OPENID_NOT_FOUND;
|
|
}
|
|
}
|
|
- JSONObject jo = JSONObject.fromObject(ret);
|
|
|
|
- if (jo.getInt("errcode") != 0) {
|
|
|
|
- throw new StatusException("登录失败," + jo.getString("errmsg"));
|
|
|
|
- }
|
|
|
|
- UserEntity userEntity = userService.getByLoginName(jo.getJSONObject("phone_info").getString("purePhoneNumber"));
|
|
|
|
|
|
+ UserEntity userEntity = userService.getByLoginName(wi.getPhone());
|
|
if (userEntity == null) {
|
|
if (userEntity == null) {
|
|
throw new StatusException("微信所绑定手机号系统中不存在");
|
|
throw new StatusException("微信所绑定手机号系统中不存在");
|
|
}
|
|
}
|
|
@@ -166,9 +164,12 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Transactional
|
|
@Override
|
|
@Override
|
|
- public AdminLoginVo loginWxAppEncryptedData(String wxappCode, String encryptedData, String iv) {
|
|
|
|
- JSONObject jo=decrypt(encryptedData, iv, getSessionKey(wxappCode));
|
|
|
|
|
|
+ public AdminLoginVo loginWxAppByEncryptedData(String loginCode, String encryptedData, String iv) {
|
|
|
|
+ JSONObject auth=getAuthorization(loginCode);
|
|
|
|
+ String openid=auth.getString("openid");
|
|
|
|
+ JSONObject jo=decrypt(encryptedData, iv, auth.getString("session_key"));
|
|
String phone=jo.getString("purePhoneNumber");
|
|
String phone=jo.getString("purePhoneNumber");
|
|
UserEntity userEntity = userService.getByLoginName(phone);
|
|
UserEntity userEntity = userService.getByLoginName(phone);
|
|
if (userEntity == null) {
|
|
if (userEntity == null) {
|
|
@@ -180,6 +181,13 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
|
|
if (!userEntity.getRoleId().equals(Role.SECTION_LEADER.getId())) {
|
|
if (!userEntity.getRoleId().equals(Role.SECTION_LEADER.getId())) {
|
|
throw new StatusException("该用户不是科组长");
|
|
throw new StatusException("该用户不是科组长");
|
|
}
|
|
}
|
|
|
|
+ WxappInfoEntity wi=wxappInfoService.getByOpenId(openid);
|
|
|
|
+ if(wi==null) {
|
|
|
|
+ wi=new WxappInfoEntity();
|
|
|
|
+ wi.setOpenid(openid);
|
|
|
|
+ }
|
|
|
|
+ wi.setPhone(phone);
|
|
|
|
+ wxappInfoService.saveOrUpdate(wi);
|
|
User user = new User();
|
|
User user = new User();
|
|
user.setName(userEntity.getName());
|
|
user.setName(userEntity.getName());
|
|
user.setSchoolId(userEntity.getSchoolId());
|
|
user.setSchoolId(userEntity.getSchoolId());
|
|
@@ -197,11 +205,11 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
|
|
return vo;
|
|
return vo;
|
|
}
|
|
}
|
|
|
|
|
|
- private String getSessionKey(String wxappCode) {
|
|
|
|
|
|
+ private JSONObject getAuthorization(String loginCode) {
|
|
Map<String, String> params = new HashMap<>();
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("appid", sysProperty.getWxappAppid());
|
|
params.put("appid", sysProperty.getWxappAppid());
|
|
params.put("secret", sysProperty.getWxappSecret());
|
|
params.put("secret", sysProperty.getWxappSecret());
|
|
- params.put("js_code", wxappCode);
|
|
|
|
|
|
+ params.put("js_code", loginCode);
|
|
params.put("grant_type", "authorization_code");
|
|
params.put("grant_type", "authorization_code");
|
|
String ret;
|
|
String ret;
|
|
try {
|
|
try {
|
|
@@ -213,7 +221,7 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
|
|
if (jo.containsKey("errcode")) {
|
|
if (jo.containsKey("errcode")) {
|
|
throw new StatusException("登录失败," + jo.getString("errmsg"));
|
|
throw new StatusException("登录失败," + jo.getString("errmsg"));
|
|
}
|
|
}
|
|
- return jo.getString("session_key");
|
|
|
|
|
|
+ return jo;
|
|
}
|
|
}
|
|
|
|
|
|
private JSONObject decrypt(String encryptedData, String iv, String sessionKey){
|
|
private JSONObject decrypt(String encryptedData, String iv, String sessionKey){
|
|
@@ -246,4 +254,57 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
|
|
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
|
|
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
|
|
return jsonObject;
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public AdminLoginVo loginWxAppByPhoneCode(String loginCode,String phoneCode) {
|
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
|
+ params.put("code", phoneCode);
|
|
|
|
+ String ret;
|
|
|
|
+ try {
|
|
|
|
+ ret = HttpUtil.httpActionPost("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="
|
|
|
|
+ + wxappAccessTokenService.getWxappAccessToken().getAccessToken(), null, params);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("登录失败", e);
|
|
|
|
+ }
|
|
|
|
+ JSONObject jo = JSONObject.fromObject(ret);
|
|
|
|
+ if (jo.getInt("errcode") != 0) {
|
|
|
|
+ throw new StatusException("登录失败," + jo.getString("errmsg"));
|
|
|
|
+ }
|
|
|
|
+ String phone=jo.getJSONObject("phone_info").getString("purePhoneNumber");
|
|
|
|
+ UserEntity userEntity = userService.getByLoginName(phone);
|
|
|
|
+ if (userEntity == null) {
|
|
|
|
+ throw new StatusException("微信所绑定手机号系统中不存在");
|
|
|
|
+ }
|
|
|
|
+ if (!userEntity.getEnable()) {
|
|
|
|
+ throw new StatusException("该用户已禁用");
|
|
|
|
+ }
|
|
|
|
+ if (!userEntity.getRoleId().equals(Role.SECTION_LEADER.getId())) {
|
|
|
|
+ throw new StatusException("该用户不是科组长");
|
|
|
|
+ }
|
|
|
|
+ JSONObject auth=getAuthorization(loginCode);
|
|
|
|
+ String openid=auth.getString("openid");
|
|
|
|
+ WxappInfoEntity wi=wxappInfoService.getByOpenId(openid);
|
|
|
|
+ if(wi==null) {
|
|
|
|
+ wi=new WxappInfoEntity();
|
|
|
|
+ wi.setOpenid(openid);
|
|
|
|
+ }
|
|
|
|
+ wi.setPhone(phone);
|
|
|
|
+ wxappInfoService.saveOrUpdate(wi);
|
|
|
|
+ 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();
|
|
|
|
+ sessionService.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;
|
|
|
|
+ }
|
|
}
|
|
}
|