PublicController.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package com.goafanti.common.controller;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8. import javax.annotation.Resource;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import com.goafanti.common.utils.excel.Excel;
  12. import org.apache.commons.collections4.map.HashedMap;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.apache.poi.ss.usermodel.Cell;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.ss.usermodel.Sheet;
  17. import org.apache.poi.ss.usermodel.Workbook;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.context.annotation.Scope;
  23. import org.springframework.http.MediaType;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RequestMethod;
  27. import org.springframework.web.bind.annotation.RequestParam;
  28. import org.springframework.web.bind.annotation.ResponseBody;
  29. import org.springframework.web.multipart.MultipartFile;
  30. import org.springframework.web.socket.TextMessage;
  31. import com.goafanti.admin.service.AdminService;
  32. import com.goafanti.admin.service.AttachmentService;
  33. import com.goafanti.common.bo.Error;
  34. import com.goafanti.common.bo.OutUser;
  35. import com.goafanti.common.bo.Result;
  36. import com.goafanti.common.constant.AFTConstants;
  37. import com.goafanti.common.constant.ErrorConstants;
  38. import com.goafanti.common.enums.UserType;
  39. import com.goafanti.common.model.User;
  40. import com.goafanti.common.service.DistrictGlossoryService;
  41. import com.goafanti.common.service.FieldGlossoryService;
  42. import com.goafanti.common.service.IndustryCategoryService;
  43. import com.goafanti.common.utils.DateUtils;
  44. import com.goafanti.common.utils.LoggerUtils;
  45. import com.goafanti.common.utils.MobileMessageUtils;
  46. import com.goafanti.common.utils.PasswordUtil;
  47. import com.goafanti.common.utils.PictureUtils;
  48. import com.goafanti.common.utils.TimeUtils;
  49. import com.goafanti.common.utils.VerifyCodeUtils;
  50. import com.goafanti.common.utils.excel.FileUtils;
  51. import com.goafanti.common.utils.excel.NewExcelUtil;
  52. import com.goafanti.core.shiro.token.TokenManager;
  53. import com.goafanti.core.websocket.SystemWebSocketHandler;
  54. import com.goafanti.patent.bo.PatentNewBo;
  55. import com.goafanti.user.service.UserService;
  56. @Controller
  57. @Scope(value = "prototype")
  58. @RequestMapping("/open")
  59. public class PublicController extends CertifyApiController {
  60. @Value(value = "${accessKey}")
  61. private String accessKey = null;
  62. @Value(value = "${accessSecret}")
  63. private String accessSecret = null;
  64. @Value(value = "${upload.path}")
  65. private String uploadPath = null;
  66. @Value(value = "${upload.private.path}")
  67. private String uploadPrivatePath = null;
  68. @Value(value = "${static.host}")
  69. private String staticHost = null;
  70. @Value(value = "${mobileCodeTemplate}")
  71. private String mobileCodeTemplate = null;
  72. @Value(value = "${mobileRemindCodeTemplate}")
  73. private String mobileRemindCodeTemplate = null;
  74. @Value(value = "${wx.clockInRange}")
  75. private Integer clockInRange;
  76. @Resource
  77. private UserService userService;
  78. @Resource
  79. private AttachmentService aftFileService;
  80. @Resource
  81. private AdminService adminService;
  82. @Resource
  83. private FieldGlossoryService fieldGlossoryService;
  84. @Autowired
  85. private IndustryCategoryService industryCategoryService;
  86. @Autowired
  87. private DistrictGlossoryService districtGlossoryService;
  88. @Value(value = "${patentTemplate}")
  89. private String patentTemplate = null;
  90. @Resource(name = "passwordUtil")
  91. private PasswordUtil passwordUtil;
  92. /** 上传图片 **/
  93. @RequestMapping(value = "/upload", method = RequestMethod.POST)
  94. public Result uploadOrderInvoiceFile(HttpServletRequest req){
  95. Result res = new Result();
  96. res.setData(handleFile(res, "/publicRelease/", false, req, "publicRelease"));
  97. return res;
  98. }
  99. /**
  100. * 获取验证码
  101. *
  102. * @param response
  103. */
  104. @RequestMapping(value = "/getVCode", method = RequestMethod.GET)
  105. public void getVCode(HttpServletResponse response, HttpServletRequest request) {
  106. try {
  107. response.setHeader("Pragma", "No-cache");
  108. response.setHeader("Cache-Control", "no-cache");
  109. response.setDateHeader("Expires", 0);
  110. response.setContentType("image/jpg");
  111. // 生成随机字串
  112. String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
  113. // 存入Shiro会话session
  114. TokenManager.setVal2Session(VerifyCodeUtils.V_CODE, verifyCode.toLowerCase());
  115. // 生成图片
  116. int w = 146, h = 33;
  117. VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
  118. } catch (Exception e) {
  119. LoggerUtils.fmtError(getClass(), e, "获取验证码异常:%s", e.getMessage());
  120. }
  121. }
  122. /**
  123. * 手机验证码
  124. *
  125. * @param mobile
  126. * @param sign
  127. * 是否用与找回密码
  128. * @param type
  129. * @return
  130. */
  131. @RequestMapping(value = "/getMCode", method = RequestMethod.GET)
  132. @ResponseBody
  133. public Result getMcode(String mobile, boolean sign, Integer type, String verificationCode) {
  134. Result res = new Result();
  135. // 用于找回密码
  136. if (sign) {
  137. if (null == userService.selectByMobieAndType(mobile.trim(), type)) {
  138. res.getError().add(buildError(ErrorConstants.NON_REGISTER, "", "该用户未注册!"));
  139. return res;
  140. }
  141. }
  142. if (!sign) {
  143. if (StringUtils.isBlank(verificationCode)) {
  144. res.getError().add(buildError(ErrorConstants.VCODE_EMPTY_ERROR, "", "图像验证码不能为空!"));
  145. return res;
  146. }
  147. if (!VerifyCodeUtils.verifyCode(verificationCode)) {
  148. res.getError().add(buildError(ErrorConstants.VCODE_ERROR, "", "图形验证码输入错误!"));
  149. return res;
  150. }
  151. }
  152. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == null
  153. || TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == "") {
  154. sendMessage(mobile, res,VerifyCodeUtils.M_CODE);
  155. } else if (TimeUtils.checkOverTime("getMCode")) {
  156. VerifyCodeUtils.clearMobileCode();
  157. VerifyCodeUtils.clearMobileCodeTime();
  158. sendMessage(mobile, res,VerifyCodeUtils.M_CODE);
  159. } else {
  160. res.getError().add(buildError(ErrorConstants.MCODE_FREQUENCY_ERROR, "", "手机验证码发送频繁!"));
  161. }
  162. return res;
  163. }
  164. /**
  165. *
  166. * @param mobile
  167. * @param res
  168. * @return
  169. */
  170. private Result sendMessage(String mobile, Result res,String codeType) {
  171. String mobileVerifyCode = VerifyCodeUtils.generateMobileVerifyCode(6); // 生成随机字串
  172. if(VerifyCodeUtils.M_CODE.equals(codeType)){
  173. TokenManager.setVal2Session(VerifyCodeUtils.M_CODE, mobileVerifyCode);// 存入Shiro会话session
  174. TokenManager.setVal2Session(VerifyCodeUtils.M_CODE_TIME, System.currentTimeMillis());
  175. }else if(VerifyCodeUtils.RESET_CODE.equals(codeType)){
  176. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE, mobileVerifyCode);// 存入Shiro会话session
  177. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE_TIME, System.currentTimeMillis());
  178. }
  179. String paramString = "{\"code\":\"" + mobileVerifyCode + "\",\"product\":\"技淘网\"}";
  180. String ret = MobileMessageUtils.sendMessage(mobileCodeTemplate, paramString, mobile, accessKey, accessSecret);
  181. if (StringUtils.isNotBlank(ret)) {
  182. res.getError().add(buildError(ErrorConstants.M_CODE_ERROR));
  183. }
  184. return res;
  185. }
  186. /**
  187. * 下载图片
  188. *
  189. * @param response
  190. * @param path
  191. * @param request
  192. */
  193. @RequestMapping(value = "/downLoadPicture", method = RequestMethod.GET)
  194. public Result downLoadPicture(HttpServletResponse response, String path, HttpServletRequest request) {
  195. Result res = new Result();
  196. if (StringUtils.isBlank(path)) {
  197. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  198. return res;
  199. }
  200. String filename = Long.toString(System.nanoTime()) + ".jpg";
  201. InputStream in = null;
  202. OutputStream out = null;
  203. byte[] buffer = new byte[8 * 1024];
  204. String fileSaveRootPath = "";
  205. try {
  206. fileSaveRootPath = uploadPrivatePath + path;
  207. File file = new File(fileSaveRootPath);
  208. in = new FileInputStream(file);
  209. out = response.getOutputStream();
  210. // 设置文件MIME类型
  211. response.setContentType("application/octet-stream");
  212. response.setHeader("Content-Disposition", "attachment; filename=" + filename);
  213. for (;;) {
  214. int bytes = in.read(buffer);
  215. if (bytes == -1) {
  216. break;
  217. }
  218. out.write(buffer, 0, bytes);
  219. }
  220. } catch (IOException e) {
  221. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  222. } finally {
  223. try {
  224. if (null != in) {
  225. in.close();
  226. }
  227. } catch (IOException e) {
  228. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  229. }
  230. try {
  231. if (null != out) {
  232. out.close();
  233. }
  234. } catch (IOException e) {
  235. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  236. }
  237. }
  238. return res;
  239. }
  240. /**
  241. * 删除图片文件(public图片)
  242. *
  243. * @param path
  244. * @return
  245. */
  246. @RequestMapping(value = "/deleteUploadPicture", method = RequestMethod.POST)
  247. public boolean deleteUploadPicture(String path) {
  248. boolean flag = false;
  249. path = uploadPath + path;
  250. File file = new File(path);
  251. // 判断目录或文件是否存在
  252. if (!file.exists() || !file.isFile()) { // 不存在返回 false
  253. return flag;
  254. } else {
  255. return file.delete();
  256. }
  257. }
  258. /**
  259. * 获取并输出private图片
  260. */
  261. @RequestMapping(value = "/writeImage", method = RequestMethod.GET)
  262. public void writeImage(HttpServletResponse response, HttpServletRequest request, String path) {
  263. try {
  264. String realPath = uploadPrivatePath + path;
  265. PictureUtils.outputImage(response, realPath);
  266. } catch (IOException e) {
  267. LoggerUtils.fmtError(getClass(), e, "获取图片异常:%s", e.getMessage());
  268. }
  269. }
  270. /**
  271. * 下载文件
  272. *
  273. * @param fileName
  274. * @param response
  275. * @param path
  276. * @return
  277. */
  278. @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
  279. public Result downloadFile(String fileName, HttpServletResponse response, String path) {
  280. Result res = new Result();
  281. return handleDownloadFile(response, fileName, path, res);
  282. }
  283. /**
  284. * 下载模版文件
  285. *
  286. * @param response
  287. * @return
  288. */
  289. @RequestMapping(value = "/downloadTemplateFile", method = RequestMethod.GET)
  290. public Result downloadTemplateFile(HttpServletResponse response, String sign) {
  291. Result res = new Result();
  292. String fileName = "";
  293. String path = aftFileService.selectAftFileBySign(sign).getFilePath();
  294. String suffix = path.substring(path.lastIndexOf("."));
  295. if (sign.equals("patent_prory_statement")) {
  296. fileName = "专利代理委托书模版" + suffix;
  297. } else {
  298. fileName = System.nanoTime() + " ";
  299. }
  300. if (!StringUtils.isBlank(path)) {
  301. return handleDownloadFile(response, fileName, path, res);
  302. } else {
  303. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "下载文件不存在!"));
  304. return res;
  305. }
  306. }
  307. private Result handleDownloadFile(HttpServletResponse response, String fileName, String path, Result res) {
  308. String fileSaveRootPath = uploadPrivatePath + path;
  309. InputStream in = null;
  310. OutputStream out = null;
  311. byte[] buffer = new byte[8 * 1024];
  312. try {
  313. File file = new File(fileSaveRootPath);
  314. in = new FileInputStream(file);
  315. out = response.getOutputStream();
  316. response.setContentType("multipart/form-data");
  317. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  318. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  319. for (;;) {
  320. int bytes = in.read(buffer);
  321. if (bytes == -1) {
  322. break;
  323. }
  324. out.write(buffer, 0, bytes);
  325. }
  326. } catch (IOException e) {
  327. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  328. } finally {
  329. try {
  330. in.close();
  331. } catch (IOException e) {
  332. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  333. }
  334. try {
  335. out.close();
  336. } catch (IOException e) {
  337. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  338. }
  339. }
  340. return res;
  341. }
  342. @RequestMapping(value = "/findIndustryCategory", method = RequestMethod.GET)
  343. @ResponseBody
  344. public Result findIndustryCategory(String id, String noCache) {
  345. Integer pid = 0;
  346. if (StringUtils.isNumeric(id)) {
  347. try {
  348. pid = Integer.parseInt(id);
  349. } catch (Exception e) {
  350. pid = 0;
  351. }
  352. }
  353. if (StringUtils.isNotBlank(noCache)) {
  354. industryCategoryService.clear(pid);
  355. }
  356. return new Result().data(industryCategoryService.list(pid));
  357. }
  358. @RequestMapping(value = "/findDistrict", method = RequestMethod.GET)
  359. @ResponseBody
  360. public Result findDistrictGlossory(String id, String noCache) {
  361. Integer pid = 0;
  362. if (StringUtils.isNumeric(id)) {
  363. try {
  364. pid = Integer.parseInt(id);
  365. } catch (Exception e) {
  366. pid = 0;
  367. }
  368. }
  369. if (StringUtils.isNotBlank(noCache)) {
  370. districtGlossoryService.clear(pid);
  371. }
  372. return new Result().data(districtGlossoryService.list(pid));
  373. }
  374. /**
  375. *
  376. * @param mobileCode
  377. * @return
  378. */
  379. @RequestMapping(value = "/checkMCode", method = RequestMethod.POST)
  380. @ResponseBody
  381. public Result checkMCode(String mobileCode) {
  382. Result res = new Result();
  383. if (StringUtils.isBlank(mobileCode)) {
  384. res.getError().add(buildError(ErrorConstants.MCODE_EMPTY_ERROR, "", "手机验证码"));
  385. return res;
  386. }
  387. if (TimeUtils.checkOverTime(VerifyCodeUtils.M_CODE)) {
  388. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "", "手机验证码超时失效!"));
  389. VerifyCodeUtils.clearMobileCode();
  390. VerifyCodeUtils.clearMobileCodeTime();
  391. return res;
  392. }
  393. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode)) {
  394. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "", "手机验证码错误"));
  395. return res;
  396. }
  397. // resetCode
  398. if (res.getError().isEmpty()) {
  399. String resetCode = UUID.randomUUID().toString();
  400. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE, resetCode);
  401. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE_TIME, System.currentTimeMillis());
  402. res.setData(resetCode);
  403. }
  404. return res;
  405. }
  406. /**
  407. * 重置密码
  408. *
  409. * @param resetCode
  410. * @param mobile
  411. * @param type
  412. * @param newPwd
  413. * @return
  414. */
  415. @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
  416. @ResponseBody
  417. public Result resetPwd(String resetCode, String mobile, Integer type, String newPwd) {
  418. Result res = new Result();
  419. if (TimeUtils.checkOverTime(VerifyCodeUtils.M_CODE)) {
  420. res.getError().add(buildError(ErrorConstants.RESET_CODE_OVERTIME, "", "页面超时失效,请重新获取验证码!"));
  421. cleanCodeSession();
  422. return res;
  423. }
  424. if(null == TokenManager.getSession().getAttribute(VerifyCodeUtils.RESET_CODE)){
  425. res.getError().add(buildError(ErrorConstants.RESET_CODE_OVERTIME, "", "页面超时失效,请重新获取验证码!"));
  426. cleanCodeSession();
  427. return res;
  428. }else if(!TokenManager.getSession().getAttribute(VerifyCodeUtils.RESET_CODE).equals(resetCode)){
  429. res.getError().add(buildError(ErrorConstants.RESET_CODE_ERROR, "", "验证码错误,请重新获取验证码!"));
  430. cleanCodeSession();
  431. return res;
  432. }
  433. if (StringUtils.isBlank(newPwd)) {
  434. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新密码"));
  435. return res;
  436. }
  437. if (StringUtils.isBlank(mobile)) {
  438. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "mobile"));
  439. return res;
  440. }
  441. if (!UserType.ORGANIZATION.getCode().equals(type) && !UserType.PERSONAL.getCode().equals(type)) {
  442. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "type"));
  443. return res;
  444. }
  445. User user = userService.selectByMobieAndType(mobile, type);
  446. if (null == user) {
  447. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "mobile"));
  448. return res;
  449. }
  450. user.setPassword(newPwd);
  451. user.setPassword(passwordUtil.getEncryptPwd(user));
  452. userService.updateByPrimaryKeySelective(user);
  453. VerifyCodeUtils.clearResetCode();
  454. VerifyCodeUtils.clearResetCodeTime();
  455. return res;
  456. }
  457. private void cleanCodeSession() {
  458. VerifyCodeUtils.clearResetCode();
  459. VerifyCodeUtils.clearResetCodeTime();
  460. VerifyCodeUtils.clearMobileCode();
  461. VerifyCodeUtils.clearMobileCodeTime();
  462. }
  463. @RequestMapping(value = "/getRegisterMCode", method = RequestMethod.GET )
  464. @ResponseBody
  465. public Result getRegisterMCode(String mobile){
  466. Result res = new Result();
  467. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == null
  468. || TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == "") {
  469. sendMessage(mobile, res,VerifyCodeUtils.M_CODE);
  470. } else if (TimeUtils.checkOverTime("getMCode")) {
  471. VerifyCodeUtils.clearMobileCode();
  472. VerifyCodeUtils.clearMobileCodeTime();
  473. sendMessage(mobile, res,VerifyCodeUtils.M_CODE);
  474. } else {
  475. res.getError().add(buildError(ErrorConstants.MCODE_FREQUENCY_ERROR, "", "手机验证码发送频繁!"));
  476. }
  477. return res;
  478. }
  479. @RequestMapping(value = "/getResetMCode", method = RequestMethod.GET )
  480. @ResponseBody
  481. public Result getResetMCode(String mobile, Integer type) {
  482. Result res = new Result();
  483. if (null == userService.selectByMobieAndType(mobile.trim(), type)) {
  484. res.getError().add(buildError(ErrorConstants.NON_REGISTER, "", "该用户未注册!"));
  485. return res;
  486. }
  487. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.RESET_CODE_TIME) == null
  488. || TokenManager.getSession().getAttribute(VerifyCodeUtils.RESET_CODE_TIME) == "") {
  489. sendMessage(mobile, res,VerifyCodeUtils.RESET_CODE);
  490. } else if (TimeUtils.checkOverTime("resetCode")) {
  491. VerifyCodeUtils.clearMobileCode();
  492. VerifyCodeUtils.clearMobileCodeTime();
  493. sendMessage(mobile, res,VerifyCodeUtils.RESET_CODE);
  494. } else {
  495. res.getError().add(buildError(ErrorConstants.MCODE_FREQUENCY_ERROR, "", "手机验证码发送频繁!"));
  496. }
  497. return res;
  498. }
  499. /** 省市区查询 **/
  500. @RequestMapping(value = "/listAllProvince" , method = RequestMethod.POST)
  501. @ResponseBody
  502. public Result listAllProvince(){
  503. Result res = new Result();
  504. res.setData(districtGlossoryService.getProvince());
  505. return res;
  506. }
  507. @RequestMapping(value = "/listDefaultPassword" , method = RequestMethod.GET)
  508. @ResponseBody
  509. public Result listDefaultPassword(String depId){
  510. Result res=new Result();
  511. res.setData(adminService.getListDefaultPassword(depId));
  512. return res;
  513. }
  514. /** 省市区查询 **/
  515. @RequestMapping(value = "/sendWeb" , method = RequestMethod.POST)
  516. @ResponseBody
  517. public Result sendWeb(String str){
  518. Result res = new Result();
  519. SystemWebSocketHandler sys=new SystemWebSocketHandler();
  520. TextMessage text=new TextMessage(str.getBytes());
  521. sys.sendMessageToUsers(text);
  522. res.setData(1);
  523. return res;
  524. }
  525. /**
  526. * 导入流水
  527. * @param file
  528. * @param request
  529. * @return
  530. */
  531. @SuppressWarnings("unchecked")
  532. @RequestMapping(value = "/import" , method = RequestMethod.POST)
  533. @ResponseBody
  534. public Result importTeacher(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) {
  535. Result res=new Result();
  536. String fileName = file.getOriginalFilename();
  537. if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
  538. res.error(new Error("", "格式不正确"));
  539. }
  540. //检查文件
  541. try {
  542. checkFile(file);
  543. } catch (IOException e) {
  544. e.printStackTrace();
  545. }
  546. //获得Workbook工作薄对象
  547. Workbook workbook = getWorkBook(file);
  548. //创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回
  549. List<List<String>> list = new ArrayList<>();
  550. if(workbook != null){
  551. for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){
  552. //获得当前sheet工作表
  553. Sheet sheet = workbook.getSheetAt(sheetNum);
  554. if(sheet == null){
  555. continue;
  556. }
  557. //获得当前sheet的开始行
  558. int firstRowNum = sheet.getFirstRowNum();
  559. //获得当前sheet的结束行
  560. int lastRowNum = sheet.getLastRowNum();
  561. //循环除了第2行的所有行
  562. for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
  563. //获得当前行
  564. Row row = sheet.getRow(rowNum);
  565. if(row == null){
  566. continue;
  567. }
  568. //获得当前行的开始列
  569. int firstCellNum = row.getFirstCellNum();
  570. //获得当前行的列数
  571. int lastCellNum = row.getLastCellNum();
  572. @SuppressWarnings("rawtypes")
  573. ArrayList arrayList = new ArrayList();
  574. //循环当前行 cellNum = (firstCellNum+1) 则是除开第一例
  575. for(int cellNum = (firstCellNum); cellNum < lastCellNum;cellNum++){
  576. Cell cell = row.getCell(cellNum);
  577. arrayList.add(getCellValue(cell));
  578. }
  579. list.add(arrayList);
  580. }
  581. }
  582. }
  583. return res;
  584. }
  585. public static void checkFile(MultipartFile file) throws IOException{
  586. //判断文件是否存在
  587. if(null == file){
  588. throw new FileNotFoundException("文件不存在!");
  589. }
  590. //获得文件名
  591. String fileName = file.getOriginalFilename();
  592. //判断文件是否是excel文件
  593. if(!fileName.endsWith("xls") && !fileName.endsWith("xlsx")){
  594. throw new IOException(fileName + "不是excel文件");
  595. }
  596. }
  597. public static Workbook getWorkBook(MultipartFile file) {
  598. //创建Workbook工作薄对象,表示整个excel
  599. Workbook workbook = null;
  600. try {
  601. //获取excel文件的io流
  602. InputStream is = file.getInputStream();
  603. //如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook
  604. workbook = new XSSFWorkbook(is);
  605. } catch (IOException e) {
  606. }
  607. return workbook;
  608. }
  609. @SuppressWarnings("deprecation")
  610. public static String getCellValue(Cell cell){
  611. String cellValue = "";
  612. if(cell == null){
  613. return cellValue;
  614. }
  615. //把数字当成String来读,避免出现1读成1.0的情况
  616. if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
  617. cell.setCellType(Cell.CELL_TYPE_STRING);
  618. }
  619. //判断数据的类型
  620. switch (cell.getCellType()){
  621. case Cell.CELL_TYPE_NUMERIC: //数字
  622. cellValue = String.valueOf(cell.getNumericCellValue());
  623. break;
  624. case Cell.CELL_TYPE_STRING: //字符串
  625. cellValue = String.valueOf(cell.getStringCellValue());
  626. break;
  627. case Cell.CELL_TYPE_BOOLEAN: //Boolean
  628. cellValue = String.valueOf(cell.getBooleanCellValue());
  629. break;
  630. case Cell.CELL_TYPE_FORMULA: //公式
  631. cellValue = String.valueOf(cell.getCellFormula());
  632. break;
  633. case Cell.CELL_TYPE_BLANK: //空值
  634. cellValue = "未知";
  635. break;
  636. case Cell.CELL_TYPE_ERROR: //故障
  637. cellValue = "非法字符";
  638. break;
  639. default:
  640. cellValue = "未知类型";
  641. break;
  642. }
  643. return cellValue;
  644. }
  645. /**
  646. * 通用下载请求
  647. *
  648. * @param fileName 文件名称
  649. * @param delete 是否删除
  650. */
  651. @RequestMapping("/download")
  652. public void fileDownload(String fileName, Boolean delete,String attName,
  653. HttpServletResponse response, HttpServletRequest request)
  654. {
  655. try
  656. {
  657. if (!FileUtils.checkAllowDownload(fileName))
  658. {
  659. throw new Exception("文件名称非法,不允许下载。 ");
  660. }
  661. String realFileName =DateUtils.parseDateToStr(AFTConstants.YYYYMMDDHHMMSS, new Date())+fileName.substring(fileName.indexOf("_") + 1);
  662. String filePath = uploadPath +"/"+ fileName;
  663. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  664. FileUtils.setAttachmentResponseHeader(response, realFileName,attName);
  665. FileUtils.writeBytes(filePath, response.getOutputStream());
  666. if(delete==null)delete=true;
  667. if (delete)
  668. {
  669. FileUtils.deleteFile(filePath);
  670. }
  671. }
  672. catch (Exception e)
  673. {
  674. LoggerUtils.error(PublicController.class,"下载文件失败", e);
  675. }
  676. }
  677. @RequestMapping("/getWxConfig")
  678. public Result getWxConfig() {
  679. Result res =new Result();
  680. Map<String, Object> config=new HashedMap<String, Object>();
  681. config.put("clockInRange", clockInRange);
  682. res.data(config);
  683. return res;
  684. }
  685. @RequestMapping("/getUser")
  686. public Result getUser() {
  687. List<OutUser> list=userService.getuser();
  688. NewExcelUtil<OutUser> excel=new NewExcelUtil<OutUser>(OutUser.class);
  689. return excel.exportExcel(list, "客户列表",uploadPath);
  690. }
  691. }