zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.changhong.epc.zuul.service.impl;
 
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.exception.EPCServiceException;
import com.changhong.epc.constter.system.UserInfo;
import com.changhong.epc.constter.system.UserInfo.LogInInfo;
import com.changhong.epc.constter.system.prop.ChangHongProperties;
import com.changhong.epc.constter.system.prop.ChangHongProperties.PassPortName;
import com.changhong.epc.rely.api.service.CscApiService;
import com.changhong.epc.rely.api.tool.CscAesUtils;
import com.changhong.epc.zuul.service.LoginService;
import com.changhong.epc.zuul.util.HttpUtil;
import com.iemsoft.framework.cloud.core.password.Des;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.Assert;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
 
//import com.changhong.ubp.login.service.CscApiService;
 
@Service("loginService")
public class LoginServiceImpl implements LoginService, BaseConst {
 
    @Resource
    private CscApiService cscApiService;
 
    @Value("${aes.key:0123456789ABCDEF}")
    private String aesKey;
    
//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;
    
    private static final Map<String, UserInfo> USER_INFOS = new HashMap<>();
    
    private static String infoId=ThreadData.get(TENANT_ID)+"_"+ThreadData.get(COMPANY_ID)+"_"+ThreadData.get(SERVICE_ID)+"_"+"ubp";
    
    /**
     * 获得用户信息类
     */
    @Autowired
    private UserInfoServiceImpl userInfoService;
    
 
    @Override
    public UserInfo getUserInfo(LogInInfo loginInfo) {
        if(Objects.equals(loginInfo.getOpenId(),"test")){
            return JSONTool.toObj("{\"birthday\":\"2016-02-02\",\"bpsUserName\":\"76_13_17_1_ubp\",\"changeTimes\":\"1\",\"city\":\"石家庄市\",\"county\":\"市辖区\",\"emailVerified\":\"0\",\"interests\":\"\",\"ipAddress\":\"54.222.240.185\",\"isSuperManager\":true,\"loginInfo\":{\"openId\":\"893ccd52f0ec4eae\",\"token\":\"0e704547f54583ad430fe09f83e4b524\"},\"loginTime\":\"2017-11-23 13:23:26\",\"passwordStrength\":\"1\",\"phone\":\"15140615455\",\"pkgName\":\"http://chweb.changhong.com\",\"province\":\"河北省\",\"registerTime\":\"2017-05-16 14:28:14\",\"sex\":\"男\",\"uid\":\"\",\"updateTime\":\"2017-10-24 19:51:20\",\"userCode\":\"893ccd52f0ec4eae\",\"userName\":\"王煜鑫\",\"workFlowCode\":\"D1B67990431B7788CF09CECBC3B938B6\"}", UserInfo.class);
        }
        ThreadData.set(OPEN_ID, loginInfo.getOpenId());
//        String token=redisTemplate.opsForValue().get(loginInfo.getOpenId()).toString();
//        if(!Objects.equals(token, loginInfo.getToken())){
//            //Assert.empty(null, N0001);
//          throw new EPCServiceException(N0001, N0001);
//        }
        
        return userInfoService.getUserInfo(loginInfo);
    }
 
    @Override
    public UserInfo getUserInfo(String token) {
        return getUserInfo(getLoginInfo(token));
    }
 
    @Override
    public UserInfo getUserInfo(LoginSecurity loginSecurity) {
        if(Objects.equals(loginSecurity.getOpenId(),"test")){
            return JSONTool.toObj("{\"birthday\":\"2016-02-02\",\"bpsUserName\":\"76_13_17_1_ubp\",\"changeTimes\":\"1\",\"city\":\"石家庄市\",\"county\":\"市辖区\",\"emailVerified\":\"0\",\"interests\":\"\",\"ipAddress\":\"54.222.240.185\",\"isSuperManager\":true,\"loginInfo\":{\"openId\":\"893ccd52f0ec4eae\",\"token\":\"0e704547f54583ad430fe09f83e4b524\"},\"loginTime\":\"2017-11-23 13:23:26\",\"passwordStrength\":\"1\",\"phone\":\"15140615455\",\"pkgName\":\"http://chweb.changhong.com\",\"province\":\"河北省\",\"registerTime\":\"2017-05-16 14:28:14\",\"sex\":\"男\",\"uid\":\"\",\"updateTime\":\"2017-10-24 19:51:20\",\"userCode\":\"893ccd52f0ec4eae\",\"userName\":\"王煜鑫\",\"workFlowCode\":\"D1B67990431B7788CF09CECBC3B938B6\"}", UserInfo.class);        
            }
        return getUserInfo(new LogInInfo(loginSecurity.getToken(), loginSecurity.getOpenId()));
    }
    
    @Override
    public LogInInfo getLoginInfo(String token) {
        LogInInfo loginInfo = JSONTool.toObj(
                new Des().strEnc(token, UserInfo.FIRST_KEY, UserInfo.SECOND_KEY, UserInfo.THIRD_KEY), LogInInfo.class);
        Assert.empty(loginInfo, U0002);
        return loginInfo;
    }
 
    @Service
    public class UserInfoServiceImpl{
 
        @Autowired
        private ChangHongProperties changHongProperties;
        
        /**
         * 获得userInfo
         * @param loginInfo
         * @return
         */
    //    @CacheSave(time=1 * 60 * 60 * 24)
        public UserInfo getUserInfo(LogInInfo loginInfo){
            // System.out.println(">>>>>"+loginInfo);
            UserInfo userInfo = null;
            try {
                        userInfo = HttpUtil.get(
                        changHongProperties.getPassUrl(PassPortName.USER_DETIAL
                                    , new ModelMap("token", loginInfo.getToken())
                                    .addAttribute("openId", loginInfo.getOpenId())
                                    .addAttribute("livemode", changHongProperties.getSsoLivemode()))
                        , null, UserInfo.class);
            } catch (TimeoutException e) {
                throw new EPCServiceException(N0002).setchildIEMExc(changHongProperties.getPassUrl(PassPortName.USER_DETIAL, null));
            }
            Assert.empty(userInfo, N0001);
            userInfo.setLoginInfo(loginInfo);
            userInfo.setAesOpenId(CscAesUtils.encrypt(userInfo.getLoginInfo().getOpenId(), aesKey));
            //setEmpInfo(userInfo); 
            return userInfo;
        }
        /*
            18273479217:12345678
            15140615455:12345678
            15882806952:hr753753
            18681661636:12345678
            17615151309:12345678
        */
//        private void setEmpInfo(UserInfo userInfo) {
//            String userId="";
//            if(ObjectUtil.notEmpty(userInfo.getPhone())&&ObjectUtil.notEmpty(ThreadData.get(TENANT_ID))){
//                userId=getUserId(userInfo.getPhone() );
//                userInfo.setUid(userId);
//            }
//            //ThreadData.get(TENANT_ID)+"_"+ThreadData.get(COMPANY_ID)+"_"+ThreadData.get(SERVICE_ID)+"_"+"ubp")
//            if(ObjectUtil.notEmpty(userId)){
//                userInfo.setBpsUserName((userId+"_"+ThreadData.get(TENANT_ID)+"_"+ThreadData.get(COMPANY_ID)+"_"+ThreadData.get(SERVICE_ID)+"_"+"ubp"));
//            userInfo.setWorkFlowCode(new Des().strEnc((userId+"_"+ThreadData.get(TENANT_ID)+"_"+ThreadData.get(COMPANY_ID)+"_"+ThreadData.get(SERVICE_ID)+"_"+"ubp").toString(), UserInfo.FIRST_KEY, UserInfo.SECOND_KEY, UserInfo.THIRD_KEY));
//            }else{
//                userInfo.setBpsUserName("sysadmin");
//                userInfo.setWorkFlowCode(new Des().strEnc(("sysadmin").toString(), UserInfo.FIRST_KEY, UserInfo.SECOND_KEY, UserInfo.THIRD_KEY));
//            }
//            userInfo.setUserCode(userInfo.getLoginInfo().getOpenId());
//            if(ThreadData.get(Keys.TENANT_ID) != null){
//                userInfo.setTenantId(ThreadData.get(TENANT_ID));
//            }
//        }
//    }
//    public  String getUserId(String phone){
//        String userId="";
//        if(!(ThreadData.get(Keys.APP_KEY).toString().contains(phoneAppKey))){
//              // Object users=redisTemplate.opsForValue().get(ThreadData.get(TENANT_ID)+"_"+ThreadData.get(COMPANY_ID)+"_"+ThreadData.get(SERVICE_ID)+"_"+"ubp"+"allParticipantUser");
//               List<WFParticipantEntity> allUser=new ArrayList<>();
////           if(ObjectUtil.notEmpty(users)){
////                       allUser=JSONTool.toList(users.toString(), WFParticipantEntity.class);
////            }else{
//                com.changhong.epc.constter.system.Service se=new com.changhong.epc.constter.system.Service();
//                se.setAppId(ThreadData.get(COMPANY_ID));
//                se.setAccessId(ThreadData.get(ACCESS_ID));
//                se.setSecurityKey(ThreadData.get(SECURITY_KEY));
//                List<WFParticipantEntity>   pList=cscApiService.findOrganizationByCompanyId(se);
//                getAllUsers(pList,allUser);
//       // }
//        //     allUser=JSONTool.toList(redisTemplate.opsForValue().get(ThreadData.get(TENANT_ID)+"_"+ThreadData.get(COMPANY_ID)+"_"+ThreadData.get(SERVICE_ID)+"_"+"ubp"+"allParticipantUser").toString(),WFParticipantEntity.class);
//            for( WFParticipantEntity user:allUser){
//                if(Objects.equals(phone,user.getPhone())){
//                    userId=user.getId();
//                }
//            }
//        }
//        return userId;
//    }
    
    
//    public static void getAllUsers(List<WFParticipantEntity> pList,List<WFParticipantEntity> orgData){
//        for(WFParticipantEntity org:pList){
//            if("emp".equals(org.getTypeCode())){
//                orgData.add(org);
//            }
//            if(ObjectUtil.notEmpty(org.getChildren()) ){
//                getAllUsers(org.getChildren(),orgData);
//            }
//        }
    }
    
}