Search in sources :

Example 1 with PmphUserWechat

use of com.bc.pmpheep.back.po.PmphUserWechat in project pmph by BCSquad.

the class PmphLoginController method login.

/**
 * <pre>
 * 功能描述:登陆
 * 使用示范:
 *
 * @param user
 * @param model
 * @return
 * </pre>
 * <p>
 * //* @throws SingleSignOnException
 */
@ResponseBody
// @OAuthRequired
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ResponseBean login(@RequestParam(value = "username", required = false) String username, @RequestParam(value = "password", required = false) String password, @RequestParam(value = "wechatUserId", required = false) String wechatUserId, @RequestParam(value = "token", required = false) String token, HttpServletRequest request) throws CheckedServiceException {
    Map<String, Object> resultMap = new HashMap<String, Object>();
    logger.info("username => " + username);
    logger.info("password => " + password);
    // HttpSingleSignOnService service = new HttpSingleSignOnService();
    // String url = service.getSingleSignOnURL();
    // try {
    // 判断是否从企业微信App登陆
    String userAgent = request.getHeader("user-agent").toLowerCase();
    Boolean isTrue = userAgent == null || userAgent.indexOf("micromessenger") == -1 ? false : true;
    logger.info("user-agent  " + userAgent);
    if (isTrue) {
        if (StringUtil.notEmpty(token)) {
            String newToken = username + password + wechatUserId + "<pmpheep>";
            if (!newToken.equals(new DesRun(token).depsw)) {
                throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "用户名密码错误!");
            }
            if ("sso".equals(wechatUserId)) {
                username = new DesRun(username).depsw;
                password = new DesRun(password).depsw;
            }
        }
    }
    PmphUser pmphUser = null;
    if (isTrue && StringUtil.notEmpty(wechatUserId) && !"sso".equals(wechatUserId) && !"pmphuserlogin".equals(wechatUserId)) {
        // 由微信--我的企业号 登录过来
        // if (isTrue) {//用户名 如果不为空,手动输入
        // 如果是微信登录过来 且wechatUserId 与 username 同时不为空,此时 维护 pmph_user_wechat 表
        PmphUserWechat pmphUserWechat = new PmphUserWechat();
        pmphUserWechat.setUsername(username);
        pmphUserWechat.setWechatId(wechatUserId);
        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "请输入用户名和密码!");
        }
        pmphUser = pmphUserService.login(username, new DesRun("", password).enpsw);
        pmphUserWechat.setUserid(pmphUser.getId());
        // 微信 我的企业号 绑定userid
        pmphUserWechatService.add(pmphUserWechat);
        pmphUserService.updateUserOpenid(wechatUserId, username, pmphUser.getId());
    // pmphUser = pmphUserService.login(username, null);
    /* } else {//已经绑定
                pmphUser = pmphUserService.login(wechatUserId);
                username = "";
                password = "";
            }
*/
    } else if (StringUtil.notEmpty(wechatUserId) && "pmphuserlogin".equals(wechatUserId)) {
        // 社内用户单点登录
        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "请输入用户名和密码!");
        }
        pmphUser = pmphUserService.login(username, password);
    } else if (StringUtil.notEmpty(wechatUserId) && "sso".equals(wechatUserId)) {
        // sso 登录
        if (StringUtil.isEmpty(username)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "单点登录失败");
        }
        pmphUser = pmphUserService.login(username, null);
    } else {
        // 正常输入用户名和密码登陆
        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "请输入用户名和密码!");
        }
        pmphUser = pmphUserService.login(username, new DesRun("", password).enpsw);
    }
    // PmphUser pmphUser = pmphUserService.login(userName, null);
    pmphUser.setLoginType(Const.LOGIN_TYPE_PMPH);
    if (!RouteUtil.DEFAULT_USER_AVATAR.equals(pmphUser.getAvatar())) {
        pmphUser.setAvatar(RouteUtil.userAvatar(pmphUser.getAvatar()));
    }
    // 根据用户Id查询对应角色(是否为管理员)
    List<PmphRole> pmphRoles = pmphRoleService.getPmphRoleByUserId(pmphUser.getId());
    if (pmphRoles.isEmpty()) {
        // 添加默认权限
        pmphRoleService.addUserRole(pmphUser.getId(), 2L);
        pmphRoles = pmphRoleService.getPmphRoleByUserId(pmphUser.getId());
    }
    List<Long> roleIds = new ArrayList<Long>();
    for (PmphRole pmphRole : pmphRoles) {
        roleIds.add(pmphRole.getId());
        if (ObjectUtil.notNull(pmphRole)) {
            if (Const.LOGIN_USER_IS_ADMIN.equals(pmphRole.getRoleName()) || Const.LOGIN_USER_IS_ADMINS.equals(pmphRole.getRoleName()) || Const.LOGIN_SYS_USER_IS_ADMIN.equals(pmphRole.getRoleName())) {
                pmphUser.setIsAdmin(true);
            } else {
                pmphUser.setIsAdmin(false);
            }
        }
        if (Const.TRUE == pmphUser.getIsAdmin()) {
            break;
        }
    }
    // 根据用户Id查询对应权限Id
    List<Long> pmphUserPermissionIds = pmphUserService.getPmphUserPermissionByUserId(pmphUser.getId());
    // 判断是否从企业微信App登陆
    /* if (isTrue && StringUtil.notEmpty(username) && StringUtil.notEmpty(wechatUserId)) {
            pmphUserService.updateUserOpenid(wechatUserId, username);
        }*/
    // String materialPermission =
    // pmphUserService.getMaterialPermissionByUserId(pmphUser.getId()); 根据用户返回书籍
    // 验证成功在Session中保存用户信息
    request.getSession().setAttribute(Const.SESSION_PMPH_USER, pmphUser);
    // 验证成功在Session中保存用户Token信息
    request.getSession().setAttribute(Const.SEESION_PMPH_USER_TOKEN, new DesRun(password, username).enpsw);
    // pmphUserSessionId
    resultMap.put(Const.USER_SEESION_ID, request.getSession().getId());
    resultMap.put(Const.SESSION_PMPH_USER, pmphUser);
    resultMap.put(Const.SEESION_PMPH_USER_TOKEN, new DesRun(password, username).enpsw);
    resultMap.put("pmphUserPermissionIds", pmphUserPermissionIds);
    // resultMap.put("materialPermission", materialPermission);
    return new ResponseBean(resultMap);
// } catch (CheckedServiceException cException) {
// return new ResponseBean(cException);
// }
}
Also used : HashMap(java.util.HashMap) PmphUser(com.bc.pmpheep.back.po.PmphUser) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) PmphUserWechat(com.bc.pmpheep.back.po.PmphUserWechat) PmphRole(com.bc.pmpheep.back.po.PmphRole) DesRun(com.bc.pmpheep.back.util.DesRun) ResponseBean(com.bc.pmpheep.controller.bean.ResponseBean) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with PmphUserWechat

use of com.bc.pmpheep.back.po.PmphUserWechat in project pmph by BCSquad.

the class WeChatLoginController method load.

/**
 * <pre>
 * 功能描述:加载个人信息,此处添加了@OAuthRequired注解
 * 使用示范:
 *
 * @param request
 * @param model
 * @return
 * </pre>
 */
@RequestMapping(value = { "/login" })
public // @OAuthRequired
ModelAndView load(HttpServletRequest request, HttpServletResponse response) throws /*, Model model*/
Exception {
    // 判断是否从企业微信App登陆
    String userAgent = request.getHeader("user-agent").toLowerCase();
    Boolean isTrue = userAgent == null || userAgent.indexOf("micromessenger") == -1 ? false : true;
    PmphUser pmphUser = null;
    String username = null;
    String password = null;
    ModelAndView model = new ModelAndView();
    logger.info("login " + isTrue);
    if (isTrue) {
        HttpSession session = request.getSession();
        // userId 在session 中可以取到 微信--企业微信号 这个是pmph_user_wechat 表中的wechat_id
        String wechatUserId = (String) session.getAttribute("UserId");
        if (StringUtil.isEmpty(wechatUserId)) {
            // app 访问登录
            wechatUserId = request.getParameter("UserId");
        }
        String appType = request.getParameter("appType");
        // 微信--微信企业号直接访问app登录
        if (StringUtil.isEmpty(appType)) {
            /*微信 -- 待办 登录 首页*/
            if (StringUtil.isEmpty(wechatUserId)) {
                throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "网络异常,请重新再试!");
            }
            model.addObject("UserId", wechatUserId);
            PmphUserWechat pmphUserWechat = pmphUserWechatService.getPmphUserWechatByWechatId(wechatUserId);
            if (ObjectUtil.isNull(pmphUserWechat)) {
                logger.info("wechatLoginController   " + ObjectUtil.isNull(pmphUserWechat));
                // 查找不到对应的社内用户 跳转登录页面
                model.addObject("isLogin", "0");
                // model.addObject("sessionPmphUser", "1");
                // model.addObject("pmphUserPermissionIds", "1");
                // commission 从微信 -- 企业微信号 代办
                model.addObject("isIndexOrCommission", ((!StringUtil.isEmpty((String) session.getAttribute("UserId")) && !StringUtil.isEmpty(request.getParameter("commission"))) ? "commission" : ""));
                // if ((!StringUtil.isEmpty((String) session.getAttribute("UserId")) && !StringUtil.isEmpty(request.getParameter("commission")))) {
                /*logger.info("http://medu.ipmph.com/wx/#/login?wechatUserId=" + wechatUserId+"&isIndexOrCommission="+((!StringUtil.isEmpty((String)session.getAttribute("UserId"))&&!StringUtil.isEmpty(request.getParameter("commission")))?"commission":""));
                    response.sendRedirect("http://medu.ipmph.com/wx/#/login?wechatUserId=" + wechatUserId+"&isIndexOrCommission=");*/
                // }
                model.setViewName("wechat");
                return model;
            } else {
                // 查找到对应的社内用户,跳转到首页
                pmphUser = pmphUserService.getPmphUserByUsername(pmphUserWechat.getUsername(), pmphUserWechat.getUserid());
                if (ObjectUtil.notNull(pmphUser)) {
                    username = new DesRun(null, pmphUserWechat.getUsername()).enpsw;
                    password = pmphUser.getPassword();
                    model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                    model.addObject("username", username);
                    model.addObject("password", password);
                    if (StringUtil.isEmpty(request.getParameter("commission"))) {
                        // 跳转到首页
                        model.addObject("isLogin", "1");
                    } else {
                        // 跳转到代办页面
                        model.addObject("isLogin", "5");
                    }
                }
            }
        } else {
            /*微信 -- 申报等页面*/
            if (StringUtil.isEmpty(wechatUserId)) {
                throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "网络异常,请重新再试!");
            }
            model.addObject("UserId", wechatUserId);
            PmphUserWechat pmphUserWechat = pmphUserWechatService.getPmphUserWechatByWechatId(wechatUserId);
            if (ObjectUtil.isNull(pmphUserWechat)) {
                pmphUser = pmphUserService.getPmphUserByOpenid(wechatUserId);
                if (ObjectUtil.notNull(pmphUser)) {
                    username = new DesRun(null, pmphUser.getUsername()).enpsw;
                    password = pmphUser.getPassword();
                    model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                    model.addObject("username", username);
                    model.addObject("password", password);
                    model.addObject("isLogin", "3");
                }
            } else {
                pmphUser = pmphUserService.getPmphUserByUsername(pmphUserWechat.getUsername(), pmphUserWechat.getUserid());
                if (ObjectUtil.notNull(pmphUser)) {
                    username = new DesRun(null, pmphUserWechat.getUsername()).enpsw;
                    password = pmphUser.getPassword();
                    model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                    model.addObject("username", username);
                    model.addObject("password", password);
                    model.addObject("isLogin", "4");
                }
            }
            /*跳转到某个具体的页面*/
            if ("1".equals(appType)) {
                // 教材审核
                String materialId = request.getParameter("materialId");
                String declarationId = request.getParameter("declarationId");
                model.addObject("materialId", materialId);
                model.addObject("declarationId", declarationId);
            } else if ("2".equals(appType)) {
            // 选题申报
            } else if ("3".equals(appType)) {
                // 图书纠错
                String bookName = request.getParameter("bookName");
                String type = request.getParameter("type");
                String id = request.getParameter("id");
                model.addObject("bookName", bookName);
                model.addObject("type", type);
                model.addObject("id", id);
            }
            model.addObject("appType", appType);
        }
    } else {
        // SSO 登陆
        model.addObject("isLogin", "2");
        HttpSingleSignOnService service = new HttpSingleSignOnService();
        try {
            LocalPrincipal principal = service.singleSignOn(request);
            String localPass = principal.getLocalPass();
            String userName = principal.getName();
            // String userName = "liub";
            assert userName != null;
            pmphUser = pmphUserService.getPmphUserByUsername(userName, null);
            // Map map = ssoHelper.getUserInfo(userName,"123456");
            // 预留
            Map map = new HashMap();
            if (ObjectUtil.isNull(pmphUser)) {
                // 为空就新建一个用户
                String pass = "888888";
                if (localPass != null) {
                    pass = localPass;
                }
                pmphUser = pmphUserService.add(new PmphUser(userName, pass, false, MapUtils.getString(map, "RealName", ""), 0L, MapUtils.getString(map, "Mobile", ""), MapUtils.getString(map, "Emial", ""), "DEFAULT", "", 999, false));
                // 添加默认权限
                pmphRoleService.addUserRole(pmphUser.getId(), 2L);
            }
            username = new DesRun(null, pmphUser.getUsername()).enpsw;
            password = pmphUser.getPassword();
            String wechatUserId = "sso";
            model.addObject("username", username);
            model.addObject("password", password);
            model.addObject("UserId", wechatUserId);
            model.addObject(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
        } catch (Exception e) {
            logger.error("SSO登陆失败,异常信息'{}'", e.getMessage());
        }
    }
    /*--------------------------- 登录权限控制 session cookie ------------为了直接跳转到某个vue页面---------------------*/
    pmphUser.setLoginType(Const.LOGIN_TYPE_PMPH);
    if (!RouteUtil.DEFAULT_USER_AVATAR.equals(pmphUser.getAvatar())) {
        pmphUser.setAvatar(RouteUtil.userAvatar(pmphUser.getAvatar()));
    }
    // 根据用户Id查询对应角色(是否为管理员)
    List<PmphRole> pmphRoles = pmphRoleService.getPmphRoleByUserId(pmphUser.getId());
    if (pmphRoles.isEmpty()) {
        // 添加默认权限
        pmphRoleService.addUserRole(pmphUser.getId(), 2L);
        pmphRoles = pmphRoleService.getPmphRoleByUserId(pmphUser.getId());
    }
    List<Long> roleIds = new ArrayList<Long>();
    for (PmphRole pmphRole : pmphRoles) {
        roleIds.add(pmphRole.getId());
        if (ObjectUtil.notNull(pmphRole)) {
            if (Const.LOGIN_USER_IS_ADMIN.equals(pmphRole.getRoleName()) || Const.LOGIN_USER_IS_ADMINS.equals(pmphRole.getRoleName()) || Const.LOGIN_SYS_USER_IS_ADMIN.equals(pmphRole.getRoleName())) {
                pmphUser.setIsAdmin(true);
            } else {
                pmphUser.setIsAdmin(false);
            }
        }
        if (Const.TRUE == pmphUser.getIsAdmin()) {
            break;
        }
    }
    // 根据用户Id查询对应权限Id
    List<Long> pmphUserPermissionIds = pmphUserService.getPmphUserPermissionByUserId(pmphUser.getId());
    request.getSession().setAttribute(Const.SESSION_PMPH_USER, pmphUser);
    // 验证成功在Session中保存用户Token信息
    request.getSession().setAttribute(Const.SEESION_PMPH_USER_TOKEN, new DesRun(password, username).enpsw);
    model.addObject(Const.USER_SEESION_ID, request.getSession().getId());
    model.addObject(Const.SESSION_PMPH_USER, JSON.toJSON(pmphUser));
    model.addObject(Const.SEESION_PMPH_USER_TOKEN, new DesRun(password, username).enpsw);
    model.addObject("pmphUserPermissionIds", pmphUserPermissionIds);
    /*---------------------------------------------------------------*/
    model.setViewName("wechat");
    return model;
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpSingleSignOnService(small.danfer.sso.http.HttpSingleSignOnService) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) PmphUserWechat(com.bc.pmpheep.back.po.PmphUserWechat) LocalPrincipal(small.danfer.sso.assertion.LocalPrincipal) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) IOException(java.io.IOException) PmphRole(com.bc.pmpheep.back.po.PmphRole) HashMap(java.util.HashMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with PmphUserWechat

use of com.bc.pmpheep.back.po.PmphUserWechat in project pmph by BCSquad.

the class WeChatLoginController method load.

/**
 * <pre>
 * 功能描述:加载个人信息,此处添加了@OAuthRequired注解
 * 使用示范:
 *
 * @param request
 * @param model
 * @return
 * </pre>
 */
@RequestMapping(value = { "/wechatUserInfo" })
@OAuthRequired
public Object load(HttpServletRequest request, Model model) {
    // System.out.println("Load a User!");
    HttpSession session = request.getSession();
    // 判断是否从企业微信App登陆
    model.addAttribute("Userid", session.getAttribute("UserId"));
    String userAgent = request.getHeader("user-agent").toLowerCase();
    Boolean isTrue = userAgent == null || userAgent.indexOf("micromessenger") == -1 ? false : true;
    if (isTrue) {
        String wechatUserId = (String) session.getAttribute("UserId");
        if (StringUtil.isEmpty(wechatUserId)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.USER_MANAGEMENT, CheckedExceptionResult.NULL_PARAM, "网络异常,请重新再试!");
        }
        PmphUserWechat pmphUserWechat = pmphUserWechatService.getPmphUserWechatByWechatId(wechatUserId);
        if (ObjectUtil.isNull(pmphUserWechat)) {
            model.addAttribute("isLogin", "0");
        } else {
            PmphUser pu = pmphUserService.getPmphUserByUsername(pmphUserWechat.getUsername());
            if (ObjectUtil.notNull(pu)) {
                String username = new DesRun(null, pmphUserWechat.getUsername()).enpsw;
                String password = pu.getPassword();
                model.addAttribute(Const.PMPH_WECHAT_USER_TOKEN, new DesRun(password, username + password + wechatUserId + "<pmpheep>").enpsw);
                model.addAttribute("username", username);
                model.addAttribute("password", password);
                model.addAttribute("isLogin", "1");
            }
        }
    }
    return "wechat";
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) HttpSession(javax.servlet.http.HttpSession) DesRun(com.bc.pmpheep.back.util.DesRun) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) PmphUserWechat(com.bc.pmpheep.back.po.PmphUserWechat) OAuthRequired(com.bc.pmpheep.wechat.interceptor.OAuthRequired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PmphUser (com.bc.pmpheep.back.po.PmphUser)3 PmphUserWechat (com.bc.pmpheep.back.po.PmphUserWechat)3 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 PmphRole (com.bc.pmpheep.back.po.PmphRole)2 DesRun (com.bc.pmpheep.back.util.DesRun)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HttpSession (javax.servlet.http.HttpSession)2 ResponseBean (com.bc.pmpheep.controller.bean.ResponseBean)1 OAuthRequired (com.bc.pmpheep.wechat.interceptor.OAuthRequired)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 LocalPrincipal (small.danfer.sso.assertion.LocalPrincipal)1 HttpSingleSignOnService (small.danfer.sso.http.HttpSingleSignOnService)1