|
@@ -1,13 +1,23 @@
|
|
|
package org.jeecg.modules.system.controller;
|
|
|
|
|
|
-import cn.com.ctop.common.module.service.ISysDepartService;
|
|
|
-import cn.com.ctop.common.module.service.ISysDictService;
|
|
|
-import cn.com.ctop.common.module.service.ISysUserService;
|
|
|
+import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
|
|
|
+import cn.com.ctop.common.module.entity.WexinConfig;
|
|
|
+import cn.com.ctop.common.module.service.*;
|
|
|
+import cn.com.ctop.common.module.utils.Check;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import lombok.val;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
|
|
|
+import me.chanjar.weixin.cp.api.WxCpUserService;
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpUserServiceImpl;
|
|
|
+import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
|
|
|
+import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
|
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
import org.jeecg.common.constant.CacheConstant;
|
|
@@ -91,6 +101,74 @@ public class LoginController {
|
|
|
sysBaseApi.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICtopCorpWexinUserService ctopCorpWexinUserService;
|
|
|
+ @Autowired
|
|
|
+ private IWexinConfigService wexinConfigService;
|
|
|
+
|
|
|
+ @RequestMapping(value = "/wxlogin", method = RequestMethod.POST)
|
|
|
+ @ApiOperation("企业微信登录接口")
|
|
|
+ public Result<JSONObject> wxlogin(@RequestParam(name = "code") String code, @RequestParam(name = "corpId") String corpId) throws Exception {
|
|
|
+ Result<JSONObject> result = new Result<>();
|
|
|
+ WexinConfig wexinConfig = wexinConfigService.getInfoByCorpId(corpId);
|
|
|
+ if (Check.isNull(wexinConfig)) {
|
|
|
+ result.error500("未找到用户配置,请联系管理员");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ val configStorage = new WxCpDefaultConfigImpl();
|
|
|
+ configStorage.setCorpId(wexinConfig.getCorpId());
|
|
|
+ configStorage.setAgentId(wexinConfig.getAgentId());
|
|
|
+ configStorage.setCorpSecret(wexinConfig.getCorpSecret());
|
|
|
+ configStorage.setToken(wexinConfig.getToken());
|
|
|
+ configStorage.setAesKey(wexinConfig.getAesKey());
|
|
|
+ val service = new WxCpServiceImpl();
|
|
|
+ service.setWxCpConfigStorage(configStorage);
|
|
|
+ WxCpOAuth2Service wxCpOAuth2Service = service.getOauth2Service();
|
|
|
+ try {
|
|
|
+ WxCpOauth2UserInfo userInfo = wxCpOAuth2Service.getUserInfo(1000002, code);
|
|
|
+ if (userInfo != null && userInfo.getUserId() != null) {
|
|
|
+ WxCpUserService wxCpUserService = new WxCpUserServiceImpl(service);
|
|
|
+ WxCpUser user = wxCpUserService.getById(userInfo.getUserId());
|
|
|
+ String email = user.getEmail();
|
|
|
+ SysUser sysUser = sysUserService.getUserByEmail(email);
|
|
|
+ if (sysUser == null) {
|
|
|
+ result.error500("该用户未注册系统账户,请联系管理员");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (sysUser.getStatus() != 1) {
|
|
|
+ result.error500("该用户已冻结,请联系管理员");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ String departId = sysDepartService.selectIdByOrdCode(sysUser.getOrgCode());
|
|
|
+ sysUser.setDepartId(departId);
|
|
|
+ result = sysUserService.checkUserIsEffective(sysUser);
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ //用户登录信息
|
|
|
+ userInfo(sysUser, result);
|
|
|
+ sysBaseApi.addLog("用户名: " + email + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
+ CtopCorpWexinUser ctopCorpWexinUser = ctopCorpWexinUserService.getById(sysUser.getId());
|
|
|
+ if (ctopCorpWexinUser == null) {
|
|
|
+ ctopCorpWexinUser = new CtopCorpWexinUser();
|
|
|
+ ctopCorpWexinUser.setCorpId(corpId);
|
|
|
+ ctopCorpWexinUser.setUserId(sysUser.getId());
|
|
|
+ ctopCorpWexinUser.setWexinEmail(user.getEmail());
|
|
|
+ ctopCorpWexinUser.setWexinGender(user.getGender().getGenderName());
|
|
|
+ ctopCorpWexinUser.setWexinName(user.getName());
|
|
|
+ ctopCorpWexinUser.setWexinId(user.getUserId());
|
|
|
+ ctopCorpWexinUser.setWexinMobile(user.getMobile());
|
|
|
+ ctopCorpWexinUserService.save(ctopCorpWexinUser);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ result.error500("企业微信授权错误");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 退出登录
|