BaseController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.goafanti.common.controller;
  2. import java.text.DateFormat;
  3. import java.text.MessageFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import javax.annotation.Resource;
  7. import javax.servlet.http.HttpServletRequest;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.shiro.SecurityUtils;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.beans.propertyeditors.CustomDateEditor;
  12. import org.springframework.core.io.support.ResourcePropertySource;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.ui.Model;
  15. import org.springframework.web.bind.WebDataBinder;
  16. import org.springframework.web.bind.annotation.InitBinder;
  17. import org.springframework.web.bind.annotation.ModelAttribute;
  18. import com.goafanti.common.bo.Error;
  19. import com.goafanti.common.constant.AFTConstants;
  20. import com.goafanti.common.model.Admin;
  21. import com.goafanti.common.model.User;
  22. import com.goafanti.core.shiro.token.TokenManager;
  23. @Controller
  24. public class BaseController {
  25. @Resource(name = "errorResource")
  26. private ResourcePropertySource errorResource;
  27. @Value(value = "${static.host}")
  28. private String staticHost = null;
  29. @Value(value = "${avatar.host}")
  30. private String avatarHost = null;
  31. @Value(value = "${upload.path}")
  32. private String uploadPath = null;
  33. @Value(value = "${upload.private.path}")
  34. private String uploadPrivatePath = null;
  35. @Value(value = "${accessKey}")
  36. private String accessKey = null;
  37. @Value(value = "${accessSecret}")
  38. private String accessSecret = null;
  39. @Value(value = "${portal.host}")
  40. private String portalHost = null;
  41. @Value(value = "${avatar.upload.host}")
  42. private String avatarUploadHost = null;
  43. protected Error buildError(String key) {
  44. return buildError(key, null);
  45. }
  46. protected Error buildError(String key, String message) {
  47. String msg = (String) errorResource.getProperty(key);
  48. if (StringUtils.isEmpty(msg)) {
  49. msg = message;
  50. }
  51. if (StringUtils.isEmpty(msg)) {
  52. msg = key;
  53. }
  54. return new Error(key, msg);
  55. }
  56. /**
  57. * 带参数的error message
  58. *
  59. * @param key
  60. * @param message
  61. * @param object
  62. * @return
  63. */
  64. protected Error buildError(String key, String message, Object... object) {
  65. String msg = (String) errorResource.getProperty(key);
  66. if (StringUtils.isEmpty(msg)) {
  67. msg = message;
  68. }
  69. msg = MessageFormat.format(msg, object);
  70. return new Error(key, msg);
  71. }
  72. protected Error buildErrorByMsg(String msg, Object... params) {
  73. return new Error(MessageFormat.format(msg, params));
  74. }
  75. protected String getErrorMessage(String key) {
  76. Object msg = errorResource.getProperty(key);
  77. if (msg == null) {
  78. return key;
  79. }
  80. return (String) msg;
  81. }
  82. protected String getErrorMessage(String key, Object... obj) {
  83. return MessageFormat.format(getErrorMessage(key), obj);
  84. }
  85. @InitBinder
  86. public void initBinder(WebDataBinder binder) {
  87. DateFormat dateFormat = new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS);
  88. dateFormat.setLenient(true);
  89. binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  90. }
  91. @ModelAttribute
  92. public void prepareModel(HttpServletRequest request, Model model) {
  93. model.addAttribute("staticDomain", avatarHost);
  94. model.addAttribute("staticHost", staticHost);
  95. model.addAttribute("avatarHost", avatarHost);
  96. model.addAttribute("avatarUploadHost", avatarUploadHost);
  97. model.addAttribute("portalHost", portalHost);
  98. model.addAttribute("basePath", request.getContextPath());
  99. model.addAttribute("shiro", SecurityUtils.getSubject());
  100. model.addAttribute("isLogin", TokenManager.isLogin());
  101. model.addAttribute("csrf", TokenManager.getCSRF());
  102. model.addAttribute("isAdmin", false);
  103. if (TokenManager.isLogin() && TokenManager.getToken() instanceof User) {
  104. model.addAttribute("userData", TokenManager.getUserToken());
  105. model.addAttribute("userName", TokenManager.getUserToken().getNickname());
  106. }
  107. if (TokenManager.isLogin() && TokenManager.getToken() instanceof Admin) {
  108. model.addAttribute("adminData", TokenManager.getAdminToken());
  109. model.addAttribute("userName", TokenManager.getAdminToken().getName());
  110. model.addAttribute("departmentId", TokenManager.getAdminToken().getDepartmentId());
  111. model.addAttribute("isAdmin", true);
  112. }
  113. }
  114. }