package com.example.orderfood.service.serviceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.orderfood.commons.Email;
import com.example.orderfood.commons.JSONResult;
import com.example.orderfood.mapper.BusinessMapper;
import com.example.orderfood.mapper.UserMapper;
import com.example.orderfood.pojo.Business;
import com.example.orderfood.pojo.User;
import com.example.orderfood.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;
@Service
public class UserServiceImpl implements UserService {
@Autowired(required = false)
UserMapper userMapper;
@Autowired(required = false)
BusinessMapper businessMapper;
//验证邮箱格式
@Override
public JSONResult checkUserEmailService(String email) {
//对参数进行判断
if (email == null || "".equals(email)) {
return new JSONResult(201, "邮箱必填", null);
}
String regx = "[1-9]\\d{7,10}@qq\\.com";
boolean flag = email.matches(regx);
if (!flag) {
return new JSONResult(202, "邮箱格式不正确", null);
}
//判断邮箱是否重复
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.eq("user_email", email);
User user = userMapper.selectOne(queryWrapper);
if (user != null) {
return new JSONResult(203, "邮箱已经被注册", null);
}
return new JSONResult(200, "邮箱正确", null);
}
@Override
public JSONResult checkBusinessEmailService(String email) {
//对参数进行判断
if (email == null || "".equals(email)) {
return new JSONResult(201, "邮箱必填", null);
}
String regx = "[1-9]\\d{7,10}@qq\\.com";
boolean flag = email.matches(regx);
if (!flag) {
return new JSONResult(202, "邮箱格式不正确", null);
}
//判断邮箱是否重复
QueryWrapper<Business> queryWrapper = new QueryWrapper<Business>();
queryWrapper.eq("business_email", email);
Business business = businessMapper.selectOne(queryWrapper);
if (business != null) {
return new JSONResult(203, "邮箱已经被注册", null);
}
return new JSONResult(200, "邮箱正确", null);
}
//发送验证码
@Override
public JSONResult sendEmailCodeService(String email, Integer role, HttpSession session) {
if (role == 0) {//用户
//验证邮箱
JSONResult result = checkUserEmailService(email);
if (result.getState() != 200) {
return result;
}
//发送邮件
String emailCode = Email.sendEmail(email);
System.out.println("code" + emailCode);
//判断验证码是否为空
if (emailCode == null || "".equals(emailCode)) {
return new JSONResult(204, "网络异常,稍后重试", null);
}
//将生成的验证码保存起来
session.setAttribute("emailCode", emailCode);//20-30min
} else {//商家
//验证邮箱
JSONResult result = checkBusinessEmailService(email);
if (result.getState() != 200) {
return result;
}
//发送邮件
String emailCode = Email.sendEmail(email);
System.out.println("code" + emailCode);
//判断验证码是否为空
if (emailCode == null || "".equals(emailCode)) {
return new JSONResult(204, "网络异常,稍后重试", null);
}
//将生成的验证码保存起来
session.setAttribute("emailCode", emailCode);//20-30min
}
return new JSONResult(200, "发送成功", null);
}
//处理登录
@Override
public JSONResult doLoginService(String email, String password,
String code, Integer role, HttpSession session) {
if (role == 0) {
//参数判断
if (email == null || "".equals(email)) {
return new JSONResult(201, "登录邮箱不能为空", null);
}
//格式验证
String regx = "[1-9]\\d{7,10}@qq\\.com";
boolean flag = email.matches(regx);
if (flag != true) {
return new JSONResult(202, "邮箱格式错误", null);
}
//是否注册过
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_email", email);
User user = userMapper.selectOne(queryWrapper);
System.out.println(user.getUserPassword());
if (user == null) {
return new JSONResult(203, "该邮箱尚未注册", null);
}
//判断密码
if (password == null || "".equals(password)) {
return new JSONResult(204, "登录密码不能为空", null);
}
//检验密码
String salt = user.getSalt();
//加密
String md5Password = getMD5Password(password, salt);
//密码比对
/* if(!password1.equals(user.getPassword())||!email.equals(user.getEmail())){
return new JSONResult(205,"邮箱或密码输入错误,请核对",null);
}*/
if (!md5Password.equals(user.getUserPassword()) || !email.equals(user.getUserEmail())) {
return new JSONResult(205, "账号或密码错误", null);
}
//验证码
String loginCode = session.getAttribute("loginCode").toString();
if (loginCode == null) {
return new JSONResult(206, "验证码失效,请重新获取", null);
}
if (!loginCode.equals(code.toUpperCase())) {
return new JSONResult(207, "验证码输入有误", null);
}
//将用户信息绑定在session中,以便将来使用
session.setAttribute("user", user);
return new JSONResult(301, "登录成功", null);
} else {
//参数判断
if (email == null || "".equals(email)) {
return new JSONResult(201, "登录邮箱不能为空", null);
}
//格式验证
String regx = "[1-9]\\d{7,10}@qq\\.com";
boolean flag = email.matches(regx);
if (flag != true) {
return new JSONResult(202, "邮箱格式错误", null);
}
//是否注册过
QueryWrapper<Business> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("business_email", email);
Business business = businessMapper.selectOne(queryWrapper1);
if (business == null) {
return new JSONResult(203, "该邮箱尚未注册", null);
}
//判断密码
if (password == null || "".equals(password)) {
return new JSONResult(204, "登录密码不能为空", null);
}
//检验密码
String salt = business.getSalt();
//加密
String md5Password = getMD5Password(password, salt);
//密码比对
/* if(!password1.equals(user.getPassword())||!email.equals(user.getEmail())){
return new JSONResult(205,"邮箱或密码输入错误,请核对",null);
}*/
if (!md5Password.equals(business.getBusinessPassword())
评论0