This commit is contained in:
bo.yang 2021-07-27 18:19:00 +08:00
parent c3c66b19a0
commit 3828b0e262
14 changed files with 218 additions and 160 deletions

View File

@ -1,136 +0,0 @@
package com.ruoyi.bps.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bps.domain.WechatUserInfo;
import com.ruoyi.bps.service.IWechatApiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
@RestController
public class WechatApiController extends BaseController {
@Autowired
IWechatApiService wechatApiService;
@Autowired
private ISysUserService userService;
@RequestMapping("anon/getAccessToken")
public String getAccessToken() {
return wechatApiService.GetAccessToken();
}
@PostMapping("/wxlogin")
@ResponseBody
public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe)
{
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
Subject subject = SecurityUtils.getSubject();
try
{
subject.login(token);
return success();
}
catch (AuthenticationException e)
{
String msg = "用户或密码错误";
if (StringUtils.isNotEmpty(e.getMessage()))
{
msg = e.getMessage();
}
return error(msg);
}
}
@GetMapping("anon/wechatLogin")
public String codeTest(HttpServletRequest request)
{
String code= request.getParameter("code");
String state = request.getParameter("state");
//获取访问用户身份ID
String url="https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
String param = "access_token="+wechatApiService.GetAccessToken()+"&code="+code;
// String userInfo = HttpUtils.sendGet(url,param); //测试已能正常返回UserInfo Json,正式使用时打开
String userInfo = "{\"UserId\":\"359\",\"DeviceId\":\"10000589102865WJ\",\"errcode\":0,\"errmsg\":\"ok\"}"; //为避免去微信获取code麻烦开发调试时打开
JSONObject jsonObjectUserInfo = JSONObject.parseObject(userInfo);
//如果返回码不为0则返回错误信息
if ( Integer.parseInt(jsonObjectUserInfo.getString("errcode")) != 0){
return jsonObjectUserInfo.getString("errmsg");
}
String userId = jsonObjectUserInfo.getString("UserId");
//获取用户邮箱
url="https://qyapi.weixin.qq.com/cgi-bin/user/get";
param="access_token="+wechatApiService.GetAccessToken()+"&userid="+userId;
String userInfoDetail=HttpUtils.sendGet(url,param); //获取成员信息
JSONObject jsonObjectUserInfoDetail=JSONObject.parseObject(userInfoDetail);
//如果返回码不为0则返回错误信息
if(Integer.parseInt(jsonObjectUserInfoDetail.getString("errcode")) != 0)
{
return jsonObjectUserInfo.getString("errmsg");
}
String userEmail= jsonObjectUserInfoDetail.getString("email");
String userName= jsonObjectUserInfoDetail.getString("name");
//根据邮箱名+用户名匹配本地用户对应的邮箱名与用户名
SysUser sysUser=new SysUser();
sysUser.setUserName(userName);
sysUser.setEmail(userEmail);
sysUser.setStatus("02"); //只获取从OA同步的用户保持与企业微信一致
List<SysUser> userList= userService.selectUserList(sysUser);
int count= userList.size();
if(count <= 0){
return "false"; //系统里没有用户没有从OA同步 处理逻辑待定
}
if(count > 1){
return "false"; //本地数据库存在多个姓名与邮箱相同的记录如何处理
}
return userEmail;
}
@GetMapping("anon/userInfo")
public Map<String, Object> getJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
Map<String, Object> params = JSON.parseObject(responseStrBuilder.toString(), Map.class);
return params;
}
}

View File

@ -1,6 +0,0 @@
package com.ruoyi.bps.service;
public interface IWechatApiService {
//获取Access Token
public String GetAccessToken();
}

View File

@ -0,0 +1,91 @@
package com.ruoyi.web.controller.system;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IWechatApiService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
@RestController
public class WechatApiController extends BaseController {
@Autowired
IWechatApiService wechatApiService;
@RequestMapping("anon/getAccessToken")
public String getAccessToken() {
return wechatApiService.GetAccessToken();
}
@GetMapping("anon/wechatLogin")
@ResponseBody
public AjaxResult WechatLogin(HttpServletRequest request)
{
String code= request.getParameter("code");
//String state = request.getParameter("state");
String username=wechatApiService.GetLoginNameWithWechatCode(code);
String password=code;
String rememberMe="0";
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
Subject subject = SecurityUtils.getSubject();
try
{
subject.login(token);
return success();
}
catch (AuthenticationException e)
{
String msg = "用户或密码错误";
if (StringUtils.isNotEmpty(e.getMessage()))
{
msg = e.getMessage();
}
return error(msg);
}
}
@GetMapping("anon/userInfo")
public Map<String, Object> getJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
Map<String, Object> params = JSON.parseObject(responseStrBuilder.toString(), Map.class);
return params;
}
}

View File

@ -19,7 +19,7 @@ server:
port: 80
servlet:
# 应用的访问路径
context-path: /it_war
context-path: /it
tomcat:
# tomcat的URI编码
uri-encoding: UTF-8

View File

@ -1,4 +1,4 @@
package com.ruoyi.bps.domain;
package com.ruoyi.system.domain;
import java.util.Date;

View File

@ -1,4 +1,4 @@
package com.ruoyi.bps.domain;
package com.ruoyi.system.domain;
import java.util.List;

View File

@ -126,4 +126,12 @@ public interface SysUserMapper
* 删除Ecology同步过来的用户
*/
public void deleteEcologySyncUser();
/**
* 查询用户列表
*
* @param sysUser 用户信息
* @return 用户信息集合信息
*/
public List<SysUser> selectUserLists(SysUser sysUser);
}

View File

@ -1,6 +1,6 @@
package com.ruoyi.bps.mapper;
package com.ruoyi.system.mapper;
import com.ruoyi.bps.domain.WechatAccessToken;
import com.ruoyi.system.domain.WechatAccessToken;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

View File

@ -209,4 +209,12 @@ public interface ISysUserService
* Ecology人员信息同步
*/
public int syncEcologyUser(String url,String params);
/**
* 查询用户列表
*
* @param user 用户信息
* @return 用户信息集合信息
*/
public List<SysUser> selectUserLists(SysUser user);
}

View File

@ -0,0 +1,9 @@
package com.ruoyi.system.service;
public interface IWechatApiService {
//获取Access Token
public String GetAccessToken();
//根据企业微信登录身份获取本地LoginName
public String GetLoginNameWithWechatCode(String code);
}

View File

@ -540,6 +540,17 @@ public class SysUserServiceImpl implements ISysUserService
return result;
}
/**
* 查询用户列表
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@Override
public List<SysUser> selectUserLists(SysUser user) {
return userMapper.selectUserLists(user);
}
@SuppressWarnings("unchecked")
public int userSync(Map<String,String> mapResult){
//如果接口返回状态码不为200则不做同步处理

View File

@ -1,12 +1,15 @@
package com.ruoyi.bps.service.impl;
package com.ruoyi.system.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.bps.domain.WechatAccessToken;
import com.ruoyi.bps.mapper.WechatAccessTokenMapper;
import com.ruoyi.bps.service.IWechatApiService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.WechatAccessToken;
import com.ruoyi.system.mapper.WechatAccessTokenMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IWechatApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@ -26,7 +29,14 @@ public class WechatApiServiceImpl implements IWechatApiService {
@Autowired
WechatAccessTokenMapper wechatAccessTokenMapper;
@Autowired
IWechatApiService wechatApiService;
@Autowired
private ISysUserService userService;
/**
*
* 获取企业微信Access Token
* @return Access Token
*/
@ -84,13 +94,62 @@ public class WechatApiServiceImpl implements IWechatApiService {
return wechatAccessToken;
}
/**
*
* @param minuendDate 被减数日期
* @param subtractionDate 减数日期
* @return 相差秒数
*/
public int differenceSecond(Date minuendDate, Date subtractionDate ) {
//获取相两个日期相差秒数
private int differenceSecond(Date minuendDate, Date subtractionDate ) {
return (int)((minuendDate.getTime() - subtractionDate.getTime()) / 1000);
}
/**
* 根据企业微信登录身份获取本地LoginName
*
* @param code
* @return LoginName
*/
@Override
public String GetLoginNameWithWechatCode(String code)
{
//获取访问用户身份ID
String url="https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
String param = "access_token="+wechatApiService.GetAccessToken()+"&code="+code;
//String userInfo = HttpUtils.sendGet(url,param); //测试已能正常返回UserInfo Json,正式使用时打开
String userInfo = "{\"UserId\":\"359\",\"DeviceId\":\"10000589102865WJ\",\"errcode\":0,\"errmsg\":\"ok\"}"; //为避免去微信获取code麻烦开发调试时打开
JSONObject jsonObjectUserInfo = JSONObject.parseObject(userInfo);
//如果返回码不为0则返回错误信息
if ( Integer.parseInt(jsonObjectUserInfo.getString("errcode")) != 0){
return jsonObjectUserInfo.getString("errmsg");
}
String userId = jsonObjectUserInfo.getString("UserId");
//获取用户邮箱与姓名
url="https://qyapi.weixin.qq.com/cgi-bin/user/get";
param="access_token="+wechatApiService.GetAccessToken()+"&userid="+userId;
String userInfoDetail=HttpUtils.sendGet(url,param); //获取成员信息
JSONObject jsonObjectUserInfoDetail=JSONObject.parseObject(userInfoDetail);
//如果返回码不为0则返回错误信息
if(Integer.parseInt(jsonObjectUserInfoDetail.getString("errcode")) != 0)
{
return jsonObjectUserInfo.getString("errmsg");
}
String userEmail= jsonObjectUserInfoDetail.getString("email");
String userName= jsonObjectUserInfoDetail.getString("name");
//根据邮箱名+用户名匹配本地用户对应的邮箱名与用户名
SysUser sysUser=new SysUser();
sysUser.setUserName(userName);
sysUser.setEmail(userEmail);
sysUser.setUserType("02"); //只获取从OA同步的用户保持与企业微信一致
List<SysUser> userList= userService.selectUserLists(sysUser);
int count= userList.size();
if(count <= 0){
return ""; //系统里没有用户没有从OA同步 处理逻辑待定
}
if(count > 1){
return ""; //本地数据库存在多个姓名与邮箱相同的记录如何处理
}
String loginName= userList.get(0).getLoginName();
return loginName;
}
}

View File

@ -227,5 +227,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<delete id="deleteEcologySyncUser" >
delete from sys_user where user_type ='02'
</delete>
<select id="selectUserLists" parameterType="SysUser" resultMap="SysUserResult">
select user_id, dept_id, login_name, user_name, user_type, email, avatar, phonenumber, password, sex, salt, status, del_flag, login_ip, login_date, create_by, create_time, remark from sys_user
where del_flag = '0'
<if test="userName != null and userName != ''">
AND user_name = #{userName}
</if>
<if test="email != null and email != ''">
AND email = #{email}
</if>
<if test="userType != null and userType != ''">
AND user_type = #{userType}
</if>
</select>
</mapper>

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.bps.mapper.WechatAccessTokenMapper">
<mapper namespace="com.ruoyi.system.mapper.WechatAccessTokenMapper">
<resultMap type="WechatAccessToken" id="WechatAccessTokenResult">
<result property="sid" column="sid" />