xinyb
2022-07-06 a4a24e74cc85db75990874e3e12459c454cb137f
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package com.yc.userCenter.action;
 
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.gson.GsonBuilder;
import com.yc.action.BaseAction;
import com.yc.action.grid.GridUtils;
import com.yc.entity.UserAccountEntity;
import com.yc.entity.attachment.AttachmentEntity;
import com.yc.entity.attachment.AttachmentWhereEntity;
import com.yc.exception.CallBackMessage;
import com.yc.factory.FactoryBean;
import com.yc.multiData.SpObserver;
import com.yc.service.impl.DBHelper;
import com.yc.service.upload.AttachmentIfc;
import com.yc.service.user.UserAccountServiceIfc;
import com.yc.userCenter.entity.JurisdictionEntiy;
import com.yc.userCenter.entity.UserEntity;
import com.yc.userCenter.service.*;
import com.yc.utils.EncodeUtil;
import com.yc.utils.SessionKey;
 
/**
 * 新权限设置界面
 * 
 * @author xinyb_
 *
 */
@Controller
@RequestMapping("/userCenter.do")
public class UserCenterAction extends BaseAction {
    @Autowired
    UserCenterIfc UserCenterIfc;
    @Autowired
    AttachmentIfc AttachmentIfc;
    @Autowired
    UserAccountServiceIfc userAccountService;
 
    /**
     * 加载所有用户信息
     * @param value
     * @param page
     * @param limit
     * @param allow
     * @param appAllow 是否已经注册APP
     * @param request
     * @param response
     */
    @RequestMapping(params = "m=user")
    public void userCenter(String value, int page, int limit,String allow ,String appAllow,HttpServletRequest request,
            HttpServletResponse response) {
        List<Map<String, Object>> result = null;
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            String dbId = (String) request.getSession().getAttribute(SessionKey.DATA_BASE_ID);
            String alist="";
            value = StringUtils.isNotBlank(value) ? value : "";
            if (StringUtils.isBlank(appAllow)) {
                appAllow = "1";
            }
            List<String> applist = null;
            if (!"1".equals(appAllow)) {//查询APP的
                try {
                    SpObserver.setDBtoDemo();
                    applist = UserCenterIfc.selectApps(dbId);
                    if (applist != null && applist.size() > 0) {
                        alist = String.join(",", applist);
                    }
                } finally {
                    SpObserver.setDBtoInstance();
                }
            }
            try {
                SpObserver.setDBtoInstance("_" + dbId);
                allow = StringUtils.isNotBlank(allow) ? allow : "";
                result = UserCenterIfc.getUserCenter(value, page, limit, allow, appAllow,alist);
            } finally {
                SpObserver.setDBtoInstance();
            }
            Integer t = 0;
            if (result != null && result.get(0) != null) {
                t = DBHelper.getValueInt(result.get(0), "total");
            }
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", t + (limit * (page - 1)));
            map.put("data", result);
            this.printJson(response, GridUtils.toJson(map));
        } catch (Exception e) {
            this.printJson(response, "{\"error\":\"" + this.getErrorMsg(e) + "\"}");
        }
    }
 
    /**
     * 删除维护登录用户
     * 
     * @param usercode
     * @param request
     * @param response
     */
    @RequestMapping(params = "m=deluser")
    public void delUser(String usercode, Integer logontype,HttpServletRequest request, HttpServletResponse response) {
        try {
            String dbId = (String) request.getSession().getAttribute(SessionKey.DATA_BASE_ID);
            SpObserver.setDBtoInstance("_" + dbId);
            int cont = UserCenterIfc.delUser(usercode,logontype);
            this.printJson(response, "{\"info\":\"" + cont + "\"}");            
        } catch (Exception e) {
            this.printJson(response, "{\"error\":\"" + this.getErrorMsg(e) + "\"}");
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    /**
     * 修改密码
     * 
     * @param mobileos
     * @param request
     * @param response
     */
    @RequestMapping(params = "m=usercPwd")
    public void updateuserPwdCenter(String oldpwd, String newpwd, Boolean isCode, String usercode,
            Integer logontype,HttpServletRequest request, HttpServletResponse response) {
        try {
            String dbId = (String) request.getSession().getAttribute(SessionKey.DATA_BASE_ID);
            SpObserver.setDBtoInstance("_" + dbId);
            if (!isCode) {// 是false的话需要验证旧密码,true为找回密码已经手机验证后才能找回(修改)
                oldpwd = EncodeUtil.replaceUrlChar(oldpwd);
                UserAccountEntity userAccount = userAccountService.getUserInfoByUserCode(usercode);
                if (!userAccount.checkPassword(oldpwd)) {
                    this.printJson(response, "{\"msg\":\"旧密码错误,请检查是否输入正确\",\"state\":-1}");
                    return;
                }
            }
            if ("".equals(newpwd)) {
                this.printJson(response, "{\"msg\":\"新密码不能为空\",\"state\":-1}");
                return;
            }
            Map<String, Object> map = UserCenterIfc.getUpdateUserPwd(newpwd, usercode,logontype);
            this.printJson(response, new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").disableHtmlEscaping()
                    .serializeNulls().create().toJson(map));
        } catch (Exception e) {
            this.printJson(response, "{\"msg\":\"" + this.getErrorMsg(e) + "\"}");
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    /**
     * 保存对应控件值到数据库
     * 
     * @param value
     * @param usercode
     * @param request
     * @param response
     */
    @RequestMapping(params = "m=Info")
    public void setUserCenterInfo(String value, String usercode,Integer logontype, HttpServletRequest request,
            HttpServletResponse response) {
        CallBackMessage callBackMessage = new CallBackMessage();
        try {
            SpObserver.setDBtoInstance("_" + request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            int cont = UserCenterIfc.getUserCenterInfo(value, usercode,logontype);
            if (cont == 1) {
                this.printJson(response, callBackMessage.sendSuccessMessage("保存成功!"));
            }
        } catch (Exception e) {//
            this.printJson(response, callBackMessage
                    .sendErrorMessage(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    /**
     * 保存头像图片
     * 
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping(params = "m=Image")
    public void setUserCenterCobox(String id, HttpServletRequest request, HttpServletResponse response) {
        try {
            String uploadPath = System.getProperty("java.io.tmpdir");
            SpObserver.setDBtoInstance("_" + request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            File tempPathFile = null;
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // Set factory constraints
            factory.setSizeThreshold(40960); // 设置缓冲区大小,这里是4kb
            factory.setRepository(tempPathFile);// 设置缓冲区目录
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // Set overall request size constraint
            upload.setSizeMax(419430400); // 设置最大文件尺寸,这里是4MB
 
            @SuppressWarnings("unchecked")
            List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
            Iterator<FileItem> i = items.iterator();
            Map<String, String> param = new HashMap<String, String>(); // 所有填写的文本字段
//            List<File> fileList = new ArrayList<File>(); // 所有上传的文件
            File files = null;
            while (i.hasNext()) { 
                FileItem fi = (FileItem) i.next();
                String fileName = fi.getName();
                if (fileName != null) {
                    File fullFile = new File(new String(fi.getName().getBytes(), "utf-8")); // 解决文件名乱码问题
                    File savedFile = new File(uploadPath, fullFile.getName() + ".JPG");
                    fi.write(savedFile);
//                    fileList.add(savedFile);
                    files = savedFile;
                } else {
                    param.put(fi.getFieldName(), fi.getString("utf-8"));// 如果你页面编码是utf-8的
                }
            }
            AttachmentEntity attachmentEntity = new AttachmentEntity();
            attachmentEntity.setAuthorCode(param.get("usercode"));
            attachmentEntity.setAuthorName(param.get("username"));
            if (files != null) {
                attachmentEntity.setOriginalFileName(files.getName());
                // 上传图片
                AttachmentWhereEntity attachmentWhereEntity = AttachmentIfc.saveAttachment(attachmentEntity, files,
                        "0");
                if (!attachmentWhereEntity.getUnid().equals("")) {
                    int cont = UserCenterIfc.getUserCenterInfo("avatarunid='" + attachmentWhereEntity.getUnid() + ";"
                            + attachmentWhereEntity.getSeq() + "'", param.get("usercode"),0);
                    if (cont == 1) {
                        this.printJson(response, "{\"info\":\"/getImage.do?uuid=" + attachmentWhereEntity.getUnid()
                                + ";" + attachmentWhereEntity.getSeq() + "&type=1\"}");
                    }
                }
            }
        } catch (Exception e) {
            this.printJson(response, e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    /**
     * 手机或Email绑定后保存到后台数据库
     * 
     * @param dbId
     * @param v
     * @param code
     * @param usercode
     * @param request
     * @param response
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(params = "m=phoneEmail")
    public void phoneEmail(String dbId, String v, String code, String usercode,String userId,Integer logontype, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            @SuppressWarnings("rawtypes")
            RedisTemplate redisTemplate = (RedisTemplate) FactoryBean.getBean("redisTemplate");
            if (!redisTemplate.hasKey("CODE:" + v + ":" + dbId)) {
                this.printJson(response, "{\"info\":\"验证码已失效,请重新获取验证码。\"}");
                return;
            }
            String rediscode = redisTemplate.opsForValue().get("CODE:" + v + ":" + dbId) + "";
            if (!code.equals(rediscode)) {// 验证码匹配
                this.printJson(response, "{\"info\":\"验证码错误,请输入正确的验证码。\"}");
                return;
            }
            if (usercode == null || usercode.equals("")) {
                this.printJson(response, "{\"info\":\"获取不到当前用户userCode。\"}");
                return;
            }
            String value = v;
            Pattern regex = Pattern
                    .compile("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
            Matcher matcher = regex.matcher(v);
            Pattern regex1 = Pattern.compile(
                    "^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$");
            Matcher matcher1 = regex1.matcher(v);
            if (matcher.matches()) {// 邮箱
                value = "email='" + v + "'";
            } else if (matcher1.matches()) {// 手机
                value = "tel='" + v + "'";
            } else {
                this.printJson(response, "{\"info\":\"格式不正确!\"}");
                return;
            }
            int cont = 0;
            try {
                SpObserver.setDBtoInstance("_" + request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
                cont = UserCenterIfc.getUserCenterInfo(value, usercode,logontype);
                if (cont == 1 && matcher1.matches()) {
                    redisTemplate.delete("CODE:" + v + ":" + dbId);// 删除
                    UserCenterIfc.updateTelephone(v, usercode, "");
                }
            } finally {
                SpObserver.setDBtoInstance();
            }
            if (cont == 1 && matcher1.matches() && userId != null && !"".equals(userId)) {// 修改的是手机进入
                try {
                    SpObserver.setDBtoDemo();
                    UserCenterIfc.updateTelephone(v, "", userId);
                } finally {
                    SpObserver.setDBtoInstance();
                }
            }
            this.printJson(response, "{\"info\":\"" + cont + "\"}");
        } catch (Exception e) {
            this.printJson(response, "{\"info\":\"验证过程出现错误:"
                    + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()) + "\"}");
        }
    }
 
    @RequestMapping(params = "m=mess")
    public void getMessage(String key, String search, HttpServletRequest request, HttpServletResponse response) {
        try {
            SpObserver.setDBtoDemo();
            List<Map<String, Object>> list = UserCenterIfc.getMessage(key, search);
            this.printJson(response, GridUtils.toJson(list));
        } catch (Exception e) {
            this.printJson(response, (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    @RequestMapping(params = "m=delmess")
    public void delMessage(String id, HttpServletRequest request, HttpServletResponse response) {
        try {
            SpObserver.setDBtoDemo();
            int cont = UserCenterIfc.delMessage(id);
            this.printJson(response, "{\"info\":" + cont + "}");
        } catch (Exception e) {
            this.printJson(response, (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    @RequestMapping(params = "m=getSelect")
    public void getCccode(String codeId, String userCode, String companyId,HttpServletRequest request, HttpServletResponse response) {
        try {
            SpObserver.setDBtoInstance("_" + request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            Map<String, Object> map = UserCenterIfc.getselect2SQL(codeId, userCode); // 获取需要的下拉sql
            Map<String, Object> userMap = new HashMap<String, Object>();
            if(companyId!=null) {
                userMap.put("companyid", companyId);
            }else {
                userMap.put("companyid", request.getSession().getAttribute(SessionKey.COMPANY_ID));
            }            
            String sql = DBHelper.getValRepShi(DBHelper.getValue(map, "sqlscript"), request.getSession(), userMap,
                    false);
            SqlRowSet srs = UserCenterIfc.getSelect(sql);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            while (srs != null && srs.next()) {
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("name", DBHelper.replaceBlank(srs.getString(2)));
                map2.put("value", DBHelper.replaceBlank(srs.getString(1)).toLowerCase().trim());
                list.add(map2);
            }
            //选中的值
            String[] val = DBHelper.getValue(map, "val").toLowerCase().trim().split(";");
            List<String> vList=new ArrayList<String>();
            for (int i = 0; i < val.length; i++) {
                vList.add(val[i]);
            }
            this.printJson(response,"{\"value\":"+GridUtils.toJson(vList)+",\"data\":"+GridUtils.toJson(list)+"}");
        } catch (Exception e) {
            this.printJson(response, (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    @RequestMapping(params = "m=addUser")
    public void adUser(UserEntity user, HttpServletRequest request, HttpServletResponse response) {
        try {
            SpObserver.setDBtoInstance("_" + request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            if (user != null) {
                user.setCompanyId((String) request.getSession().getAttribute(SessionKey.COMPANY_ID));// 获取公司Id
                user.setCompanyName((String) request.getSession().getAttribute(SessionKey.COMPANY_NAME));// 获取公司名称
                int count = UserCenterIfc.addUser(user);
                this.printJson(response, "{\"info\":" + count + "}");
            }
        } catch (Exception e) {
            this.printJson(response, (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    @RequestMapping(params = "m=updateApp")
    public void updateApp(String value, String userId, String dbId, HttpServletRequest request,
            HttpServletResponse response) {
        CallBackMessage callBackMessage = new CallBackMessage();
        try {
            SpObserver.setDBtoDemo();
            int cont = UserCenterIfc.updateApp(value, userId, dbId);
            if (cont == 1) {
                this.printJson(response, callBackMessage.sendSuccessMessage("修改成功!"));
            } else {
                this.printJson(response, callBackMessage.sendErrorMessage("修改失败!"));
            }
        } catch (Exception e) {
            this.printJson(response, callBackMessage
                    .sendErrorMessage(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    @RequestMapping(params = "m=delApp")
    public void delApp(String userId, String dbId, HttpServletRequest request, HttpServletResponse response) {
        CallBackMessage callBackMessage = new CallBackMessage();
        try {
            SpObserver.setDBtoDemo();
            int cont = UserCenterIfc.deleteApp(userId, dbId);
            if (cont == 1) {
                this.printJson(response, callBackMessage.sendSuccessMessage("删除成功!"));
            } else {
                this.printJson(response, callBackMessage.sendErrorMessage("删除失败!"));
            }
        } catch (Exception e) {
            this.printJson(response, callBackMessage
                    .sendErrorMessage(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
 
    @RequestMapping(params = "m=setting")
    public void setTing(JurisdictionEntiy jurisdiction, HttpServletRequest request, HttpServletResponse response) {
        CallBackMessage callBackMessage = new CallBackMessage();
        try {
            SpObserver.setDBtoInstance("_" + request.getSession().getAttribute(SessionKey.DATA_BASE_ID));
            int cont = UserCenterIfc.updateSetTing(jurisdiction);
            if (cont == 1) {
                this.printJson(response, callBackMessage.sendSuccessMessage("保存成功!"));
            } else {
                this.printJson(response, callBackMessage.sendErrorMessage("保存失败!"));
            }
        } catch (Exception e) {
            this.printJson(response, callBackMessage
                    .sendErrorMessage(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
        } finally {
            SpObserver.setDBtoInstance();
        }
    }
}