diff --git a/dq-financial-crms-auth/target/classes/bootstrap.properties b/dq-financial-crms-auth/target/classes/bootstrap.properties deleted file mode 100644 index 160ddf8b..00000000 --- a/dq-financial-crms-auth/target/classes/bootstrap.properties +++ /dev/null @@ -1,19 +0,0 @@ - -#服务名称 -spring.application.name=dq-financial-crms -#配置中心地址 -spring.cloud.nacos.config.server-addr=192.168.31.142:8848 -spring.cloud.nacos.config.namespace=37d72d30-3178-4173-8b5e-269a23355ed9 -#spring.cloud.nacos.config.group=prod - -spring.cloud.nacos.config.ext-config[0].data-id=datasource.yml -spring.cloud.nacos.config.ext-config[0].group=dev -spring.cloud.nacos.config.ext-config[0].refresh=true - -spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml -spring.cloud.nacos.config.ext-config[1].group=dev -spring.cloud.nacos.config.ext-config[1].refresh=true - -spring.cloud.nacos.config.ext-config[2].data-id=other.yml -spring.cloud.nacos.config.ext-config[2].group=dev -spring.cloud.nacos.config.ext-config[2].refresh=true \ No newline at end of file diff --git a/dq-financial-crms-auth/target/classes/logback-spring.xml b/dq-financial-crms-auth/target/classes/logback-spring.xml deleted file mode 100644 index 9d44c6c1..00000000 --- a/dq-financial-crms-auth/target/classes/logback-spring.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - utf8 - - - - - - - - ${LOG_HOME}/crms_auth.%d{yyyy-MM-dd}.log - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - 0 - - 512 - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/dq-financial-crms-auth/target/classes/mapper/crmsauth/CustomerLoginMapper.xml b/dq-financial-crms-auth/target/classes/mapper/crmsauth/CustomerLoginMapper.xml deleted file mode 100644 index ac1cc76d..00000000 --- a/dq-financial-crms-auth/target/classes/mapper/crmsauth/CustomerLoginMapper.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/dq-financial-crms-auth/target/test-classes/com/daqing/financial/crauth/DqFinancialCrmsAuthApplicationTests.class b/dq-financial-crms-auth/target/test-classes/com/daqing/financial/crauth/DqFinancialCrmsAuthApplicationTests.class deleted file mode 100644 index d5a57e61..00000000 Binary files a/dq-financial-crms-auth/target/test-classes/com/daqing/financial/crauth/DqFinancialCrmsAuthApplicationTests.class and /dev/null differ diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/config/WeChatConfig.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/config/WeChatConfig.java new file mode 100644 index 00000000..d467eff5 --- /dev/null +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/config/WeChatConfig.java @@ -0,0 +1,61 @@ +package com.daqing.financial.hrauth.config; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +/** + * 寰俊閰嶇疆绫 + */ +@Configuration +@PropertySource(value="classpath:bootstrap.properties") +@Data +public class WeChatConfig { + + /** + * 鍏紬鍙穉ppid + */ + //@Value("${wxpay.appid}") + //private String appId; + + /** + * 鍏紬鍙风閽 + */ + //@Value("${wxpay.appsecret}") + //private String appsecret; + + /** + * 寮鏀惧钩鍙癮ppid + */ + @Value("${wxopen.appid}") + private String openAppid; + + /** + * 寮鏀惧钩鍙癮ppsecret + */ + @Value("${wxopen.appsecret}") + private String openAppsecret; + + /** + * 寮鏀惧钩鍙板洖璋僽rl + */ + @Value("${wxopen.redirect_url}") + private String openRedirectUrl; + + /** + * 寰俊寮鏀惧钩鍙颁簩缁寸爜杩炴帴 + */ + private String OPEN_QRCODE_URL= "https://open.weixin.qq.com/" + + "connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect"; + + /** + * 寮鏀惧钩鍙拌幏鍙朼ccess_token鍦板潃 + */ + private String OPEN_ACCESS_TOKEN_URL="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; + + /** + * 鑾峰彇鐢ㄦ埛淇℃伅 + */ + private String OPEN_USER_INFO_URL ="https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN"; +} diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/controller/UserLoginController.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/controller/UserLoginController.java index 19236ee1..11c3f03f 100644 --- a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/controller/UserLoginController.java +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/controller/UserLoginController.java @@ -1,18 +1,26 @@ package com.daqing.financial.hrauth.controller; import com.daqing.financial.hrauth.UserLoginControllerApi; +import com.daqing.financial.hrauth.config.WeChatConfig; import com.daqing.financial.hrauth.service.UserLoginService; +import com.daqing.financial.hrauth.service.UserService; +import com.daqing.framework.domain.hrms.UserEntity; import com.daqing.framework.domain.hrms.request.LoginRequest; import com.daqing.framework.domain.hrms.request.ResetPwdRequest; import com.daqing.framework.domain.hrms.request.UpdatePwdRequest; import com.daqing.framework.domain.hrms.request.UserLoginRequest; import com.daqing.framework.model.response.ResponseResult; +import com.daqing.framework.util.JwtUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; /** @@ -26,6 +34,10 @@ public class UserLoginController implements UserLoginControllerApi { @Autowired private UserLoginService userLoginService; + @Autowired + private WeChatConfig weChatConfig; + @Autowired + private UserService userService; @PostMapping("/loginSuccess") public String loginSuccess(){ @@ -77,4 +89,46 @@ public class UserLoginController implements UserLoginControllerApi { boolean result = userLoginService.updatePwd(user); return result ? ResponseResult.SUCCESS() : ResponseResult.FAIL(); } + + /** + * 鎷艰寰俊鎵竴鎵櫥褰晆rl + * accessPage鏄寚鐢ㄦ埛褰撳墠鎵鍦ㄧ殑椤甸潰锛屽氨鏄敤鎴锋壂鐮佹椂褰撳墠鎵鍦ㄩ〉闈 + * @return + */ + @GetMapping("wxLoginUrl") + @ResponseBody + public ResponseResult loginUrl(@RequestParam(value = "accessPage",required = true)String accessPage) throws UnsupportedEncodingException { + + String redirectUrl = weChatConfig.getOpenRedirectUrl(); //鑾峰彇寮鏀惧钩鍙伴噸瀹氬悜鍦板潃锛屽氨鏄敤鎴锋壂瀹岀爜寰俊鍥炶皟鎴戜滑鐨勫湴鍧 + + String callbackUrl = URLEncoder.encode(redirectUrl,"GBK"); //杩涜缂栫爜 + + String qrcodeUrl = String.format(weChatConfig.getOPEN_QRCODE_URL(),weChatConfig.getOpenAppid(),callbackUrl,accessPage); + + return ResponseResult.SUCCESS(qrcodeUrl); + } + + /** + * 寰俊鎵爜鐧诲綍锛岀敤鎴风‘璁ゆ巿鏉冨悗锛屽井淇″洖璋冩垜浠湇鍔″櫒鐨勬帴鍙 + * 杩欎釜鎺ュ彛閲岀殑閫昏緫搴旇鏄嬁鍒板井淇″洖浼犵殑鎺堟潈code鐮佽皟鐢ㄥ井淇$殑鏈嶅姟鑾峰彇access_token + * 鑾峰彇鐨刟ccess_token鐨勮繑鍥炲奸噷鏈塷penId锛岀敤openId鍐嶆璋冨井淇℃湇鍔″幓鎷跨敤鎴风殑淇℃伅 + * @param code + * @param state 鐢ㄦ埛鎵爜鏃跺綋鍓嶆墍鍦ㄩ〉闈 + * @param response + * @throws IOException + */ + @GetMapping("/user/callback") + public void wechatUserCallback(@RequestParam(value = "code",required = true) String code, + String state, HttpServletResponse response) throws IOException { + UserEntity user = userService.saveWeChatUser(code); + if(user != null){ + long times = 86400; + //鐢熸垚jwt + String token = JwtUtils.createJWT(user.getId()+"","token", times); + // state 褰撳墠鐢ㄦ埛鐨勯〉闈㈠湴鍧锛岄渶瑕佹嫾鎺 http:// 杩欐牱鎵嶄笉浼氱珯鍐呰烦杞 + response.sendRedirect(state+"?token="+token); + //response.sendRedirect(state+"?token="+token+"&head_img="+user.getHeadImg()+"&name="+URLEncoder.encode(user.getName(),"UTF-8")); + } + } + } diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/dao/UserLoginDao.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/dao/UserLoginDao.java index 439b9691..e6bfbc2d 100644 --- a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/dao/UserLoginDao.java +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/dao/UserLoginDao.java @@ -19,4 +19,5 @@ public interface UserLoginDao extends BaseMapper { UserEntity login(LoginRequest loginRequest); UserEntity selectByPhoneAccount(String phoneAccount); + UserEntity findByOpenid(String openId); } diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/UserService.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/UserService.java new file mode 100644 index 00000000..43e4c699 --- /dev/null +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/UserService.java @@ -0,0 +1,12 @@ +package com.daqing.financial.hrauth.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.daqing.framework.domain.hrms.UserEntity; + +/** + * @auther River + * @date 2020/9/22 15:00 + */ +public interface UserService extends IService { + UserEntity saveWeChatUser(String code); +} diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserServiceImpl.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserServiceImpl.java new file mode 100644 index 00000000..133f5e41 --- /dev/null +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserServiceImpl.java @@ -0,0 +1,76 @@ +package com.daqing.financial.hrauth.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.daqing.financial.hrauth.config.WeChatConfig; +import com.daqing.financial.hrauth.dao.UserLoginDao; +import com.daqing.financial.hrauth.service.UserService; +import com.daqing.financial.hrauth.util.HttpUtils; +import com.daqing.framework.domain.hrms.UserEntity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.Map; + +@Service +public class UserServiceImpl extends ServiceImpl implements UserService { + + @Autowired + private WeChatConfig weChatConfig; + + @Resource + private UserLoginDao userMapper; + + @Override + public UserEntity saveWeChatUser(String code) { + + String accessTokenUrl = String.format(weChatConfig.getOPEN_ACCESS_TOKEN_URL(),weChatConfig.getOpenAppid(),weChatConfig.getOpenAppsecret(),code); + + //鑾峰彇access_token + Map baseMap = HttpUtils.doGet(accessTokenUrl); + + if(baseMap == null || baseMap.isEmpty()){ return null; } + String accessToken = (String)baseMap.get("access_token"); + String openId = (String) baseMap.get("openid"); + + UserEntity dbUser = userMapper.findByOpenid(openId); + + if(dbUser!=null) { //鏇存柊鐢ㄦ埛锛岀洿鎺ヨ繑鍥 + return dbUser; + } + + //鑾峰彇鐢ㄦ埛鍩烘湰淇℃伅 + String userInfoUrl = String.format(weChatConfig.getOPEN_USER_INFO_URL(),accessToken,openId); + Map baseUserMap = HttpUtils.doGet(userInfoUrl); + + if(baseUserMap == null || baseUserMap.isEmpty()){ + return null; + } +/* String nickname = (String)baseUserMap.get("nickname"); + + Double sexTemp = (Double) baseUserMap.get("sex"); + int sex = sexTemp.intValue(); + String province = (String)baseUserMap.get("province"); + String city = (String)baseUserMap.get("city"); + String country = (String)baseUserMap.get("country"); + String headimgurl = (String)baseUserMap.get("headimgurl"); + StringBuilder sb = new StringBuilder(country).append("||").append(province).append("||").append(city); + String finalAddress = sb.toString(); + try { + //瑙e喅涔辩爜 + nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8"); + finalAddress = new String(finalAddress.getBytes("ISO-8859-1"), "UTF-8"); + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + }*/ + + UserEntity user = new UserEntity(); + user.setWechatId(openId); + user.setCreateTime(new Date()); + userMapper.insert(user); + return user; + } +} diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/util/HttpUtils.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/util/HttpUtils.java new file mode 100644 index 00000000..e75e62fb --- /dev/null +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/util/HttpUtils.java @@ -0,0 +1,106 @@ +package com.daqing.financial.hrauth.util; + +import com.google.gson.Gson; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import java.util.HashMap; +import java.util.Map; + +/** + * 灏佽http get post + */ +public class HttpUtils { + + private static final Gson gson = new Gson(); + + /** + * get鏂规硶 + * @param url + * @return + */ + public static Map doGet(String url){ + + Map map = new HashMap<>(); + CloseableHttpClient httpClient = HttpClients.createDefault(); + + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) //杩炴帴瓒呮椂 + .setConnectionRequestTimeout(5000)//璇锋眰瓒呮椂 + .setSocketTimeout(5000) + .setRedirectsEnabled(true) //鍏佽鑷姩閲嶅畾鍚 + .build(); + + HttpGet httpGet = new HttpGet(url); + httpGet.setConfig(requestConfig); + + try{ + HttpResponse httpResponse = httpClient.execute(httpGet); + if(httpResponse.getStatusLine().getStatusCode() == 200){ + + String jsonResult = EntityUtils.toString( httpResponse.getEntity()); + map = gson.fromJson(jsonResult,map.getClass()); + } + + }catch (Exception e){ + e.printStackTrace(); + }finally { + try { + httpClient.close(); + }catch (Exception e){ + e.printStackTrace(); + } + } + return map; + } + + /** + * 灏佽post + * @return + */ + public static String doPost(String url, String data,int timeout){ + CloseableHttpClient httpClient = HttpClients.createDefault(); + //瓒呮椂璁剧疆 + + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout) //杩炴帴瓒呮椂 + .setConnectionRequestTimeout(timeout)//璇锋眰瓒呮椂 + .setSocketTimeout(timeout) + .setRedirectsEnabled(true) //鍏佽鑷姩閲嶅畾鍚 + .build(); + + HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); + httpPost.addHeader("Content-Type","text/html; chartset=UTF-8"); + + if(data != null && data instanceof String){ //浣跨敤瀛楃涓蹭紶鍙 + StringEntity stringEntity = new StringEntity(data,"UTF-8"); + httpPost.setEntity(stringEntity); + } + + try{ + CloseableHttpResponse httpResponse = httpClient.execute(httpPost); + HttpEntity httpEntity = httpResponse.getEntity(); + if(httpResponse.getStatusLine().getStatusCode() == 200){ + String result = EntityUtils.toString(httpEntity); + return result; + } + + }catch (Exception e){ + e.printStackTrace(); + }finally { + try{ + httpClient.close(); + }catch (Exception e){ + e.printStackTrace(); + } + } + return null; + } + +} \ No newline at end of file diff --git a/dq-financial-hrms-auth/src/main/resources/bootstrap.properties b/dq-financial-hrms-auth/src/main/resources/bootstrap.properties index b2c4518d..caddf3ab 100644 --- a/dq-financial-hrms-auth/src/main/resources/bootstrap.properties +++ b/dq-financial-hrms-auth/src/main/resources/bootstrap.properties @@ -27,6 +27,11 @@ spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.min-idle=0 +#寰俊寮鏀惧钩鍙伴厤缃 +wxopen.appid=wx3b7bca679da34921 +wxopen.appsecret=c8de113c18052b4e2173d913511e486d +#鍥炶皟鍦板潃 +wxopen.redirect_url=http://www.occupationlab.com/apiHrmsAuth/hrms/auth/userlogin/user/callback # 姝e紡鐜锛坧rod锛 #spring.application.name=dq-financial-hrms-auth diff --git a/dq-financial-hrms-auth/src/main/resources/mapper/hrmsauth/UserLoginMapper.xml b/dq-financial-hrms-auth/src/main/resources/mapper/hrmsauth/UserLoginMapper.xml index fd2d7237..e813de8a 100644 --- a/dq-financial-hrms-auth/src/main/resources/mapper/hrmsauth/UserLoginMapper.xml +++ b/dq-financial-hrms-auth/src/main/resources/mapper/hrmsauth/UserLoginMapper.xml @@ -37,5 +37,8 @@ + \ No newline at end of file diff --git a/dq-govern-gateway/src/main/resources/jwt.properties b/dq-govern-gateway/src/main/resources/jwt.properties index 6bef2a95..5ffed5bc 100644 --- a/dq-govern-gateway/src/main/resources/jwt.properties +++ b/dq-govern-gateway/src/main/resources/jwt.properties @@ -4,4 +4,5 @@ jwt.ignoreUrlList=/apiHrmsAuth/hrms/auth/userlogin/getBackPwd,\ /api-crms/sweagger-ui.html,\ /api-crms/crms/customer/excelTemplate,\ /api-crms/crms/customer/excelExport,\ - /api-crms/crms/customer/excelImport \ No newline at end of file + /api-crms/crms/customer/excelImport,\ + /apiHrmsAuth/hrms/auth/userlogin/wxLoginUrl \ No newline at end of file