IndexController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package com.sooka.module.web.cms;
  2. import com.github.pagehelper.PageInfo;
  3. import com.google.common.collect.Maps;
  4. import com.sooka.ServletInitializer;
  5. import com.sooka.common.constant.CmsConst;
  6. import com.sooka.common.exception.CmsException;
  7. import com.sooka.common.utils.*;
  8. import com.sooka.component.beetl.thread.HtmlThread;
  9. import com.sooka.component.lucene.util.IndexObject;
  10. import com.sooka.module.web.cms.service.*;
  11. import com.sooka.module.web.cms.vo.TCmsContentVo;
  12. import com.sooka.module.web.system.service.AttachmentService;
  13. import com.sooka.mybatis.model.*;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.servlet.ModelAndView;
  22. import tk.mybatis.mapper.util.StringUtil;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.io.*;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. import static com.sooka.common.constant.CmsConst.LOGIN_USER;
  28. /**
  29. * Description:前台首页
  30. *
  31. *
  32. * @create 2017-04-13
  33. **/
  34. @Controller
  35. @RequestMapping("/")
  36. public class IndexController {
  37. private final static Logger log = LoggerFactory.getLogger(IndexController.class);
  38. @Autowired
  39. private SiteService siteService;
  40. @Autowired
  41. private CategoryService categoryService;
  42. @Autowired
  43. private ContentService contentService;
  44. @Autowired
  45. private ModelService modelService;
  46. @Autowired
  47. private ModelFiledService modelFiledService;
  48. @Autowired
  49. private LuceneService luceneService;
  50. @Autowired
  51. private TopicService topicService;
  52. @Autowired
  53. private AdService adService;
  54. @Autowired
  55. private AttachmentService attachmentService;
  56. @Value("${system.site.name}")
  57. private String siteName;
  58. @Value("${system.http.host}")
  59. private String host;
  60. @Value("${server.port}")
  61. private String port;
  62. @Value("${system.http.protocol}")
  63. private String httpProtocol;
  64. @Value("${system.site.prefix}")
  65. private String sitePrefix;
  66. @Value("${system.site.subfix}")
  67. private String siteSubfix;
  68. @Value("${system.site.static}")
  69. private String siteStatic;
  70. @Value("${system.site.page.size}")
  71. private String pageSize;
  72. /*网站首页*/
  73. @GetMapping
  74. public ModelAndView index(@RequestParam(value = "keyword",required = false) String keyword, HttpServletRequest request){
  75. String domain = ControllerUtil.getDomain();
  76. log.debug("通过域名访问网站首页[{}]",domain);
  77. TCmsSite site = siteService.findByDomain(domain);
  78. if(CmsUtil.isNullOrEmpty(site)) {
  79. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  80. }
  81. if(!StrUtil.isBlank(keyword)) {
  82. return new ModelAndView( "forward:/"+sitePrefix+"/search");
  83. }
  84. ModelAndView modelAndView = new ModelAndView( "forward:/"+sitePrefix+"/"+site.getSiteId());
  85. if (request.getSession().getAttribute(LOGIN_USER) == null) {
  86. modelAndView.addObject("btnId", "login");
  87. } else {
  88. modelAndView.addObject("btnId", "exit");
  89. }
  90. return modelAndView;
  91. }
  92. @GetMapping("/login")
  93. public ModelAndView login(){
  94. ModelAndView modelAndView=new ModelAndView("www/tuiyijunren/login");
  95. modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
  96. return modelAndView;
  97. }
  98. @GetMapping("/exit")
  99. public ModelAndView exit(HttpServletRequest request){
  100. request.getSession().removeAttribute(LOGIN_USER);
  101. return index(null, request);
  102. }
  103. @GetMapping("/register")
  104. public ModelAndView register(){
  105. ModelAndView modelAndView=new ModelAndView("www/tuiyijunren/register");
  106. modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
  107. return modelAndView;
  108. }
  109. @RequestMapping("/queryDescription")
  110. public ModelAndView QueryTitleDescription (String keyword, String siteId, @RequestParam(value = "number",defaultValue = "1") Integer pageNumber){
  111. ModelAndView modelAndView=new ModelAndView("www/xczxw/listBySouSuo");
  112. Map<String,Object> map =new HashMap<String,Object>(2);
  113. map.put("keyword",keyword);
  114. PageInfo<TCmsContent> tCmsContentPageInfo = contentService.QueryTitleDescription(pageNumber, map);
  115. TCmsSite site = new TCmsSite();
  116. site.setSiteId(Integer.parseInt(siteId));
  117. modelAndView.addObject("model",tCmsContentPageInfo);
  118. modelAndView.addObject("frontPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+sitePrefix);
  119. modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
  120. modelAndView.addObject("keyword",keyword);
  121. modelAndView.addObject("siteId",siteId);
  122. modelAndView.addObject("site",site);
  123. return modelAndView;
  124. }
  125. //搜索
  126. @RequestMapping("/searchFor")
  127. public ModelAndView SearchTitleFor(String type, String lmid, String keyword, String siteId, @RequestParam(value = "number",defaultValue = "1") Integer pageNumber){
  128. ModelAndView modelAndView=new ModelAndView("www/tuiyijunren/listForSearch");
  129. Map<String,Object>map=new HashMap<>(3);
  130. map.put("type",type);
  131. if(lmid != null && !lmid.equals("0")){
  132. map.put("categoryId",lmid);
  133. }
  134. map.put("keyword",keyword);
  135. PageInfo<TCmsContent> tCmsContentPageInfo = contentService.SearchTitleFor(pageNumber,map);
  136. TCmsSite site = new TCmsSite();
  137. site.setSiteId(Integer.parseInt(siteId));
  138. modelAndView.addObject("model",tCmsContentPageInfo);
  139. modelAndView.addObject("frontPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+sitePrefix);
  140. modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
  141. modelAndView.addObject("keyword",keyword);
  142. modelAndView.addObject("siteId",siteId);
  143. modelAndView.addObject("site",site);
  144. return modelAndView;
  145. }
  146. /**
  147. * 站点首页
  148. * @param siteId
  149. * @param model
  150. * @return
  151. */
  152. @GetMapping("/${system.site.prefix}/{siteId}")
  153. public String index(@PathVariable("siteId") Integer siteId,
  154. Model model){
  155. log.debug("通过站点Id访问网站首页[{}]",siteId);
  156. SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
  157. SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
  158. SimpleDateFormat formatMonth = new SimpleDateFormat("M");
  159. //查询新闻资讯
  160. TCmsContentVo c= new TCmsContentVo();
  161. c.setCategoryId((long) 1058);
  162. PageInfo p = contentService.pages(1,4,c);
  163. List<IndexObject> xwdtList=p.getList();
  164. /* TCmsCategory category =categoryService.findById((long) 1036);
  165. TCmsModel cmsModel = modelService.findById(category.getModelId());*/
  166. /*判断是否开启静态*/
  167. File file = new File(PathUtil.getWebRootPath() +File.separator+ "html" + File.separator+ siteId + File.separator+"index.html");
  168. if (file.exists() && Boolean.parseBoolean(siteStatic)&& HtmlThread.size()<=0&&!ControllerUtil.isMobile()) {
  169. return "redirect:/html/"+ siteId + "/index.html";
  170. }
  171. TCmsSite site = siteService.findById(siteId);
  172. if(CmsUtil.isNullOrEmpty(site)) {
  173. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  174. }
  175. if(!site.getStatus()) {
  176. throw new CmsException(CmsConst.SITE_LOCKED);
  177. }
  178. // model.addAttribute("content",contentService.findContentByContentIdAndTableName((long) 12834,cmsModel.getTableName()));
  179. model.addAttribute("title",site.getTitle());
  180. model.addAttribute("keyword",site.getKeyword());
  181. model.addAttribute("description",site.getDescription());
  182. model.addAttribute("site",site);
  183. model.addAttribute("xwdtList",xwdtList);
  184. if(StrUtil.isBlank(site.getTemplate())) {
  185. return view(CmsConst.INDEX_TPL);
  186. }
  187. return view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),CmsConst.INDEX_TPL);
  188. }
  189. @GetMapping("/${system.site.prefix}/{siteId}/{categoryId}")
  190. public String category(@PathVariable("siteId") Integer siteId,
  191. @PathVariable("categoryId") Long categoryId,
  192. Model model){
  193. log.debug("栏目");
  194. TCmsSite site = siteService.findById(siteId);
  195. if(CmsUtil.isNullOrEmpty(site)) {
  196. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  197. }
  198. TCmsCategory category = categoryService.findById(categoryId);
  199. if(CmsUtil.isNullOrEmpty(category)) {
  200. throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
  201. }
  202. PageInfo page = contentService.page(1,siteId,category.getCategoryId());
  203. model.addAttribute("title",category.getCategoryName());
  204. model.addAttribute("keyword",site.getKeyword());
  205. model.addAttribute("description",site.getDescription());
  206. model.addAttribute("site",site);
  207. model.addAttribute("category",category);
  208. model.addAttribute("page",page);
  209. if(StrUtil.isBlank(site.getTemplate())) {
  210. return view(category.getIndexTpl());
  211. }
  212. return view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),category.getIndexTpl());
  213. }
  214. /*网站栏目列表页*/
  215. @GetMapping("/${system.site.prefix}/{siteId}/{categoryId}/index_{pageNumber}")
  216. public String page(@PathVariable("siteId") Integer siteId,
  217. @PathVariable("categoryId") Long categoryId,
  218. @PathVariable(value = "pageNumber") Integer pageNumber,
  219. Model model){
  220. log.debug("列表");
  221. TCmsSite site = siteService.findById(siteId);
  222. if(CmsUtil.isNullOrEmpty(site)) {
  223. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  224. }
  225. TCmsCategory category = categoryService.findById(categoryId);
  226. if(CmsUtil.isNullOrEmpty(category)) {
  227. throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
  228. }
  229. if(CmsUtil.isNullOrEmpty(pageNumber)) {
  230. throw new CmsException(CmsConst.PAGE_NOT_FOUND);
  231. }
  232. PageInfo page = contentService.page(pageNumber,siteId,category.getCategoryId());
  233. model.addAttribute("title",category.getCategoryName());
  234. model.addAttribute("keyword",site.getKeyword());
  235. model.addAttribute("description",site.getDescription());
  236. model.addAttribute("site",site);
  237. model.addAttribute("category",category);
  238. model.addAttribute("page",page);
  239. if(StrUtil.isBlank(site.getTemplate())) {
  240. return view(category.getListTpl());
  241. }
  242. return view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),category.getListTpl());
  243. }
  244. /*网站内容页*/
  245. @GetMapping("/${system.site.prefix}/{siteId}/{categoryId}/{contentId}")
  246. public String content(@PathVariable("siteId") Integer siteId,
  247. @PathVariable("categoryId") Long categoryId,
  248. @PathVariable("contentId") Long contentId,
  249. Model model){
  250. TCmsSite site = siteService.findById(siteId);
  251. if(CmsUtil.isNullOrEmpty(site)) {
  252. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  253. }
  254. TCmsCategory category = categoryService.findById(categoryId);
  255. if(CmsUtil.isNullOrEmpty(category)) {
  256. throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
  257. }
  258. TCmsModel contentModel = modelService.findById(category.getModelId());
  259. if(CmsUtil.isNullOrEmpty(category)) {
  260. throw new CmsException(CmsConst.PAGE_NOT_FOUND);
  261. }
  262. Map content = contentService.findContentByContentIdAndTableName(contentId,contentModel.getTableName());
  263. if(CmsUtil.isNullOrEmpty(content)) {
  264. throw new CmsException(CmsConst.CONTENT_NOT_FOUND);
  265. }
  266. contentService.viewUpdate(contentId);
  267. model.addAttribute("title",content.get("title"));
  268. model.addAttribute("keyword",content.get("keywords"));
  269. model.addAttribute("description",content.get("description"));
  270. model.addAttribute("site",site);
  271. model.addAttribute("category",category);
  272. model.addAttribute("content",content);
  273. if(StrUtil.isBlank(site.getTemplate())) {
  274. return this.view(category.getContentTpl());
  275. }
  276. return this.view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),category.getContentTpl());
  277. }
  278. /*全文检索和模型字段检索*/
  279. @RequestMapping("/search")
  280. public String search(@RequestParam(value = "keyword",required = false) String keyword,
  281. @RequestParam(value = "m",defaultValue = "0") Integer modelId,
  282. @RequestParam(value = "s",defaultValue = "0") Integer siteId,
  283. @RequestParam(value = "c",defaultValue = "0") Long catId,
  284. @RequestParam(value = "p",defaultValue = "1") Integer pageNumber,
  285. HttpServletRequest request){
  286. log.debug("搜索");
  287. TCmsSite site = siteService.findById(siteId);
  288. if(CmsUtil.isNullOrEmpty(site)) {
  289. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  290. }
  291. if (modelId > 0 && catId > 0) {
  292. String action = httpProtocol + "://" + ControllerUtil.getDomain()+"/"+sitePrefix+"/"+site.getSiteId();
  293. action += "/search?m=" + modelId + "&c=" + catId;
  294. TCmsCategory category = categoryService.findById(catId);
  295. if(CmsUtil.isNullOrEmpty(category)) {
  296. throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
  297. }
  298. TCmsModel model = modelService.findById(modelId);
  299. Map<String, Object> param = Maps.newHashMap();
  300. List<TCmsModelFiled> modelFileds = modelFiledService.findModelFiledListByModelId(modelId);
  301. /* 循环检索获取filedName和fildValue*/
  302. for (TCmsModelFiled filed : modelFileds) {
  303. String filedValue = request.getParameter(filed.getFiledName());
  304. if (!StrUtil.isBlank(filedValue)) {
  305. param.put(HtmlKit.getText(filed.getFiledName()).trim(), HtmlKit.getText(filedValue).trim());
  306. action += "&" + filed.getFiledName() + "=" + filedValue;
  307. }
  308. }
  309. PageInfo page = contentService.findContentListByModelFiledValue(pageNumber,catId,model.getTableName(), param);
  310. request.setAttribute("title",category.getCategoryName());
  311. request.setAttribute("keyword",site.getKeyword());
  312. request.setAttribute("description",site.getDescription());
  313. request.setAttribute("site", site);
  314. request.setAttribute("category", category);
  315. request.setAttribute("page", page);
  316. request.setAttribute("param", param);
  317. request.setAttribute("action", action);
  318. return view(site.getTemplate(), category.getListTpl());
  319. }else{
  320. String action = httpProtocol + "://" + ControllerUtil.getDomain()+"/"+sitePrefix+"/"+site.getSiteId();
  321. if(StrUtil.isBlank(keyword)) {
  322. throw new CmsException(CmsConst.SEARCH_KEYWORD_NOT_FOUND);
  323. }
  324. action +="/search?keyword="+keyword;
  325. PageInfo page =luceneService.page(pageNumber,Integer.parseInt(this.pageSize),keyword);
  326. request.setAttribute("page",page);
  327. request.setAttribute("site",site);
  328. request.setAttribute("action", action);
  329. request.setAttribute("keyword", keyword);
  330. return view(site.getTemplate(), CmsConst.SEARCH_TPL);
  331. }
  332. }
  333. /*专题*/
  334. @GetMapping("/${system.site.prefix}/{siteId}/topic/{topicId}")
  335. public String topic(@PathVariable("siteId") Integer siteId,
  336. @PathVariable("topicId") Integer topicId,
  337. Model model){
  338. TCmsSite site = siteService.findById(siteId);
  339. if(CmsUtil.isNullOrEmpty(site)) {
  340. throw new CmsException(CmsConst.SITE_NOT_FOUND);
  341. }
  342. TCmsTopic topic =topicService.findById(topicId);
  343. if(CmsUtil.isNullOrEmpty(topic)) {
  344. throw new CmsException(CmsConst.TOPIC_NOT_FOUND);
  345. }
  346. model.addAttribute("title",topic.getTopicName());
  347. model.addAttribute("keyword",topic.getKeywords());
  348. model.addAttribute("description",topic.getDescription());
  349. model.addAttribute("site",site);
  350. model.addAttribute("topic",topic);
  351. return view(site.getTemplate(), !StrUtil.isBlank(topic.getTopicTpl())?topic.getTopicTpl():CmsConst.TOPIC_TPL);
  352. }
  353. @RequestMapping(value = "/ad/js/{id}",produces = "text/javascript; charset=UTF-8")
  354. @ResponseBody
  355. public String AdJs(@PathVariable Integer id){
  356. if(CmsUtil.isNullOrEmpty(id)) {
  357. throw new CmsException("广告不存在!");
  358. }
  359. return adService.toJavascript(id);
  360. }
  361. @PostMapping("getAllCategory")
  362. @ResponseBody
  363. public List<TCmsCategory> getAllCategory(){
  364. return categoryService.findAll();
  365. }
  366. private String view(String viewName){
  367. return this.view("default",viewName);
  368. }
  369. private String view(String theme,String viewName){
  370. return "www/"+theme.trim()+"/"+viewName.trim();
  371. }
  372. }