P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package foundation.system;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.capacity.Actor;
import foundation.capacity.ActorBucket;
import foundation.capacity.ActorTarget;
import foundation.capacity.ActorsRuntime;
import foundation.capacity.role.Role;
import foundation.capacity.role.RoleMenu;
import foundation.capacity.role.RolePage;
import foundation.dao.preload.Bucket;
import foundation.dao.preload.Tree;
import foundation.data.entity.Entity;
import foundation.data.entity.IDictionary;
import foundation.dictionary.DictionaryBucket;
import foundation.geography.CityBucket;
import foundation.geography.CountyBucket;
import foundation.geography.ProvinceBucket;
import foundation.handler.Handler;
import foundation.json.JObjectReader;
import foundation.json.JSONBuilder;
import foundation.menu.Menu;
import foundation.menu.MenuTree;
import foundation.persist.NamedSQL;
import foundation.persist.SQLRunner;
import foundation.token.UserToken;
import foundation.user.OnlineUser;
import foundation.user.User;
import foundation.user.UserBucket;
import foundation.util.Util;
 
public class ClientHandler extends Handler {
 
    protected static Logger logger;
    private static DateFormat dateFormat;
    
    static {
        logger = LogManager.getLogger(ClientHandler.class); 
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    }
    
    @Override
    protected void publishMethod() {
        //1. login
        addMethod("login");
        
        //2. login
        addMethod("systemLogin");
        
        //3. get user
        addMethod("getUser");
        
        //4. change password
        addMethod("changePassword");
        
        //5. get user capacity
        addMethod("getCapacity");
        
        //6. change user actor
        addMethod("changeCurrentActor");
        
        //7. get menu tree
        addMethod("getMenuTree");
        
        //8. get menu tree all
        addMethod("getMenuTreeAll");
        
        //9. get page
        addMethod("getPage");
        
        //10.  获取字典
        addMethod("getDictionarys");
        
        //11.  获取一个字典
        addMethod("getOneDictionary");
        
        //12. 获取省份列表
        addMethod("getProvinceList");
        
        //13. 获取城市列表
        addMethod("getCityList");
        
        //14.  得到区县列表
        addMethod("getCountyList");
        
        //15. 得到系统当前日期
        addMethod("getCalendar");
        
        //16. 得到对应角色的菜单页面
        addMethod("getPageByRole");
        
        //1. 获取角色页面button、tab
        addMethod("getBtnTabByRolePage");
    }
    
    public void login() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        
        if (dataReader.isEmpty()) {
            resultPool.setSuccess(false);
            return;
        }
        
        String userName = dataReader.getString("userName");
        String password = dataReader.getString("password");
        
        NamedSQL namedSQL = NamedSQL.getInstance("getUser");
        namedSQL.setParam("userName", userName);
        namedSQL.setParam("password", password);
        namedSQL.setParam("is_active", "T");
        
        Entity entity = SQLRunner.getEntity(namedSQL);
        
        if (entity == null) {
            resultPool.setSuccess(false);
            return;
        }
        
        String userCaption = entity.getString("employee_name");
        
        if (Util.isEmpty(userCaption)) {
            userCaption = entity.getString("name");
        }
        
        LoginResult result = new LoginResult(entity);
        resultPool.addValue("data", result);
    }
    
    public void systemLogin() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        
        if (dataReader.isEmpty()) {
            resultPool.setSuccess(false);
            return;
        }
 
        String secret = dataReader.getString("secret");
        String systemId = dataReader.getString("systemId");
        String userSecret = dataReader.getString("userSecret");
        String oaAccount = dataReader.getString("oaAccount");
        
        //1. 进行secret合法性检查
        boolean systemIdentify = "SystemOAWeaver".equalsIgnoreCase(systemId);
        boolean systemSecretentify = "KCMSIEUASKEOIQWANTVWOWPOWPPWIEIR".equalsIgnoreCase(secret);
        
        if (systemIdentify && systemSecretentify) {
            //2. 进行用户存在检查
            NamedSQL namedSQL = NamedSQL.getInstance("getUserByUsername");
            namedSQL.setParam("oaAccount", oaAccount);
            namedSQL.setParam("userSecret", userSecret);
            namedSQL.setParam("isActive", "T");
 
            Entity entity = SQLRunner.getEntity(namedSQL);
            
            if (entity == null) {
                resultPool.setSuccess(false);
                return;
            }
            
            String userCaption = entity.getString("employee_name");
            
            if (Util.isEmpty(userCaption)) {
                userCaption = entity.getString("name");
            }
            
            LoginResult result = new LoginResult(entity);
            resultPool.addValue("data", result);
        }
        else {
            resultPool.setSuccess(false);
            return;
        }
    }
 
    public void getUser() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        
        if (dataReader.isEmpty()) {
            resultPool.setSuccess(false);
            return;
        }
        
        OnlineUser user = OnlineUser.getInstance();
        
        if (user == null) {
            return;
        }
        
        user.collectActors();
        
        resultPool.addValue("user", user);
    }
    
    public void changePassword() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        
        if (dataReader.isEmpty()) {
            resultPool.setSuccess(false);
            return;
        }
        
        OnlineUser user = OnlineUser.getInstance();
        
        if (user == null) {
            return;
        }
        
        String oldPass = dataReader.getString("old_pass");
        String newPass = dataReader.getString("new_pass");
        
        if (Util.isEmpty(newPass) || Util.isEmpty(oldPass)) {
            resultPool.reportOneError("密码修改", "新密码或旧密码为空,不能修改");
            return;
        }
        
        if (!oldPass.equals(user.getPassword())) {
            resultPool.reportOneError("密码修改", "用户输入的旧密码不对,不能修改");
            return;
        }
        
        user.chagnePassword(newPass);
        
        resultPool.reportOneMessage("密码修改", "密码修改成功");
    }
    
    public void getActor() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        String token = null;
        
        if (dataReader.isEmpty()) {
            resultPool.setSuccess(false);
            return;
        }
        
        String userId = dataReader.getString("userId");
        
        UserToken userToken = null;
        if (Util.isEmpty(userId)) {
            token = dataReader.getString("token");
            userToken = UserToken.getInstance(token);
        }
        
        UserBucket userBucket = UserBucket.getInstance();
        
        if (!Util.isEmpty(token) && userToken != null) {
            userId = userToken.getUserId();
        }
        User user = userBucket.get(userId);
        
        if (user == null) {
            return;
        }
        
        ActorsRuntime actors = user.getActorsRuntime();
        resultPool.addValue("actors", actors);
    }
    
    public void changeCurrentActor() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        String actorId = dataReader.getString("actor_target_id");
        
        OnlineUser user = OnlineUser.getInstance();
        
        if (user == null) {
            return;
        }
        
        ActorTarget actor = user.changeCurrentCapacity(actorId);
        resultPool.addValue("capacity", actor);
    }
    
    public void getMenuTreeAll() throws Exception {
        LoadMonitor monitor = new LoadMonitor();
        
        MenuTree menuTree = monitor.get(MenuTree.class);
        List<Menu> menuList = new ArrayList<Menu>();
        Iterator<Menu> menuIterator = menuTree.iterator();
        
        while(menuIterator.hasNext()) {
              Menu menu = menuIterator.next();
              if (Util.isEmpty(menu.getParentId())) {
                  menuList.add(menuTree.get(menu.getId()));
              }
        }
    
        resultPool.addValue("menus", menuList);
    }
    
    public void getMenuTree() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        String actorId = dataReader.getString("actor_id");
        
        //1. 当前用获取自己的菜单
        if (Util.isEmpty(actorId)) {
            OnlineUser user = OnlineUser.getInstance();
            ActorTarget actor = user.getCurrentActor();
            
            if (actor == null) {
                return;
            }
            
            Role role = actor.getRoleRuntime();
            Tree<RoleMenu> roleMenuTree = role.getMenuTree();
            
            resultPool.addValue("menus", roleMenuTree);
            return;
        }
        
        //2. 根据角色获取菜单
        Actor actor = ActorBucket.getInstance().get(actorId);
        
        if (actor == null) {
            return;
        }
        
        Role role = actor.getRoleRuntime();
        Tree<RoleMenu> roleMenuTree = role.getMenuTree();
        
        resultPool.addValue("roleAddress", role.toString());
        resultPool.addValue("menus", roleMenuTree);
    }
    
    public void getPageByRole() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        String actorId = dataReader.getString("actor_id");
        
        //1. 根据角色获取菜单
        Actor actor = ActorBucket.getInstance().get(actorId);
        
        if (actor == null) {
            return;
        }
        
        Role role = actor.getRoleRuntime();
        Bucket<RolePage> pages = role.getPages();
        
        resultPool.addValue("pages", pages);        
    }    
    
    public void getPage() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        String pageId = dataReader.getString(ISystem.PageId);
        String menuId = dataReader.getString(ISystem.MenuId);
        
        OnlineUser user = OnlineUser.getInstance();
        
        if (user == null) {
            return;
        }
        
        ActorTarget actor = user.getCurrentActor();
        
        if (actor == null) {
            return;
        }
        
        Role role = actor.getRoleRuntime();
        String pageKey = RolePage.createKey(menuId, pageId);
        RolePage rolePage = role.getPageById(pageKey);
        resultPool.addValue("page", rolePage);
    }
 
    public void getDictionarys() throws Exception {
        DictionaryBucket dictionarys = DictionaryBucket.getInstance();
        resultPool.addValue("dictionarys", dictionarys);
    }
    
    public void getOneDictionary() throws Exception {
        JObjectReader dataReader = dataPool.getJObjectReader();
        String dictionaryName = dataReader.getString(ISystem.DictionaryCode);
        
        DictionaryBucket dictionaryBucket = DictionaryBucket.getInstance();
        IDictionary dictionary = dictionaryBucket.getDictionary(dictionaryName);
        
        resultPool.addValue("dictionary", dictionary);
    }
    
    public void getProvinceList() throws Exception {
        ProvinceBucket provinceBucket = ProvinceBucket.getInstance();
        resultPool.addValue("provinces", provinceBucket);
    }
    
    public void getCityList() throws Exception {
        CityBucket cityBucket = CityBucket.getInstance();
        resultPool.addValue("citys", cityBucket);
    }
    
    public void getCountyList() throws Exception {
        CountyBucket countyBucket = CountyBucket.getInstance();
        resultPool.addValue("countys", countyBucket);
    }
    
    public void getCalendar() throws Exception {
        //1.
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        
        //2.
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        
        String monthNo = year + "" + (month < 10 ? "0" + month : month); 
        String date = dateFormat.format(now);
        
        //3.
        JSONBuilder builder = new JSONBuilder();
        
        builder.beginObject();
        builder.write("year", year);
        builder.write("month", month);
        builder.write("monthNo", monthNo);
        builder.write("day", calendar.get(Calendar.DAY_OF_MONTH));
        builder.write("date", date);
        builder.endObject();
        
        resultPool.addValue("calendar", builder);
    }    
    
}