package com.order.action;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.order.common.MailUtil;
import com.order.common.NextPage;
import com.order.common.QueryResult;
import com.order.dao.UserDao;
import com.order.entity.User;
@Controller @Scope("prototype")
public class UserAction {
@Resource private UserDao userDao;
@Resource private NextPage nextPage;
@Resource private MailUtil mailUtil;
private User user;
//验证码显示
public void Code() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
BufferedImage imgBox = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);// 宽高,图片类型
Graphics g = imgBox.getGraphics();// 画图片
Color bgColor = new Color(238, 238, 238);//背景颜色
g.setColor(bgColor);// 把颜色给对象
g.fillRect(0, 0, 68, 22);// 背景框
// 图片内的内容
char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWSYZ0123456789".toCharArray();
Random r = new Random();
int len = ch.length,index;
StringBuffer str = new StringBuffer();
//产生4个随机字母或数字
for (int i = 0; i < 4; i++) {
index = r.nextInt(len);//获取[0,len)的随机数
g.setColor(new Color(r.nextInt(88), r.nextInt(88), r.nextInt(255)));
g.drawString(ch[index] + "", (i * 18) + 3, 18);
str.append(ch[index]);
}
//绘制3条干扰线
for(int j=0;j<3;j++){
g.setColor(new Color(r.nextInt(141)+80, r.nextInt(141)+80, r.nextInt(141)+80));
g.drawLine(r.nextInt(67)+1, r.nextInt(21)+1, r.nextInt(67)+1, r.nextInt(21)+1);
}
request.getSession().setAttribute("codes", str.toString());//验证码保存在session中
ImageIO.write(imgBox, "JPG", response.getOutputStream());// 输出
}
//用户登录部分
public void login() throws IOException{
//先判断验证码是否正确
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
//设置返回json数据,否则中文将会乱码
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
Map<String,String> msg = new HashMap<String,String>();
String code = request.getParameter("user_code").toUpperCase();
String codes = (String) request.getSession().getAttribute("codes");
String status = "";
String message = "";
if(code.equals(codes)){
//根据用户输入的user的部分信息对user对象进行查找
User u = userDao.userLogin(user);//传入的是用户名和密码
if(u!=null){
Boolean b = userDao.mailActive(u.getUser_id());//邮箱激活状态
if(b){
ActionContext ac = ActionContext.getContext();
ac.getSession().put("user_sex", u.getUser_sex());
ac.getSession().put("user_mail", u.getUser_mail());
ac.getSession().put("user_realname",u.getUser_realname());
ac.getSession().put("user_name", u.getUser_name());
ac.getSession().put("user_id", u.getUser_id());
ac.getSession().put("user_flag", u.getUser_flag());
ac.getSession().put("user_head", u.getUser_head());
status = "true";
msg.put("status", status);
}else{
status = "false";
message = "对不起,您的邮箱未激活!不能登录。";
msg.put("status", status);
msg.put("message", message);
}
}else{
status = "false";
message = "用户名或密码错误!";
msg.put("status", status);
msg.put("message", message);
}
}else{
status = "false";
message = "验证码错误!";
msg.put("status", status);
msg.put("message", message);
}
out.println(new Gson().toJson(msg));
}
//用户退出
public String exit(){
ActionContext ac = ActionContext.getContext();
ac.getSession().remove("user_name");
return "exit";
}
//用户注册部分
public void userRegister() throws IOException, MessagingException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
Map<String,String> msg = new HashMap<String,String>();
String status = "";
String message = "";
//判断用户是否已经存在
if(userDao.judgeUser(user.getUser_name())){
if(userDao.userMail(user.getUser_mail())==null){
Calendar now = Calendar.getInstance();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time = fmt.format(now.getTime());
user.setRegistTime(time);//设置用户的注册时间
user.setUser_flag("0");//默认设置为普通用户
user.setUser_head("user_pic.jpeg");//设置用户默认的头像
userDao.save(user);
//把激活地址发送给用户填写的邮箱
StringBuffer sb=new StringBuffer("请点击下面链接激活账号!</br>");
sb.append("<a href=\"http://localhost:8080/OrderSystem/order/user_mailValidate?name=");
sb.append(user.getUser_name());
sb.append("\">");
sb.append("http://localhost:8080/OrderSystem/order/user_mailValidate?name=");
sb.append(user.getUser_name());
sb.append("</a>");
mailUtil.send_mail(user.getUser_mail(), sb.toString());
status = "true";
message = "注册成功,请激活您的邮箱!";
msg.put("status", status);
msg.put("message", message);
}else{
status = "false";
message = "该邮箱已被注册!";
msg.put("status", status);
msg.put("message", message);
}
}else{
status = "false";
message = "用户名已存在!请重新输入。";
msg.put("status", status);
msg.put("message", message);
}
out.println(new Gson().toJson(msg));
}
//用户注册时邮箱激活
public String mailValidate(){
HttpServletRequest request = ServletActionContext.getRequest();
String username = request.getParameter("name");
User use = userDao.findByName(username);
use.setStatus("1");//邮箱设为已激活状态
userDao.update(use);
String message = "感谢您注册乐卖,您的邮箱已激活";
request.getSession().setAttribute("message", message);
return "activeResult";
}
//用户通过邮箱找回用户名
public void findUserName() throws IOException, MessagingException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
Map<String,String> msg = new HashMap<String,String>();
HttpServletRequest request = ServletActionContext.getRequest();
String mail = request.getParameter("mail");
String validateCode = request.getParameter("validateCode").toUpperCase();
String sessionCode = (String) request.getSession().getAttribute("codes");
String status = "";
String message = "";
User user = userDao.userMail(mail);
if(validateCode.equals(sessionCode)){
if(user!=null){
//用户名发送至邮箱
StringBuffer sb = new StringBuffer();
sb.append("您的用户名是:<br/>");
sb.append(user.getUser_name());
mailUtil.send_mail(mail, sb.toString());
status = "success";
message = "用户名已发送至邮箱!";
msg.put("s