一、注册
相关代码:
UserDao
//注册
public int add(User user) throws Exception {
String sql = "insert into t_easyui_user(name,pwd) values(?,?) ";
return super.executeUpdate(sql,user,new String[]{"name","pwd"});
}
UserAction
public String add(HttpServletRequest request, HttpServletResponse response){
try {
this.userDao.add(user);
} catch (Exception e) {
e.printStackTrace();
return "register";
}
return "login";
}
1、我们来到用户注册界面,注册一个账号为qqq,密码为qqq的账号
2、注册好账号之后,会跳转到登录界面
3、此时数据库里已经有这个账号啦
二、登录
相关代码:
UserDao
//登录
public User login(User user) throws Exception {
String name = user.getName();
String pwd = user.getPwd();
String sql = "select * from t_easyui_user where true ";
if (StringUtils.isNotBlank(name)) {
sql += " and name = '"+name+"'";
}
if (StringUtils.isNotBlank(pwd)) {
sql += " and pwd = '"+pwd+"'";
}
// List<User> list = super.executeQuery(sql, User.class,pageBean);
List<User> list=super.executeQuery(sql, User.class, null);
if (list.size() == 0) {
return null;
}
return list.get(0);
}
UserAction
private User user = new User();
private UserDao userDao = new UserDao();
@Override
public User getModel() {
return user;
}
public String login(HttpServletRequest request, HttpServletResponse response){
try {
User current = this.userDao.login(user);
if (current==null){
return "main";
}
request.getSession().setAttribute("currentUser",current);
} catch (Exception e) {
e.printStackTrace();
return "login";
}
return "main";
}
1、我们首先在数据库里找一个已有的账号
2、来到登录界面。我们输入一个已有的账号和密码
3、点击登录,跳转到了主界面
3、权限管理
相关代码:
PermissionDao
public List<Permission> permission(String pids,PageBean pageBean) throws Exception {
String sql="select * from t_easyui_Permission where id in("+pids+")";
return super.executeQuery(sql, Permission.class, pageBean);
}
public List<TreeVo<Permission>> topNodePermission(String pids,PageBean pageBean) throws Exception {
List<Permission> list = this.permission(pids, pageBean);
List<TreeVo<Permission>> nodes = new ArrayList<TreeVo<Permission>>();
TreeVo treeVo = null;
for (Permission p : list) {
treeVo = new TreeVo<>();
treeVo.setId(p.getId()+"");
treeVo.setText(p.getName());
treeVo.setParentId(p.getPid()+"");
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("self", p);
treeVo.setAttributes(attributes);
nodes.add(treeVo);
}
return BuildTree.buildList(nodes,"0");
}
RolePermissionDao
package com.xieying.dao;
import java.util.List;
import com.xieying.entity.RolePermission;
import com.xieying.util.BaseDao;
import com.xieying.util.PageBean;
public class RolePermissionDao extends BaseDao<RolePermission> {
// 获取角色对应的权限id
public List<RolePermission> list(RolePermission rp) throws Exception {
String sql = "select * from t_easyui_role_permission where true";
long rid=rp.getRid();
if(rid !=0) {
sql += " and rid ="+rid;
}
PageBean pageBean;
return super.executeQuery(sql, RolePermission.class, pageBean);
}
}
PermissionAction
private RolePermissionDao rpd = new RolePermissionDao();
// 不管是老板还是会员都是看到所有的菜单
// 希望:老板看到全部,会员看到部分
User current = (User) req.getSession().getAttribute("current");
RolePermission rp=new RolePermission();
rp.setRid(current.getType());
List<RolePermission> list = this.rpd.list(rp);
//
StringBuilder pids= new StringBuilder();
for (RolePermission rps : list) {
pids.append(",").append(rps.getPid());
}
List<TreeVo<Permission>> topNodePermission = this.pd.topNodePermission(pids.substring(1), null);
ResponseUtil.writeJSON(resp, topNodePermission);
结果是:
只有老板能看到全部的界面
而会员看不到订单管理