You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
test/service/loginService.java

167 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.itbaizhan.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import com.itbaizhan.dao.DB;
import com.itbaizhan.orm.TAdmin;
import com.itbaizhan.orm.Tcatelog;
import com.itbaizhan.orm.Tuser;
import com.itbaizhan.util.Cart;
public class loginService {
/**
* 用户登录方法
* 根据用户类型判断登录的身份,并验证账号密码。
* 登录成功后,将用户信息存入 session 中。
*
* @param userName 用户名
* @param userPw 密码
* @param userType 用户类型0管理员1会员2其他
* @return 登录结果,"yes" 表示登录成功,"no" 表示登录失败
*/
public String login(String userName, String userPw, int userType) {
String result = "no"; // 默认登录失败
if (userType == 0) { // 系统管理员登录
String sql = "select * from t_admin where userName=? and userPw=?";
Object[] params = { userName, userPw };
DB mydb = new DB();
mydb.doPstm(sql, params);
try {
ResultSet rs = mydb.getRs(); // 获取查询结果集
boolean mark = (rs == null || !rs.next() ? false : true); // 判断查询结果是否为空
if (!mark) {
result = "no"; // 登录失败
} else {
result = "yes"; // 登录成功
TAdmin admin = new TAdmin();
admin.setUserId(rs.getInt("userId"));
admin.setUserName(rs.getString("userName"));
admin.setUserPw(rs.getString("userPw"));
WebContext ctx = WebContextFactory.get(); // 获取 WebContext 对象
HttpSession session = ctx.getSession();
session.setAttribute("userType", 0); // 设置用户类型为管理员
session.setAttribute("admin", admin); // 将管理员信息存入 session
}
rs.close();
} catch (SQLException e) {
System.out.println("登录失败!");
e.printStackTrace();
} finally {
mydb.closed();
}
}
if (userType == 1) { // 会员登录
String sql = "select * from t_user where loginname=? and loginpw=? and del='no'";
Object[] params = { userName, userPw };
DB mydb = new DB();
try {
mydb.doPstm(sql, params); // 执行 SQL 查询
ResultSet rs = mydb.getRs(); // 获取查询结果集
boolean mark = (rs == null || !rs.next() ? false : true); // 判断查询结果是否为空
if (!mark) {
result = "no"; // 登录失败
} else {
result = "yes"; // 登录成功
Tuser user = new Tuser();
user.setId(rs.getString("id"));
user.setLoginname(rs.getString("loginname"));
user.setLoginpw(rs.getString("loginpw"));
user.setName(rs.getString("name"));
user.setDel(rs.getString("del"));
WebContext ctx = WebContextFactory.get(); // 获取 WebContext 对象
HttpSession session = ctx.getSession();
session.setAttribute("userType", 1); // 设置用户类型为会员
session.setAttribute("user", user); // 将用户信息存入 session
// 创建一个空的购物车对象,存入 session
Cart cart = new Cart();
session.setAttribute("cart", cart);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
mydb.closed();
}
if (userType == 2) {
// 用户类型为 2 的登录逻辑可以在这里添加(暂未实现)
}
return result; // 返回登录结果
}
/**
* 修改管理员密码
* 管理员修改密码功能。
*
* @param userPwNew 新密码
* @return 修改结果,"yes" 表示修改成功
*/
public String adminPwEdit(String userPwNew) {
System.out.println("DDDD");
try {
Thread.sleep(700); // 确保线程运行顺序
} catch (InterruptedException e) {
e.printStackTrace();
}
WebContext ctx = WebContextFactory.get(); // 获取 WebContext 对象
HttpSession session = ctx.getSession();
TAdmin admin = (TAdmin) session.getAttribute("admin"); // 获取当前登录的管理员对象
String sql = "update t_admin set userPw=? where userId=?";
Object[] params = { userPwNew, admin.getUserId() }; // 设置更新的参数
DB mydb = new DB();
mydb.doPstm(sql, params); // 执行更新操作
return "yes"; // 返回修改成功的结果
}
/**
* 获取所有菜品类别
* 查询所有未删除的菜品类别并返回结果。
*
* @return 菜品类别列表
*/
public List catelogAll() {
try {
Thread.sleep(700); // 确保线程运行顺序
} catch (InterruptedException e) {
e.printStackTrace();
}
List catelogList = new ArrayList(); // 初始化菜品类别列表
String sql = "select * from t_catelog where del='no'"; // 查询未删除的菜品类别
Object[] params = {};
DB mydb = new DB();
try {
mydb.doPstm(sql, params); // 执行查询
ResultSet rs = mydb.getRs(); // 获取查询结果集
while (rs.next()) { // 遍历结果集
Tcatelog catelog = new Tcatelog();
catelog.setId(rs.getString("id")); // 设置菜品类别 ID
catelog.setName(rs.getString("name")); // 设置菜品类别名称
catelogList.add(catelog); // 将菜品类别添加到列表
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
mydb.closed(); // 关闭数据库连接
return catelogList; // 返回菜品类别列表
}
}