restapi编写和调用方法

restapi编写和调用方法

调用方法

var ps = new FormData();
            ps["parentId"] = id;   //0为顶层菜单
            ps["type"] = 0;   //0目录,1菜单,2操作
            ps["name"] = "新菜单项";
            ps["url"] = '#';
            ps["perms"] = "";  //权限
            ps["icon"] = "";
            ps["orderNum"] = 1;
            //
            let para = Object.assign({}, ps);   //针对这种格式必须加的行 R save(@RequestBody MenuDO menuDO)
            //
            API_POST('/api-admin/menu', para).then(
                function (result) {
                    if (result && parseInt(result.code) === 0) {
                        ShowAlert("新增成功");
                        loadTreeMenuData();
                    }
                }, function (err) {
                    ShowAlert(err.toString());
                }).catch(function (err) {
                    ShowAlert(err.toString());
                });
this.btnDeleteMenuItem.onClick(function () {
            var id = tree.getValue();
            if (!id) {
                ShowAlert("请先选择一个要删除的菜单项");
                return;
            }
            ShowConfirm("请确认是否要删除当前选中的菜单项?",function(){
                var params=new FormData();
                params["id"]=id;   //{id: row.id}
                API_DELETE("/api-admin/menu",params)
                  .then(
                    function (result) {
                        if (result && parseInt(result.code) === 0) {
                            //ShowAlert("删除成功");
                            loadTreeMenuData();
                        }
                    }, function (err) {
                        ShowAlert(err.toString());
                    }
                ).catch(function (err) {
                    ShowAlert(err.toString());
                });
            });
        });
//当前登陆用户信息  OK
function API_tokenUser() {
    var tUserparams = new FormData();
    return API_GET('/api-admin/user/currentUser', tUserparams)
};
//获取用户编号id的详细信息  OK
function API_userInfoById(id) {
    var params = new FormData();
    params["id"] = id;
    return API_GET('/api-admin/user/'+id, params)
};
//获取菜单集合
function API_menus() {
    var params = new FormData();
    return API_GET('/api-admin/menu', params);
};
//获取当前用户有权访问的菜单集合
function API_currentUserMenus() {
    var params = new FormData();
    return API_GET('/api-admin/menu/currentUserMenus', params);
};
//axios模式
//vp:hsg
//create date:2019-10
var base = "http://localhost:8002";
axios.defaults.withCredentials = true;
//通用方法
function API_POST(url, params) {
    var tUrl=base+url;
    return axios.post(tUrl, params).then(res => res.data)
};

function API_GET(url, params)  {
    var tUrl=base+url;
    return axios.get(tUrl, {
        params: params
    }).then(res => res.data)
};

function API_PUT(url, params)  {
    var tUrl=base+url;
    return axios.put(tUrl, params).then(res => res.data)
};

function API_DELETE(url, params)  {
    var tUrl=base+url;
    return axios.delete(tUrl, {
        params: params
    }).then(res => res.data)
};

function API_PATCH(url, params) {
    var tUrl=base+url;
    return axios.patch(tUrl, params).then(res => res.data)
};
//------
//添加一个请求拦截器
axios.interceptors.request.use(
  config => {
      if (window.localStorage.getItem('access-token')) {
          config.headers.Authorization = window.localStorage.getItem('access-token');
      }      
      return config
  },
  error => {
      return Promise.reject(error)
  }
);
// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {    
    if (response.data && response.data.code) {
        if (parseInt(response.data.code) === 401) {
            //未登录
            //bus.$emit('goto', '/login')
            //未登陆跳转到登陆页面
            document.location.href = "login.html";
        }
    }
    return response;
}, function (error) {    
    // Do something with response error
    return Promise.reject(error);
});

restapi编写方法

package com.bootdo.clouddoadmin.controller;

import com.bootdo.clouddoadmin.domain.MenuDO;
import com.bootdo.clouddoadmin.domain.Tree;
import com.bootdo.clouddoadmin.service.MenuService;
import com.bootdo.clouddoadmin.utils.SecuityUtils;
import com.bootdo.clouddocommon.annotation.Log;
import com.bootdo.clouddocommon.context.FilterContextHandler;
import com.bootdo.clouddocommon.dto.MenuDTO;
import com.bootdo.clouddocommon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bootdo 1992lcg@163.com
 * @version V1.0
 */
@RequestMapping("/menu")
@RestController()
public class MenuController {
    @Autowired
    MenuService menuService;

    @Log("获取当前用户的菜单")
    @GetMapping("currentUserMenus")
    R currentUserMenus() {
        return R.ok().put("currentUserMenus",menuService.RouterDTOsByUserId(SecuityUtils.getCurrentUser().getId()));
    }

    @Log("访问菜单")
    @GetMapping("tree")
    Tree<MenuDO> tree() {
        return menuService.getTree();
    }

    @GetMapping
    List<Tree<MenuDO>> list() {
        return menuService.getTree().getChildren();
    }

    @GetMapping("{id}")
    MenuDO get(@PathVariable("id") Long id) {
        MenuDO menu = menuService.get(id);
        return menu;
    }

    @GetMapping("list")
    List<MenuDO> list(@RequestParam Map<String, Object> params) {
        List<MenuDO> menus = menuService.list(params);
        return menus;
    }
    //
    @PutMapping()
    R update(@RequestBody MenuDO menuDO) {
        if (menuService.update(menuDO) > 0) {
            return R.ok();
        }
        return R.error();
    }
    //
    @PostMapping
    R save(@RequestBody MenuDO menuDO) {
        return R.operate(menuService.save(menuDO) > 0);
    }

    @DeleteMapping()
    R remove(Long id) {
        if (menuService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("userMenus")
    List<MenuDTO> userMenus() {
        List<MenuDO> menuDOS = menuService.userMenus(Long.parseLong(FilterContextHandler.getUserID()));
        List<MenuDTO> menuDTOS = new ArrayList<>();
        for (MenuDO menuDO : menuDOS) {
            MenuDTO menuDTO = new MenuDTO();
            menuDTO.setMenuId(menuDO.getMenuId());
            menuDTO.setUrl(menuDO.getUrl());
            menuDTO.setPerms(menuDO.getPerms());
            menuDTOS.add(menuDTO);
        }
        return menuDTOS;
    }

    @GetMapping("clearCache")
    R clearCache() {
        Boolean flag = menuService.clearCache(Long.parseLong(FilterContextHandler.getUserID()));
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

//    /**
//     * 当前用户菜单的树形结构
//     *
//     * @return
//     */
//    @RequestMapping("/currentUserMenus")
//    List<Tree<MenuDO>> currentUserMenus() {
//        List<Tree<MenuDO>> menus = menuService.listMenuTree(Long.parseLong(FilterContextHandler.getUserID()));
//        return menus;
//    }

    @GetMapping("/roleId")
    List<Long> menuIdsByRoleId(Long roleId) {
        return menuService.MenuIdsByRoleId(roleId);
    }
}

public class MenuDO implements Serializable {
	private static final long serialVersionUID = 1L;
	
	//
	private String component;
	//
	private Long menuId;
	//父菜单ID,一级菜单为0
	private Long parentId;
	//菜单名称
	private String name;
	//菜单URL
	private String url;
	//授权(多个用逗号分隔,如:user:list,user:create)
	private String perms;
	//类型   0:目录   1:菜单   2:按钮
	private Integer type;
	//菜单图标
	private String icon;
	//排序
	private Integer orderNum;
	//创建时间
	private Date gmtCreate;
	//修改时间
	private Date gmtModified;
	//
	private String redirect;

	/**
	 * 设置:
	 */
	public void setComponent(String component) {
		this.component = component;
	}
	/**
	 * 获取:
	 */
	public String getComponent() {
		return component;
	}
	/**
	 * 设置:
	 */
	public void setMenuId(Long menuId) {
		this.menuId = menuId;
	}
	/**
	 * 获取:
	 */
	public Long getMenuId() {
		return menuId;
	}
	/**
	 * 设置:父菜单ID,一级菜单为0
	 */
	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	/**
	 * 获取:父菜单ID,一级菜单为0
	 */
	public Long getParentId() {
		return parentId;
	}
	/**
	 * 设置:菜单名称
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * 获取:菜单名称
	 */
	public String getName() {
		return name;
	}
	/**
	 * 设置:菜单URL
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	/**
	 * 获取:菜单URL
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * 设置:授权(多个用逗号分隔,如:user:list,user:create)
	 */
	public void setPerms(String perms) {
		this.perms = perms;
	}
	/**
	 * 获取:授权(多个用逗号分隔,如:user:list,user:create)
	 */
	public String getPerms() {
		return perms;
	}
	/**
	 * 设置:类型   0:目录   1:菜单   2:按钮
	 */
	public void setType(Integer type) {
		this.type = type;
	}
	/**
	 * 获取:类型   0:目录   1:菜单   2:按钮
	 */
	public Integer getType() {
		return type;
	}
	/**
	 * 设置:菜单图标
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}
	/**
	 * 获取:菜单图标
	 */
	public String getIcon() {
		return icon;
	}
	/**
	 * 设置:排序
	 */
	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}
	/**
	 * 获取:排序
	 */
	public Integer getOrderNum() {
		return orderNum;
	}
	/**
	 * 设置:创建时间
	 */
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}
	/**
	 * 获取:创建时间
	 */
	public Date getGmtCreate() {
		return gmtCreate;
	}
	/**
	 * 设置:修改时间
	 */
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}
	/**
	 * 获取:修改时间
	 */
	public Date getGmtModified() {
		return gmtModified;
	}
	/**
	 * 设置:
	 */
	public void setRedirect(String redirect) {
		this.redirect = redirect;
	}
	/**
	 * 获取:
	 */
	public String getRedirect() {
		return redirect;
	}
}

—the—end—

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页