IndexController.java 18 KB

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