cn.itganhuo.app.web.controller.CommonController.java Source code

Java tutorial

Introduction

Here is the source code for cn.itganhuo.app.web.controller.CommonController.java

Source

/*
 * Copyright 2014-2024 the https://github.com/xiaoxing598/itganhuo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * This project consists of JAVA private school online learning community group Friends co-creator [QQ group 329232140].
 * JAVA???[QQ329232140];
 * See the list of IT dry technology sharing network [http://www.itganhuo.cn/teams].
 * ????IT[http://www.itganhuo.cn/teams];
 * The author does not guarantee the quality of the project and its stability, reliability, and security does not bear any responsibility.
 * ????????.
 */
package cn.itganhuo.app.web.controller;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.itganhuo.app.common.pool.ConfigPool;
import cn.itganhuo.app.common.utils.DateUtil;
import cn.itganhuo.app.common.utils.StringUtil;
import cn.itganhuo.app.entity.Article;
import cn.itganhuo.app.entity.Label;
import cn.itganhuo.app.entity.RespMsg;
import cn.itganhuo.app.entity.User;
import cn.itganhuo.app.exception.InternalException;
import cn.itganhuo.app.service.ArticleService;
import cn.itganhuo.app.service.LabelService;
import cn.itganhuo.app.service.UserService;

/**
 * <h2></h2>
 * <dl>
 * <dt>??</dt>
 * <dd></dd>
 * <dt></dt>
 * <dd></dd>
 * </dl>
 *
 * @author -?
 * @version 0.0.1-SNAPSHOT
 */
@Controller
public class CommonController {

    private static final Logger log = LogManager.getLogger(CommonController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private LabelService labelService;
    @Autowired
    private ArticleService articleService;

    /**
     * ?
     *
     * @return ??
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/header", method = RequestMethod.GET)
    public String refurlHeader() {
        return "common/header";
    }

    /**
     * ?
     *
     * @return ??
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/footer", method = RequestMethod.GET)
    public String refurlFooter() {
        return "common/footer";
    }

    /**
     * 
     *
     * @return ?
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        Subject current_user = SecurityUtils.getSubject();
        current_user.logout();
        return "redirect:/user/signin";
    }

    /**
     * ?????
     * <ol>
     * <li>??????</li>
     * </ol>
     *
     * @return
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/unauthorized")
    public String refurlUnauthorized() {
        return "common/unauthorized";
    }

    /**
     * ???
     *
     * @return ????
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/teams", method = RequestMethod.GET)
    public String refurlTeam(Model model, HttpServletRequest request) {
        model.addAttribute("path", request.getContextPath());
        return "team";
    }

    /**
     * ??
     * <ol>
     * <li>
     * ???? ID?????? <br>
     * ????????????????</li>
     * </ol>
     *
     * @return
     * @author -?(504487927) 2014-9-8
     * @since itganhuo1.0
     */
    @Transactional
    @RequestMapping(value = "/auth_email/{id}/{code}", method = RequestMethod.GET)
    public ModelAndView authEmail(@PathVariable(value = "id") String id,
            @PathVariable(value = "code") String code) {
        ModelAndView mav = new ModelAndView();
        RespMsg respMsg = new RespMsg();
        // ??
        if (StringUtil.hasText(id) && StringUtil.hasText(code)) {
            User user = userService.loadById(Integer.valueOf(id.trim()));
            // ??
            if (user != null && user.getId() > 0) {
                // ????
                if (1 != user.getIsValidateEmail()) {
                    try {
                        Date validate_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .parse(user.getEmailValidateDate());
                        Date now_date = DateUtil.getAfterOrBeforDay(
                                Integer.valueOf(ConfigPool.getString("parameter.emailLinkValidCertification")));
                        // ??
                        if (now_date.after(validate_date)) {
                            // ???
                            if (code.trim().equals(user.getEmailValidateCode().trim())) {
                                // ?????
                                User um = new User();
                                um.setAccount(user.getAccount());
                                um.setIsValidateEmail(1);
                                um.setIsLock(0);
                                userService.updateInfoByAccount(um);
                                // ???
                                respMsg.setStatus("0000");
                                respMsg.setMessage(MessageFormat.format(
                                        ConfigPool.getString("respMsg.email.EmailAuthSuccessful"),
                                        user.getAccount()));
                            } else {
                                respMsg.setStatus("1004");
                                respMsg.setMessage(
                                        ConfigPool.getString("respMsg.email.CertificationSignatureAddressErrors"));
                            }
                        } else {
                            respMsg.setStatus("1003");
                            respMsg.setMessage(ConfigPool.getString("respMsg.email.LinkSddressFailure"));
                        }
                    } catch (ParseException e) {
                        throw new InternalException(log,
                                "Date conversion error, the date may be the source character is not legitimate.",
                                e);
                    }
                } else {
                    respMsg.setStatus("1002");
                    respMsg.setMessage(ConfigPool.getString("respMsg.email.EmailHasBeenCertified"));
                }
            } else {
                respMsg.setStatus("1001");
                respMsg.setMessage(ConfigPool.getString("respMsg.email.UnknownAccount"));
            }
        } else {
            respMsg.setStatus("1000");
            respMsg.setMessage(ConfigPool.getString("respMsg.email.RequestAddressWrong"));
        }
        mav.setViewName("common/email_auth_result");
        mav.addObject("respMsg", respMsg);
        return mav;
    }

    /**
     * ??
     *
     * @return ???
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/devblog", method = RequestMethod.GET)
    public String refurlDevblog(Model model, HttpServletRequest request) {
        model.addAttribute("path", request.getContextPath());
        return "devblog";
    }

    /**
     * ?
     *
     * @return ??
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public String refurlAbout(Model model, HttpServletRequest request) {
        model.addAttribute("path", request.getContextPath());
        return "about";
    }

    /**
     * ??
     *
     * @return
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/invitation", method = RequestMethod.GET)
    public String invated(Model model, HttpServletRequest request) {
        model.addAttribute("path", request.getContextPath());
        return "invitation";
    }

    /**
     * 
     *
     * @return ?
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String refurlIndex(Model model, HttpServletRequest request) {
        // 1?
        List<Label> ls = labelService.getLabelByCondition(null);
        // 2??
        if (ls != null && ls.size() > 0) {
            for (int i = 0; i < ls.size(); i++) {
                Map<String, Object> param = new HashMap<String, Object>();
                param.put("labelId", ls.get(i).getId());
                param.put("rows", 5);
                List<Article> articles = articleService.getArticleByLabelId(param);
                ls.get(i).setArticles(articles);
            }
        }
        //
        List<Label> popularTags = labelService.queryPopularTags(10);
        //
        List<Map<String, Object>> popularAuthors = articleService.queryPopularAuthors(10);
        model.addAttribute("popularAuthors", popularAuthors);
        model.addAttribute("popularTags", popularTags);
        model.addAttribute("labels", ls);
        model.addAttribute("path", request.getContextPath());
        model.addAttribute("servletPath", request.getServletPath());
        return "index";
    }

    /**
     * ?
     *
     * @return ??
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/labels", method = RequestMethod.GET)
    public String refurlLabels(Model model, HttpServletRequest request) {
        Subject current_user = SecurityUtils.getSubject();
        String account = (String) current_user.getPrincipal();
        if (StringUtil.hasText(account)) {
            User user = userService.loadByAccount(account);
            // 1?
            List<Map<String, String>> lists = labelService.getLabelByCondition2(user.getId());
            model.addAttribute("lists", lists);
        } else {
            // 2?
            List<Label> ls = labelService.getLabelByCondition(null);
            model.addAttribute("ls", ls);
        }
        model.addAttribute("path", request.getContextPath());
        model.addAttribute("servletPath", request.getServletPath());
        return "labels";
    }
}