Parcourir la source

:sparkles: 添加新特性。完善後台綁定的代碼

冷冷 il y a 7 ans
Parent
commit
36b2ce817e

+ 0 - 5
pigx-auth/src/main/java/com/pig4cloud/pigx/auth/PigxAuthApplication.java

@@ -38,9 +38,4 @@ public class PigxAuthApplication {
 	public static void main(String[] args) {
 		SpringApplication.run(PigxAuthApplication.class, args);
 	}
-
-	@Bean
-	public RestTemplate restTemplate() {
-		return new RestTemplate();
-	}
 }

+ 1 - 1
pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java

@@ -94,7 +94,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
 			String url = String.format(WX_AUTHORIZATION_CODE_URL
 				, wxSocialConfig.getAppid(), wxSocialConfig.getSecret(), code);
 			String result = restTemplate.getForObject(url, String.class);
-			log.warn("微信响应报文:{}", result);
+			log.debug("微信响应报文:{}", result);
 
 			Object obj = JSONUtil.parseObj(result).get("openid");
 			if (obj != null) {

+ 35 - 0
pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/RestTemplateConfig.java

@@ -0,0 +1,35 @@
+/*
+ *    Copyright (c) 2018-2025, lengleng All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the pig4cloud.com developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: lengleng (wangiegie@gmail.com)
+ */
+
+package com.pig4cloud.pigx.common.core.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @author lengleng
+ * @date 2018/8/16
+ * RestTemplate
+ */
+@Configuration
+public class RestTemplateConfig {
+	@Bean
+	public RestTemplate restTemplate() {
+		return new RestTemplate();
+	}
+}

+ 1 - 0
pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories

@@ -1,4 +1,5 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
   com.pig4cloud.pigx.common.core.config.JacksonConfig,\
   com.pig4cloud.pigx.common.core.config.RedisConfig,\
+  com.pig4cloud.pigx.common.core.config.RestTemplateConfig,\
   com.pig4cloud.pigx.common.core.util.SpringContextHolder

+ 1 - 1
pigx-config/src/main/resources/config/application-dev.yml

@@ -93,7 +93,7 @@ swagger:
 ## spring security 配置
 security:
   auth:
-    server: http://pigx-gateway:9999/auth/oauth
+    server: http://localhost:9999/auth/oauth
   oauth2:
     resource:
       token-info-uri: ${security.auth.server}/check_token

+ 12 - 0
pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/UserController.java

@@ -91,6 +91,18 @@ public class UserController {
 		return new R<>(userService.findUserInfo(type, openid));
 	}
 
+	/**
+	 * 绑定社交账号
+	 *
+	 * @param state 类型
+	 * @param code  code
+	 * @return
+	 */
+	@PostMapping("/social/bind")
+	public R<Boolean> bind(String state, String code) {
+		return new R<>(userService.bindSocial(state, code));
+	}
+
 	/**
 	 * 通过ID查询当前用户信息
 	 *

+ 9 - 0
pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysUserService.java

@@ -83,4 +83,13 @@ public interface SysUserService extends IService<SysUser> {
 	 * @return 用户信息
 	 */
 	UserVO selectUserVoById(Integer id);
+
+	/**
+	 * 绑定社交账号
+	 *
+	 * @param state 类型
+	 * @param code  code
+	 * @return
+	 */
+	Boolean bindSocial(String state, String code);
 }

+ 43 - 10
pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java

@@ -22,6 +22,7 @@ package com.pig4cloud.pigx.admin.service.impl;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
@@ -40,15 +41,18 @@ import com.pig4cloud.pigx.admin.service.SysUserService;
 import com.pig4cloud.pigx.common.core.constant.enums.EnumLoginType;
 import com.pig4cloud.pigx.common.core.util.Query;
 import com.pig4cloud.pigx.common.core.util.R;
+import com.pig4cloud.pigx.common.security.util.SecurityUtils;
+import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.BeanUtils;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.CacheManager;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
 
 import java.time.LocalDateTime;
 import java.util.ArrayList;
@@ -62,16 +66,17 @@ import java.util.Set;
  */
 @Slf4j
 @Service
+@AllArgsConstructor
 public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
 	private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
-	@Autowired
-	private SysMenuService sysMenuService;
-	@Autowired
-	private SysUserMapper sysUserMapper;
-	@Autowired
-	private SysRoleService sysRoleService;
-	@Autowired
-	private SysUserRoleService sysUserRoleService;
+	private static final String WX_AUTHORIZATION_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token" +
+		"?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
+	private final SysMenuService sysMenuService;
+	private final SysUserMapper sysUserMapper;
+	private final CacheManager cacheManager;
+	private final RestTemplate restTemplate;
+	private final SysRoleService sysRoleService;
+	private final SysUserRoleService sysUserRoleService;
 
 	/**
 	 * 通过用户名查用户的全部信息
@@ -80,7 +85,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
 	 * @return
 	 */
 	@Override
-	@Cacheable(value = "user_details", key = "#type +':'+ #username")
+	@Cacheable(value = "user_details", key = "#username")
 	public UserInfo findUserInfo(String type, String username) {
 		SysUser condition = new SysUser();
 		if (EnumLoginType.PWD.getType().equals(type)) {
@@ -122,6 +127,34 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
 		return userInfo;
 	}
 
+	/**
+	 * 绑定社交账号
+	 *
+	 * @param state 类型
+	 * @param code  code
+	 * @return
+	 */
+	@Override
+	public Boolean bindSocial(String state, String code) {
+		String result = restTemplate.getForObject(WX_AUTHORIZATION_CODE_URL, String.class);
+		log.debug("微信响应报文:{}", result);
+
+		Object obj = JSONUtil.parseObj(result).get("openid");
+		if (obj == null) {
+			return Boolean.FALSE;
+		}
+
+		SysUser condition = new SysUser();
+		condition.setUsername(SecurityUtils.getUser());
+		SysUser sysUser = this.selectOne(new EntityWrapper<>(condition));
+		sysUser.setWxOpenid(obj.toString());
+		sysUserMapper.updateAllColumnById(sysUser);
+
+		//更新緩存
+		cacheManager.getCache("user_details").evict(sysUser.getUsername());
+		return Boolean.TRUE;
+	}
+
 	@Override
 	public Page selectWithRolePage(Query query) {
 		Object username = query.getCondition().get("username");