1. 程式人生 > >第一個SSM前端專案(七):完成郵件啟用與重發郵件操作

第一個SSM前端專案(七):完成郵件啟用與重發郵件操作

1)當註冊完成後出現的是如下頁面

2)因此在jsp頁面時,點選立即檢視郵件需要根據接收郵箱的不同歸屬而跳往不同的郵箱登入主頁

function lookEmail(message) {
        var arr = message.split(",");
        var email = arr[0];
        var opt = email.split("@")[1];
        if("qq.com"==opt){
            location.href = "https://mail.qq.com/";
        }else if("163.com"==opt){
                location.href = "https://mail.163.com/";
        }else if("162.com"==opt){
            location.href = "https://mail.162.com/";
        }else if("sina.com"==opt){
            location.href = "http://mail.sina.com.cn/";
        }else if("sohu"==opt){
            location.href = "https://mail.sohu.com";
        }
    }

 3)點選重新發送郵件,則需要跳轉後臺進行重新發送

function reSendEmail(message) {
       var arr = message.split(",");
       var email = arr[0];
       var code = arr[1];
        $.ajax({
            type:'post',
            url:'<%=basePath%>register/sendEmail',
            data: {"email":email,"validateCode":code},
            dataType:'json',
            success:function(data){
                var s = data["success"];
                if(s=="success"){
                     new $.zui.Messager('傳送成功!', {
                         type: 'success',
                         icon:'ok-sign',
                         actions: [{
                             name: 'close',
                            icon: 'remove'
                         }]
                     }).show()
                }
            }
        });
    }

   /**
     * 再次傳送郵件
     */
    @RequestMapping(value="/sendEmail")
    @ResponseBody
    public Object sendEmail(String email,String validateCode)
    {
        
        Map<String, Object> map=new HashMap<>();
        
        try
        {
            SendEmailUtil.sendEmail(email, validateCode);
            
            map.put("success", "success");
        }
        catch (Exception e)
        {
           
            e.printStackTrace();
        }
        
        return map;
        
    }

4)點選註冊頁面則需要跳轉到註冊

  function reRegist() {
        location.href = "<%=basePath%>register.jsp";
    }

5)當我們在郵箱中檢視啟用郵件時,可以明顯發現郵箱內容是一個超連結,我們檢視元素可以發現這是訪問專案的某個方法的連結,因此需要在後臺新增啟用郵件的一個方法

<a href="http://localhost:80/myProject/activeMail/[email protected]&vilidationCode=e26a848494d26a01a6d245e541ef6a26" target="_blank" rel="noopener">請於24小時內點選啟用</a>

6)方法如下:從redis中根據郵箱名稱獲取到驗證碼,檢測是否過期,若過期則刪除使用者同時提醒重新註冊,若已啟用則提醒去登入,若未啟用則進行啟用操作

package cn.com.gulu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.gulu.entity.User;
import cn.com.gulu.service.UserService;

/**
 * 啟用郵箱操作
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/activeMail")
public class ActiveMailController
{
    
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserService userService;
     
    
    /**
     * 啟用郵箱
     */
    @RequestMapping(value="/activeMail")
    public String activeMail(String email,String vilidationCode,Model model)
    {
        
        String redisCode = redisTemplate.opsForValue().get(email);
        
        User user=userService.findByEmail(email);
        
        if(redisCode==null)
        {
            // 已過期
            model.addAttribute("success", "您的啟用碼已過期,請重新註冊");

            userService.deleteByEmail(email);

            return "/regist/activeFail";  
        }else if(user.isIs_active())
        {
            
            // 已啟用
            model.addAttribute("success", "您已啟用,請直接登入!");
            return "../login";
            
        }else if(redisCode.equals(vilidationCode))
        {
            
           //啟用操作
            user.setIs_active(true);
            
            userService.edit(user);
            
            model.addAttribute( "email",email );
            
            return "/regist/activeSuccess";
        }else
        {
            //啟用碼錯誤
            model.addAttribute( "fail","您的啟用碼錯誤,請重新啟用!" );
            return "/regist/activeFail"; 
        }   
    }
}