신입사원 입문교육_정리

project_controller

FireStone 2021. 2. 21. 23:09
package com.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.dto.Member;
import com.service.MemberService;



@SessionAttributes("member")
@Controller
public class MemberController {

	private Logger logger = LoggerFactory.getLogger(MemberController.class);
	@Autowired
	MemberService service;
	@Autowired
	BCryptPasswordEncoder pwEncoder;
	
	@Autowired
	private JavaMailSender mailSender;
	private String emailCode;

	@RequestMapping(value = "/member/changeProfile.do", method = RequestMethod.POST)
	public ModelAndView changeProfile(Member m, MultipartFile newProfileImg, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		String saveDir = "";

		if (m.getMemberAuthority().equals("A")) {
			saveDir = request.getSession().getServletContext().getRealPath("/resources/image/adminProfile");
		} else {
			saveDir = request.getSession().getServletContext().getRealPath("/resources/image/memberProfile");
		}

		String msg = "";
		String loc = "";

		if (!newProfileImg.isEmpty()) {
			// 파일명을 생성(rename)
			String orifileName = newProfileImg.getOriginalFilename();
			System.out.println("현재파일:" + orifileName);
			String ext = orifileName.substring(orifileName.lastIndexOf("."));
			// rename 규칙설정
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
			int rdv = (int) (Math.random() * 1000);
			String reName = sdf.format(System.currentTimeMillis()) + "_" + rdv + ext;
			// 파일을 저장해보자
			try {
				newProfileImg.transferTo(new File(saveDir + "/" + reName));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			m.setMemberProfile(reName);
			System.out.println("바뀐파일:" + reName);
		}

		int result = service.changeProfile(m);

		if (result > 0) {
			msg = "프로필사진 변경 완료";
			if (m.getMemberAuthority().equals("A")) {
				loc = "/admin/adminPage.do";
			} else {
				loc = "/member/myPage.do";
			}
		} else {
			msg = "프로필사진 변경 실패";
			if (m.getMemberAuthority().equals("A")) {
				loc = "/admin/adminPage.do";
			} else {
				loc = "/member/myPage.do";
			}
		}

		mv.addObject("msg", msg);
		mv.addObject("loc", loc);
		mv.setViewName("common/msg");

		return mv;
	}

	// mailSending 코드
	@RequestMapping("/member/mailSending.do")
	public ModelAndView mailSending(String memberEmail, ModelAndView mv) throws UnsupportedEncodingException {
		int ran = new Random().nextInt(100000) + 10000; // 인증 코드용 난수 발생 10000 ~ 99999
		System.out.println("왔다");
		System.out.println("이메일 : " + memberEmail);
		String setfrom = "admin";
		String title = "handmakers 인증 코드"; // 제목
		String content = "인증 코드는 " + Integer.toString(ran) + " 입니다. 인증 코드란에 입력해주세요."; // 인증 코드
		emailCode = Integer.toString(ran);
		try {
			MimeMessage message = mailSender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

			messageHelper.setFrom(setfrom); // 보내는사람 생략하면 정상작동을 안함
			messageHelper.setTo(memberEmail); // 받는사람 이메일
			messageHelper.setSubject(title); // 메일제목은 생략이 가능하다
			messageHelper.setText(content); // 메일 내용

			mailSender.send(message);
		} catch (Exception e) {
			System.out.println(e);
		}

		mv.addObject("randomNumber", ran);
		mv.setViewName("jsonView");

		return mv;
	}

	@RequestMapping("/member/mailCheck.do")
	public ModelAndView mailCheck(String memberEmail, ModelAndView mv) throws UnsupportedEncodingException {
		System.out.println("왔다@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		int ran = new Random().nextInt(100000) + 10000; // 인증 코드용 난수 발생 10000 ~ 99999
		Member m = service.memberEmailFind(memberEmail);
		String emailState = "F";

		System.out.println("이메일 : " + memberEmail);

		if (m != null) {
			String setfrom = "admin";
			String title = "handmakers 인증 코드"; // 제목
			String content = "인증 코드는 " + Integer.toString(ran) + " 입니다. 인증 코드란에 입력해주세요."; // 인증 코드
			emailCode = Integer.toString(ran);

			try {
				MimeMessage message = mailSender.createMimeMessage();
				MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

				messageHelper.setFrom(setfrom); // 보내는사람 생략하면 정상작동을 안함
				messageHelper.setTo(memberEmail); // 받는사람 이메일
				messageHelper.setSubject(title); // 메일제목은 생략이 가능하다
				messageHelper.setText(content); // 메일 내용

				mailSender.send(message);
			} catch (Exception e) {
				System.out.println(e);
			}

			emailState = "T";
			mv.addObject("randomNumber", emailCode);
			mv.addObject("emailState", emailState);
			mv.setViewName("jsonView");

			return mv;
		} else {
			mv.addObject("emailState", emailState);
			mv.setViewName("jsonView");

			return mv;
		}
	}

	@RequestMapping("/member/memberIdFind.do")
	public ModelAndView memberIdFind(String memberEmail, ModelAndView mv) throws UnsupportedEncodingException {

		String memberId = service.memberIdFind(memberEmail);
		String setfrom = "admin";
		String title = "handmakers 아이디 찾기 이메일입니다."; // 제목
		String content = "귀하의 ID는  " + memberId + " 입니다."; // 인증 코드

		logger.debug("내 아이디 왔니?" + content);

		try {
			MimeMessage message = mailSender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

			messageHelper.setFrom(setfrom); // 보내는사람 생략하면 정상작동을 안함
			messageHelper.setTo(memberEmail); // 받는사람 이메일
			messageHelper.setSubject(title); // 메일제목은 생략이 가능하다
			messageHelper.setText(content); // 메일 내용

			mailSender.send(message);
		} catch (Exception e) {
			System.out.println(e);
		}

		mv.setViewName("jsonView");

		return mv;
	}

	@RequestMapping("/member/mailSendingForPwd.do")
	public ModelAndView mailSendingForPwd(String memberEmail, String memberEmailId, ModelAndView mv)
			throws UnsupportedEncodingException {
		System.out.println("찾으러왔다");
		int ran = new Random().nextInt(100000) + 10000; // 인증 코드용 난수 발생 10000 ~ 99999
		Map<String, String> findMember = new HashMap<String, String>();
		findMember.put("memberEmail", memberEmail);
		findMember.put("memberId", memberEmailId);
		String memberState = "F";

		Member member = service.memberFind(findMember);
		System.out.println("내가 찾은 멤버: " + member);
		/*
		 * if(member.getMemberId().equals(memberEmailId) &&
		 * member.getMemberEmail().equals(memberEmail))
		 */
		if (member != null) {
			// 존재한다

			String setfrom = "admin";
			String title = "handmakers 인증 코드"; // 제목
			String content = "인증 코드는 " + Integer.toString(ran) + " 입니다. 인증 코드란에 입력해주세요."; // 인증 코드
			emailCode = Integer.toString(ran);
			try {
				MimeMessage message = mailSender.createMimeMessage();
				MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

				messageHelper.setFrom(setfrom); // 보내는사람 생략하면 정상작동을 안함
				messageHelper.setTo(memberEmail); // 받는사람 이메일
				messageHelper.setSubject(title); // 메일제목은 생략이 가능하다
				messageHelper.setText(content); // 메일 내용

				mailSender.send(message);
			} catch (Exception e) {
				System.out.println(e);
			}
			memberState = "T";
			mv.addObject("randomNumber", emailCode);
			mv.addObject("memberState", memberState);
			mv.setViewName("jsonView");

			return mv;
		} else {

			mv.addObject("memberState", memberState);
			mv.setViewName("jsonView");

			return mv;
		}

	}

	@RequestMapping("/member/checkId.do")
	/*
	 * public void checkId(String userId,HttpServletResponse response) throws
	 * IOException { logger.debug("중복체크"); boolean
	 * isId=service.checkId(userId)==0?false:true; response.getWriter().print(isId);
	 * }
	 */
	public ModelAndView checkId(String memberId, ModelAndView mv) throws UnsupportedEncodingException {
		Map map = new HashMap();
		boolean isId = service.checkId(memberId) == 0 ? false : true;
		map.put("isId", isId);

		mv.addAllObjects(map);
		mv.setViewName("jsonView");

		return mv;
	}

	@RequestMapping("/member/checkEmail.do")
	public ModelAndView checkEmail(String memberEmail, ModelAndView mv) throws UnsupportedEncodingException {
		Map map = new HashMap();
		boolean isEmail = service.checkEmail(memberEmail) == 0 ? false : true;
		map.put("isEmail", isEmail);

		mv.addAllObjects(map);
		mv.setViewName("jsonView");

		return mv;
	}

	@RequestMapping("/member/memberEnroll.do")
	public String memberEnroll() {
		return "member/memberEnroll";
	}

	@RequestMapping("/member/memberEnrollEnd.do")
	public String memberEnrollEnd(Member m, Model model) {

		System.out.println("m이다" + m);
		String rawPw = m.getMemberPwd();
		System.out.println("암호화전 " + rawPw);
		// System.out.println("암호화후 "+pwEncoder.encode(rawPw));
		m.setMemberPwd(pwEncoder.encode(rawPw));
		int result = service.memberEnroll(m);
		String msg = "";
		String loc = "/";
		if (result > 0) {
			msg = "회원가입을 성공하였습니다.";
		} else {
			msg = "회원가입 실패하였습니다.";
		}
		model.addAttribute("msg", msg);
		model.addAttribute("loc", loc);
		return "common/msg";

	}

	@RequestMapping("/member/memberLogin.do")
	public String memberLogin() {
		return "member/memberLogin";
	}

	@RequestMapping("/member/memberLoginEnd.do")
	// public String login(String userId, String password, Model model)
	public ModelAndView memberLoginEnd(Member m) {
		ModelAndView mv = new ModelAndView();
		System.out.println("m : " + m);
		logger.debug("로그인 제대로 들어옴!");
		logger.debug("파라미터확인 memberId : " + m.getMemberId() + " memberPwd : " + m.getMemberPwd());

		Member result = service.memberLogin(m.getMemberId());
		logger.debug("로그인결과 : " + result);
		String msg = "";
		String loc = "/";
		if (result != null && result.getMemberState().equals("1")) {
			if (pwEncoder.matches(m.getMemberPwd(), result.getMemberPwd())) {
				msg = "로그인성공";

				mv.addObject("member", result);
			} else {
				msg = "패스워드가 일치하지 않습니다.";
				loc = "/member/memberLogin.do";
			}
		} else {
			msg = "아이디가 존재하지 않습니다.";
			loc = "/member/memberLogin.do";
		}

		mv.addObject("msg", msg);
		mv.addObject("loc", loc);
		mv.setViewName("common/msg");

		return mv;
	}

	@RequestMapping("/member/memberLogout.do")
	public String memberLogout(SessionStatus status) {
		// SessionAttributes등록 로그인이면 : sessionStatus객체의
		// setComplete()매소드 이용로그아웃 처리
		// HttpSession이용 로그인이면 : HttpSession.invalide();
		if (!status.isComplete()) {
			status.setComplete();
		}
		return "redirect:/index.jsp";
	}

	@RequestMapping("/member/myPage.do")
	public ModelAndView memberMyPage(HttpSession session) {
		// 판매회원일 경우 등록된 브랜드 목록 가져옴
		String memberNo = ((Member) session.getAttribute("member")).getMemberNo();
		ModelAndView mv = new ModelAndView();

		mv.setViewName("member/myPage");
		return mv;
	}

	@RequestMapping("/member/adminPage.do")
	public String adminMyPage() {
		return "member/adminPage";
	}


	// member/memberUpdateEnd.do
	@RequestMapping("/member/memberUpdate.do")
	public String memberUpdate() {
		return "member/memberInfo";
	}

	@RequestMapping("/member/memberUpdateEnd.do")
	public ModelAndView memberUpdateEnd(Member m) {
		ModelAndView mv = new ModelAndView();
		System.out.println("m : " + m);
		logger.debug("회원정보 수정");

		int result = service.memberUpdate(m);

		String msg = "";
		String loc = "/";
		if (result > 0) {
			msg = "회원정보 수정이 완료되었습니다.";
			loc = "/member/memberUpdate.do";

		} else {
			msg = "회원정보 수정에 실패하였습니다.";
			loc = "/member/memberUpdate.do";
		}

		mv.addObject("msg", msg);
		mv.addObject("loc", loc);
		mv.setViewName("common/msg");

		return mv;
	}


	@RequestMapping("/member/memberWithdrawal.do")
	public String memberWithdrawal() {
		return "member/memberWithdrawal";
	}

	@RequestMapping("/member/checkPasswordEnd.do")
	public ModelAndView checkPasswordEnd(int checkNo, Member m) {
		logger.debug("회원탈퇴로 들어옴");
		ModelAndView mv = new ModelAndView();
		Member result = service.memberLogin(m.getMemberId());
		String memberId = m.getMemberId();
		String msg = "";
		String loc = "/";
		if (result != null) {
			if (pwEncoder.matches(m.getMemberPwd(), result.getMemberPwd())) {
				msg = "패스워드가 일치합니다.";
				if (checkNo == 0) {
					loc = "/member/memberWithdrawalEnd.do";
				} else if (checkNo == 1) {
					loc = "/member/changePassword.do";
				} else if (checkNo == 2) {
					loc = "/member/memberUpdate.do";
				} else if (checkNo == 3) {
					loc = "/member/enrollLocationView.do";
				}

			} else {
				msg = "패스워드가 일치하지 않습니다.";
				loc = "/member/checkPassword.do?checkNo=" + checkNo;
			}
		} else {
			msg = "패스워드가 일치하지 않습니다.";
			loc = "/member/checkPassword.do?checkNo=" + checkNo;
		}
		mv.addObject("msg", msg);
		mv.addObject("loc", loc);
		mv.setViewName("common/msg");

		return mv;
	}

	@RequestMapping("/member/memberWithdrawalEnd.do")
	public String memberWithdrawalEnd() {
		return "member/memberWithdrawalEnd";
	}

	@RequestMapping("/member/memberWithdrawalEnds.do")
	public ModelAndView memberWithdrawalEnds(String memberNo, SessionStatus status) {
		logger.debug("회원탈퇴 대상 : " + memberNo);
		ModelAndView mv = new ModelAndView();

		int result = service.memberWithdrawal(memberNo);
		String msg = "";
		String loc = "/";

		mv.addObject("msg", msg);
		mv.addObject("loc", loc);
		mv.setViewName("common/msg");

		return mv;
	}

	@RequestMapping("/member/checkPassword.do")
	public ModelAndView checkPassword(int checkNo) {
		ModelAndView mv = new ModelAndView();
		logger.debug("체크NO: " + checkNo);
		mv.addObject("checkNo", checkNo);
		mv.setViewName("/member/checkPassword");

		return mv;
	}

	@RequestMapping("/member/changePassword.do")
	public String changePassword() {
		return "member/changePassword";
	}

	@RequestMapping("/member/newPwdChange.do")
	public ModelAndView newPwdChange(String newPwd, String memberId) {
		ModelAndView mv = new ModelAndView();
		logger.debug("패스워드 변경");
		logger.debug("memberId: " + memberId);
		logger.debug("newPwd: " + newPwd);
		String newChangedPwd = pwEncoder.encode(newPwd);
		String memberState = "F";
		Map<String, String> cP = new HashMap<String, String>();
		cP.put("memberId", memberId);
		cP.put("newChangedPwd", newChangedPwd);

		int result = service.memberNewPwdUpdate(cP);

		if (result > 0) {
			memberState = "T";
			mv.addObject("memberState", memberState);
			mv.setViewName("jsonView");
			return mv;
		} else {

			mv.addObject("memberState", memberState);
			mv.setViewName("jsonView");
			return mv;
		}

	}

	@RequestMapping("/member/changePasswordEnd.do")
	public ModelAndView changePasswordEnd(Member m) {
		ModelAndView mv = new ModelAndView();
		logger.debug("패스워드 변경");
		logger.debug("Member: " + m);
		m.setMemberPwd(pwEncoder.encode(m.getMemberPwd()));
		int result = service.memberPwdUpdate(m);

		String msg = "";
		String loc = "/";
		if (result > 0) {
			msg = "비밀번호 변경이 완료되었습니다.";
			loc = "/member/myPage.do";
		} else {
			msg = "비밀번호 변경에 실패했습니다.";
			loc = "/member/changePassword.do";
		}

		mv.addObject("msg", msg);
		mv.addObject("loc", loc);
		mv.setViewName("common/msg");

		return mv;
	}

	

	@RequestMapping("/member/searchId.do")
	public String searchId() {
		return "member/findMemberId";
	}

	@RequestMapping("/member/searchPassword.do")
	public String searchPassword() {
		return "member/searchPassword";
	}

}

'신입사원 입문교육_정리' 카테고리의 다른 글

project_ login  (0) 2021.02.21
project_enroll  (0) 2021.02.21
web & spring 정리  (0) 2021.02.16
Spring Framework - Day2  (0) 2021.02.08
Spring Framework - Day1  (0) 2021.02.02