| 
					
				 | 
			
			
				@@ -1,5 +1,6 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package com.huaxu.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.baomidou.mybatisplus.core.metadata.IPage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.huaxu.dao.LoginLogMapper; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -19,11 +20,17 @@ import com.huaxu.util.ByteArrayUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.huaxu.util.RedisUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.huaxu.util.UserUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.huaxu.util.Util; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.ibatis.annotations.Param; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.data.redis.connection.RedisConnection; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpEntity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpHeaders; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpStatus; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.ResponseEntity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.security.core.GrantedAuthority; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.security.core.authority.SimpleGrantedAuthority; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.security.core.context.SecurityContextHolder; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -34,7 +41,11 @@ import org.springframework.security.oauth2.provider.token.AuthorizationServerTok 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.security.oauth2.provider.token.ConsumerTokenServices; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.security.oauth2.provider.token.TokenStore; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.util.LinkedMultiValueMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.util.MultiValueMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.util.ReflectionUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.client.RestTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import sun.misc.BASE64Encoder; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import javax.servlet.http.HttpServletRequest; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.*; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -47,6 +58,7 @@ import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * @since 2020-10-21 15:23:52 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Service("userService") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 public class UserServiceImpl implements UserService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private UserMapper userMapper; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -65,7 +77,8 @@ public class UserServiceImpl implements UserService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private LoginLogMapper loginLogMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private TokenStore tokenStore; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${iot.url}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String iotUrl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * 通过ID查询单条数据 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -184,6 +197,37 @@ public class UserServiceImpl implements UserService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             tokenStore.removeAccessToken(oAuth2AccessToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String getRangeCode(String appId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        LoginUser currentUser = UserUtil.getCurrentUser(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Integer id = currentUser.getId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String appSecret=userMapper.getAppSecret(appId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String uniqueUserID=userMapper.getUniqueUserID(id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Random random = new Random(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String code=""; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(uniqueUserID!=null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (int i=0;i<6;i++) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                code+=random.nextInt(10); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String key=code+"_"+appId+"_"+appSecret; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            redisUtil.set(key,uniqueUserID); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            redisUtil.setExpire(key,300); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return code; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String getUniqId(String appId, String appSecret, String code) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String key=code+"_"+appId+"_"+appSecret; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return redisUtil.get(key); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public User chooseUser(User user) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -273,12 +317,50 @@ public class UserServiceImpl implements UserService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         OAuth2Authentication oAuth2AuthenticationNew = new OAuth2Authentication(oAuth2Authentication.getOAuth2Request(), authenticationTokenReslut); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         OAuth2AccessToken accessToken = authorizationServerTokenServices.createAccessToken(oAuth2AuthenticationNew); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //可以加一个映射表 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String iotToken=getLoginIotToken(user.getIotPhoneNumber()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(iotToken!=null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String iotTokenKey = "iotToken:" + accessToken.getValue(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            redisUtil.set(iotTokenKey,iotToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            redisUtil.setExpire(iotTokenKey,60*30); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         insertLoginLog(loginUser,"登录"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         user.setToken(accessToken.getValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return user; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String getLoginIotToken(String account){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("begin IotService login account={}",account); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String url = iotUrl + "/api/integration/auth/login"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        RestTemplate restTemplate = new RestTemplate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(account==null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        map.add("source","rmcp"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        map.add("code",new BASE64Encoder().encode(account.getBytes())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        HttpHeaders headers = new HttpHeaders(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map,headers); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ResponseEntity<JSONObject> responseEntity  = restTemplate.postForEntity(url, request, JSONObject.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("responseEntity ={}", JSONObject.toJSONString(responseEntity)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(responseEntity !=null && responseEntity.getStatusCode() == HttpStatus.OK){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            JSONObject jsonObject =  responseEntity.getBody(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(jsonObject != null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(jsonObject.getInteger("status")==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    String token = jsonObject.getString("data"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    log.info("IotService getLoginIotToken success res={}",token); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return token; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    log.info("IotService getLoginIotToken fail"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private void getDataPermission(int permissionType,User user, List<ProgramItem> programItemList){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ProgramItem defaultProgramItem=new ProgramItem(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         defaultProgramItem.setOrgCompanyId(-999); 
			 |