123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- package com.sooka.module.web.cms;
- import com.github.pagehelper.PageInfo;
- import com.google.common.collect.Maps;
- import com.sooka.ServletInitializer;
- import com.sooka.common.constant.CmsConst;
- import com.sooka.common.exception.CmsException;
- import com.sooka.common.utils.*;
- import com.sooka.component.beetl.thread.HtmlThread;
- import com.sooka.component.lucene.util.IndexObject;
- import com.sooka.module.web.cms.service.*;
- import com.sooka.module.web.cms.vo.TCmsContentVo;
- import com.sooka.module.web.system.service.AttachmentService;
- import com.sooka.mybatis.model.*;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.servlet.ModelAndView;
- import tk.mybatis.mapper.util.StringUtil;
- import javax.servlet.http.HttpServletRequest;
- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import static com.sooka.common.constant.CmsConst.LOGIN_USER;
- /**
- * Description:前台首页
- *
- *
- * @create 2017-04-13
- **/
- @Controller
- @RequestMapping("/")
- public class IndexController {
- private final static Logger log = LoggerFactory.getLogger(IndexController.class);
- @Autowired
- private SiteService siteService;
- @Autowired
- private CategoryService categoryService;
- @Autowired
- private ContentService contentService;
- @Autowired
- private ModelService modelService;
- @Autowired
- private ModelFiledService modelFiledService;
- @Autowired
- private LuceneService luceneService;
- @Autowired
- private TopicService topicService;
- @Autowired
- private AdService adService;
- @Autowired
- private AttachmentService attachmentService;
- @Value("${system.site.name}")
- private String siteName;
- @Value("${system.http.host}")
- private String host;
- @Value("${server.port}")
- private String port;
- @Value("${system.http.protocol}")
- private String httpProtocol;
- @Value("${system.site.prefix}")
- private String sitePrefix;
- @Value("${system.site.subfix}")
- private String siteSubfix;
- @Value("${system.site.static}")
- private String siteStatic;
- @Value("${system.site.page.size}")
- private String pageSize;
- /*网站首页*/
- @GetMapping
- public ModelAndView index(@RequestParam(value = "keyword",required = false) String keyword, HttpServletRequest request){
- String domain = ControllerUtil.getDomain();
- log.debug("通过域名访问网站首页[{}]",domain);
- TCmsSite site = siteService.findByDomain(domain);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- if(!StrUtil.isBlank(keyword)) {
- return new ModelAndView( "forward:/"+sitePrefix+"/search");
- }
- ModelAndView modelAndView = new ModelAndView( "forward:/"+sitePrefix+"/"+site.getSiteId());
- if (request.getSession().getAttribute(LOGIN_USER) == null) {
- modelAndView.addObject("btnId", "login");
- } else {
- modelAndView.addObject("btnId", "exit");
- }
- return modelAndView;
- }
- @GetMapping("/login")
- public ModelAndView login(){
- ModelAndView modelAndView=new ModelAndView("www/tuiyijunren/login");
- modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
- return modelAndView;
- }
- @GetMapping("/exit")
- public ModelAndView exit(HttpServletRequest request){
- request.getSession().removeAttribute(LOGIN_USER);
- return index(null, request);
- }
- @GetMapping("/register")
- public ModelAndView register(){
- ModelAndView modelAndView=new ModelAndView("www/tuiyijunren/register");
- modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
- return modelAndView;
- }
- @RequestMapping("/queryDescription")
- public ModelAndView QueryTitleDescription (String keyword, String siteId, @RequestParam(value = "number",defaultValue = "1") Integer pageNumber){
- ModelAndView modelAndView=new ModelAndView("www/xczxw/listBySouSuo");
- Map<String,Object> map =new HashMap<String,Object>(2);
- map.put("keyword",keyword);
- PageInfo<TCmsContent> tCmsContentPageInfo = contentService.QueryTitleDescription(pageNumber, map);
- TCmsSite site = new TCmsSite();
- site.setSiteId(Integer.parseInt(siteId));
- modelAndView.addObject("model",tCmsContentPageInfo);
- modelAndView.addObject("frontPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+sitePrefix);
- modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
- modelAndView.addObject("keyword",keyword);
- modelAndView.addObject("siteId",siteId);
- modelAndView.addObject("site",site);
- return modelAndView;
- }
- //搜索
- @RequestMapping("/searchFor")
- public ModelAndView SearchTitleFor(String type, String lmid, String keyword, String siteId, @RequestParam(value = "number",defaultValue = "1") Integer pageNumber){
- ModelAndView modelAndView=new ModelAndView("www/tuiyijunren/listForSearch");
- Map<String,Object>map=new HashMap<>(3);
- map.put("type",type);
- if(lmid != null && !lmid.equals("0")){
- map.put("categoryId",lmid);
- }
- map.put("keyword",keyword);
- PageInfo<TCmsContent> tCmsContentPageInfo = contentService.SearchTitleFor(pageNumber,map);
- TCmsSite site = new TCmsSite();
- site.setSiteId(Integer.parseInt(siteId));
- modelAndView.addObject("model",tCmsContentPageInfo);
- modelAndView.addObject("frontPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+sitePrefix);
- modelAndView.addObject("resPath",httpProtocol+"://"+ host+":"+port+"/"+siteName+"/"+"static"+"/"+"www");
- modelAndView.addObject("keyword",keyword);
- modelAndView.addObject("siteId",siteId);
- modelAndView.addObject("site",site);
- return modelAndView;
- }
- /**
- * 站点首页
- * @param siteId
- * @param model
- * @return
- */
- @GetMapping("/${system.site.prefix}/{siteId}")
- public String index(@PathVariable("siteId") Integer siteId,
- Model model){
- log.debug("通过站点Id访问网站首页[{}]",siteId);
- SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
- SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
- SimpleDateFormat formatMonth = new SimpleDateFormat("M");
- //查询新闻资讯
- TCmsContentVo c= new TCmsContentVo();
- c.setCategoryId((long) 1058);
- PageInfo p = contentService.pages(1,4,c);
- List<IndexObject> xwdtList=p.getList();
- /* TCmsCategory category =categoryService.findById((long) 1036);
- TCmsModel cmsModel = modelService.findById(category.getModelId());*/
- /*判断是否开启静态*/
- File file = new File(PathUtil.getWebRootPath() +File.separator+ "html" + File.separator+ siteId + File.separator+"index.html");
- if (file.exists() && Boolean.parseBoolean(siteStatic)&& HtmlThread.size()<=0&&!ControllerUtil.isMobile()) {
- return "redirect:/html/"+ siteId + "/index.html";
- }
- TCmsSite site = siteService.findById(siteId);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- if(!site.getStatus()) {
- throw new CmsException(CmsConst.SITE_LOCKED);
- }
- // model.addAttribute("content",contentService.findContentByContentIdAndTableName((long) 12834,cmsModel.getTableName()));
- model.addAttribute("title",site.getTitle());
- model.addAttribute("keyword",site.getKeyword());
- model.addAttribute("description",site.getDescription());
- model.addAttribute("site",site);
- model.addAttribute("xwdtList",xwdtList);
- if(StrUtil.isBlank(site.getTemplate())) {
- return view(CmsConst.INDEX_TPL);
- }
- return view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),CmsConst.INDEX_TPL);
- }
- @GetMapping("/${system.site.prefix}/{siteId}/{categoryId}")
- public String category(@PathVariable("siteId") Integer siteId,
- @PathVariable("categoryId") Long categoryId,
- Model model){
- log.debug("栏目");
- TCmsSite site = siteService.findById(siteId);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- TCmsCategory category = categoryService.findById(categoryId);
- if(CmsUtil.isNullOrEmpty(category)) {
- throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
- }
- PageInfo page = contentService.page(1,siteId,category.getCategoryId());
- model.addAttribute("title",category.getCategoryName());
- model.addAttribute("keyword",site.getKeyword());
- model.addAttribute("description",site.getDescription());
- model.addAttribute("site",site);
- model.addAttribute("category",category);
- model.addAttribute("page",page);
- if(StrUtil.isBlank(site.getTemplate())) {
- return view(category.getIndexTpl());
- }
- return view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),category.getIndexTpl());
- }
- /*网站栏目列表页*/
- @GetMapping("/${system.site.prefix}/{siteId}/{categoryId}/index_{pageNumber}")
- public String page(@PathVariable("siteId") Integer siteId,
- @PathVariable("categoryId") Long categoryId,
- @PathVariable(value = "pageNumber") Integer pageNumber,
- Model model){
- log.debug("列表");
- TCmsSite site = siteService.findById(siteId);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- TCmsCategory category = categoryService.findById(categoryId);
- if(CmsUtil.isNullOrEmpty(category)) {
- throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
- }
- if(CmsUtil.isNullOrEmpty(pageNumber)) {
- throw new CmsException(CmsConst.PAGE_NOT_FOUND);
- }
- PageInfo page = contentService.page(pageNumber,siteId,category.getCategoryId());
- model.addAttribute("title",category.getCategoryName());
- model.addAttribute("keyword",site.getKeyword());
- model.addAttribute("description",site.getDescription());
- model.addAttribute("site",site);
- model.addAttribute("category",category);
- model.addAttribute("page",page);
- if(StrUtil.isBlank(site.getTemplate())) {
- return view(category.getListTpl());
- }
- return view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),category.getListTpl());
- }
- /*网站内容页*/
- @GetMapping("/${system.site.prefix}/{siteId}/{categoryId}/{contentId}")
- public String content(@PathVariable("siteId") Integer siteId,
- @PathVariable("categoryId") Long categoryId,
- @PathVariable("contentId") Long contentId,
- Model model){
- TCmsSite site = siteService.findById(siteId);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- TCmsCategory category = categoryService.findById(categoryId);
- if(CmsUtil.isNullOrEmpty(category)) {
- throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
- }
- TCmsModel contentModel = modelService.findById(category.getModelId());
- if(CmsUtil.isNullOrEmpty(category)) {
- throw new CmsException(CmsConst.PAGE_NOT_FOUND);
- }
- Map content = contentService.findContentByContentIdAndTableName(contentId,contentModel.getTableName());
- if(CmsUtil.isNullOrEmpty(content)) {
- throw new CmsException(CmsConst.CONTENT_NOT_FOUND);
- }
- contentService.viewUpdate(contentId);
- model.addAttribute("title",content.get("title"));
- model.addAttribute("keyword",content.get("keywords"));
- model.addAttribute("description",content.get("description"));
- model.addAttribute("site",site);
- model.addAttribute("category",category);
- model.addAttribute("content",content);
- if(StrUtil.isBlank(site.getTemplate())) {
- return this.view(category.getContentTpl());
- }
- return this.view((ControllerUtil.isMobile()&&site.getIsMobile())?site.getMobileTpl():site.getTemplate(),category.getContentTpl());
- }
- /*全文检索和模型字段检索*/
- @RequestMapping("/search")
- public String search(@RequestParam(value = "keyword",required = false) String keyword,
- @RequestParam(value = "m",defaultValue = "0") Integer modelId,
- @RequestParam(value = "s",defaultValue = "0") Integer siteId,
- @RequestParam(value = "c",defaultValue = "0") Long catId,
- @RequestParam(value = "p",defaultValue = "1") Integer pageNumber,
- HttpServletRequest request){
- log.debug("搜索");
- TCmsSite site = siteService.findById(siteId);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- if (modelId > 0 && catId > 0) {
- String action = httpProtocol + "://" + ControllerUtil.getDomain()+"/"+sitePrefix+"/"+site.getSiteId();
- action += "/search?m=" + modelId + "&c=" + catId;
- TCmsCategory category = categoryService.findById(catId);
- if(CmsUtil.isNullOrEmpty(category)) {
- throw new CmsException(CmsConst.CATEGORY_NOT_FOUND);
- }
- TCmsModel model = modelService.findById(modelId);
- Map<String, Object> param = Maps.newHashMap();
- List<TCmsModelFiled> modelFileds = modelFiledService.findModelFiledListByModelId(modelId);
- /* 循环检索获取filedName和fildValue*/
- for (TCmsModelFiled filed : modelFileds) {
- String filedValue = request.getParameter(filed.getFiledName());
- if (!StrUtil.isBlank(filedValue)) {
- param.put(HtmlKit.getText(filed.getFiledName()).trim(), HtmlKit.getText(filedValue).trim());
- action += "&" + filed.getFiledName() + "=" + filedValue;
- }
- }
- PageInfo page = contentService.findContentListByModelFiledValue(pageNumber,catId,model.getTableName(), param);
- request.setAttribute("title",category.getCategoryName());
- request.setAttribute("keyword",site.getKeyword());
- request.setAttribute("description",site.getDescription());
- request.setAttribute("site", site);
- request.setAttribute("category", category);
- request.setAttribute("page", page);
- request.setAttribute("param", param);
- request.setAttribute("action", action);
- return view(site.getTemplate(), category.getListTpl());
- }else{
- String action = httpProtocol + "://" + ControllerUtil.getDomain()+"/"+sitePrefix+"/"+site.getSiteId();
- if(StrUtil.isBlank(keyword)) {
- throw new CmsException(CmsConst.SEARCH_KEYWORD_NOT_FOUND);
- }
- action +="/search?keyword="+keyword;
- PageInfo page =luceneService.page(pageNumber,Integer.parseInt(this.pageSize),keyword);
- request.setAttribute("page",page);
- request.setAttribute("site",site);
- request.setAttribute("action", action);
- request.setAttribute("keyword", keyword);
- return view(site.getTemplate(), CmsConst.SEARCH_TPL);
- }
- }
- /*专题*/
- @GetMapping("/${system.site.prefix}/{siteId}/topic/{topicId}")
- public String topic(@PathVariable("siteId") Integer siteId,
- @PathVariable("topicId") Integer topicId,
- Model model){
- TCmsSite site = siteService.findById(siteId);
- if(CmsUtil.isNullOrEmpty(site)) {
- throw new CmsException(CmsConst.SITE_NOT_FOUND);
- }
- TCmsTopic topic =topicService.findById(topicId);
- if(CmsUtil.isNullOrEmpty(topic)) {
- throw new CmsException(CmsConst.TOPIC_NOT_FOUND);
- }
- model.addAttribute("title",topic.getTopicName());
- model.addAttribute("keyword",topic.getKeywords());
- model.addAttribute("description",topic.getDescription());
- model.addAttribute("site",site);
- model.addAttribute("topic",topic);
- return view(site.getTemplate(), !StrUtil.isBlank(topic.getTopicTpl())?topic.getTopicTpl():CmsConst.TOPIC_TPL);
- }
- @RequestMapping(value = "/ad/js/{id}",produces = "text/javascript; charset=UTF-8")
- @ResponseBody
- public String AdJs(@PathVariable Integer id){
- if(CmsUtil.isNullOrEmpty(id)) {
- throw new CmsException("广告不存在!");
- }
- return adService.toJavascript(id);
- }
- @PostMapping("getAllCategory")
- @ResponseBody
- public List<TCmsCategory> getAllCategory(){
- return categoryService.findAll();
- }
- private String view(String viewName){
- return this.view("default",viewName);
- }
- private String view(String theme,String viewName){
- return "www/"+theme.trim()+"/"+viewName.trim();
- }
- }
|