結組作業,第一階段衝刺(4)

saccharin發表於2024-05-07

昨天完成了資料庫的連線,以及登陸介面和主介面的完善,今天實現不同使用者登入顯示不同介面,普通使用者登入後,能夠釋出招聘資訊,招聘資訊裡有電話,郵箱,招聘類別,招聘內容,並且能夠上傳圖片。

明天目標是實現管理員登入的頁面。

以下為今日程式碼:

package com.work.controller.pc;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.work.common.utils.BaseUtil;
import com.work.common.utils.JsonUtil;
import com.work.common.utils.MD5Util;
import com.work.pojo.SystemUpset;
import com.work.pojo.User;
import com.work.service.SystemUpsetService;
import com.work.service.UserService;

@Controller
@RequestMapping("/pc/login")
public class PcLoginController extends BaseUtil{

    @Autowired
    private UserService userService;
    @Autowired
    private SystemUpsetService systemUpsetService;
    
    //去登入頁面
    @RequestMapping(value="/goLogin")
    public ModelAndView goLogin(HttpServletResponse response,HttpServletRequest request,ModelAndView mv,SystemUpset systemUpset){
        List<SystemUpset> systemUpsets = systemUpsetService.getSystemUpsetList(systemUpset, null, null);
        if(systemUpsets.size() > 0){
            mv.addObject("systemUpset", systemUpsets.get(0));
        }
        User pcUser = (User) request.getSession().getAttribute("pcUser");
        if(pcUser == null){
            mv.setViewName("pc/login");
        }else{
            mv.addObject("updatePage", "y");
            mv.setViewName("pc/index");
        }
        return mv;
    }
    
    //執行登入
    @RequestMapping(value="/doLogin")
    public void doLogin(HttpServletRequest request,HttpServletResponse response,User user){
        User pcUser = (User) request.getSession().getAttribute("pcUser");
        if(pcUser == null){
            if(user.getUserId() == null){
                output(response,JsonUtil.buildFalseJson(1,"賬號不能為空"));
            }
            if(user.getPassword().equals("") || user.getPassword() == null){
                    output(response,JsonUtil.buildFalseJson(1,"密碼不能為空"));
                }
            User userInfo = userService.getUserById(user.getUserId());
                if(userInfo == null){
                    output(response,JsonUtil.buildFalseJson(1,"賬號不正確或系統不存在該使用者"));
                }          
                if(!userInfo.getPassword().equals(MD5Util.MD5Encode(user.getPassword(),"utf-8"))){
                    output(response,JsonUtil.buildFalseJson(1,"您輸入的密碼不正確,請重試")); 
                }          
                //判斷賬號是否被禁用
                if(userInfo.getIsEffect() != 1){
                    output(response,JsonUtil.buildFalseJson(1,"對不起您的賬號已被禁用"));
                }else{
                    //執行登陸後臺的邏輯
                    request.getSession().setAttribute("pcUser",userInfo);
                    output(response,JsonUtil.buildFalseJson(0,"登入成功"));
                }
        }else{
            output(response,JsonUtil.buildFalseJson(0,"您已登入,無需重複登入"));
        }
    }
    
    //退出登入
    @RequestMapping(value="/outLogin")
    public void outLogin(HttpServletRequest request,HttpServletResponse response,User user){
        User pcUser = (User) request.getSession().getAttribute("pcUser");
        if(pcUser != null){
            //銷燬session
            request.getSession().invalidate();;
            output(response, JsonUtil.buildFalseJson(0, "退出登入成功!"));
        }else{
            output(response, JsonUtil.buildFalseJson(1, "您還未登陸不能執行退出操作"));
        }
    }
    
    //去註冊頁面
    @RequestMapping(value="/goRegister")
    public ModelAndView goRegister(ModelAndView mv,HttpServletRequest request,SystemUpset systemUpset){
        List<SystemUpset> systemUpsets = systemUpsetService.getSystemUpsetList(systemUpset, null, null);
        if(systemUpsets.size() > 0){
            mv.addObject("systemUpset", systemUpsets.get(0));
        }
        User user = (User) request.getSession().getAttribute("pcUser");
        if(user != null){
            mv.setViewName("pc/index");
        }else{
            mv.setViewName("pc/register");
        }
        return mv;
    }
    
    //檢測暱稱是否重複
    @RequestMapping(value="/findUserByNickName")
    public void findUserByNickName(HttpServletRequest request,HttpServletResponse response,User user){
        User userInfo = userService.getUserByNickName(user);
        User pcUser = (User) request.getSession().getAttribute("pcUser");
        if(pcUser != null){
            if(userInfo != null && !pcUser.getNickName().equals(user.getNickName())){
                output(response, JsonUtil.buildFalseJson(0, "該暱稱已被使用者使用"));
            }else{
                output(response, JsonUtil.buildFalseJson(1, "該暱稱未被使用者使用"));
            }
        }else{
            if(userInfo != null){
                output(response, JsonUtil.buildFalseJson(0, "該暱稱已被使用者使用"));
            }else{
                output(response, JsonUtil.buildFalseJson(1, "該暱稱未被使用者使用"));
            }
        }
    }
    
    //執行註冊
    @RequestMapping(value="/doRegister")
    public void doRegister(HttpServletRequest request,HttpServletResponse response,User user){
        user.setIsEffect(1);
        user.setUserLeven(0);
        user.setIsWork(0);
        user.setPassword(MD5Util.MD5Encode(user.getPassword(),"utf-8"));
        user.setCreateTime(new Date());
        userService.addUser(user);
        Integer userId =  user.getUserId();
        output(response, JsonUtil.buildFalseJson(0, String.valueOf(userId)));
    }
    
    //判斷賬號有沒有設定密保
    @RequestMapping(value="/isQuestion")
    public void isQuestion(HttpServletRequest request,HttpServletResponse response,User user){
        User userInfo = userService.getUserById(user.getUserId());
        if(userInfo == null || userInfo.getIsEffect() == 0){
            output(response, JsonUtil.buildFalseJson(1, "該賬號不存在!"));
        }else if(userInfo.getQuestion() == null || userInfo.getQuestion().equals("")){
            output(response, JsonUtil.buildFalseJson(1, "該賬號沒有設定密保!"));
        }else{
            output(response, JsonUtil.buildFalseJson(0, "該賬號已設定密保!"));
        }
    }
    
    //去找回密碼
    @RequestMapping(value="/goForgetPassword")
    public ModelAndView goForgetPassword(ModelAndView mv,HttpServletRequest request,User user){
        SystemUpset systemUpset = new SystemUpset(); 
        List<SystemUpset> systemUpsets = systemUpsetService.getSystemUpsetList(systemUpset, null, null);
        if(systemUpsets.size() > 0){
            mv.addObject("systemUpset", systemUpsets.get(0));
        }
        User userInfo = userService.getUserById(user.getUserId());
        mv.addObject("user", userInfo);
        mv.setViewName("pc/forget_password");
        return mv;
    }
    
    //判斷答案是否正確
    @RequestMapping(value="/isAnswer")
    public void isAnswer(HttpServletRequest request,HttpServletResponse response,User user){
        User userInfo = userService.getUserById(user.getUserId());
        if(userInfo.getAnswer().equals(user.getAnswer())){
            request.getSession().setAttribute("temUser",userInfo);
            output(response, JsonUtil.buildFalseJson(0, "答案正確!"));
        }else{
            output(response, JsonUtil.buildFalseJson(1, "答案錯誤!"));
        }
    }
    
    //去設定新密碼
    @RequestMapping(value="/goSetPassword")
    public ModelAndView goSetPassword(ModelAndView mv,HttpServletRequest request){
        SystemUpset systemUpset = new SystemUpset(); 
        List<SystemUpset> systemUpsets = systemUpsetService.getSystemUpsetList(systemUpset, null, null);
        if(systemUpsets.size() > 0){
            mv.addObject("systemUpset", systemUpsets.get(0));
        }
        mv.setViewName("pc/set_password");
        return mv;
    }
    
    //修改密碼
    @RequestMapping("/updatePassword") 
    public void updatePassword(HttpServletRequest request,HttpServletResponse response,String passwordYes, String password){
        User temUser = (User) request.getSession().getAttribute("temUser");
        User user = new User();
        if(passwordYes.equals(password)){
            user.setUserId(temUser.getUserId());
            user.setPassword(MD5Util.MD5Encode(password,"utf-8"));
            userService.updateUser(user);
            output(response,JsonUtil.buildFalseJson(0, "修改密碼成功"));
        }else{
            output(response,JsonUtil.buildFalseJson(1, "前後密碼不一致"));
        }
    }
}

相關文章