React后台管理系统-后台接口封装
1新建文件夹 service ,里边建4个文件,分别是statistic-service.jsx 首页数据统计接口, user-service.jsx用户接口, product-service.jsx产品接口,order-service.jx订单接口

2.首页数据统计接口statistic-service
mm.jsx是封装的ajax请求,在上一边博客里边有讲到
- import MUtil from 'util/mm.jsx'
- const _mm = new MUtil();
- class Statistic{
- // 首页数据统计
- getHomeCount(){
- return _mm.request({
- url: '/manage/statistic/base_count.do'
- });
- }
- }
- export default Statistic;
3. 用户接口user-service
- import MUtil from 'util/mm.jsx'
- const _mm = new MUtil();
- class User{
- // 用户登录
- login(loginInfo){
- return _mm.request({
- type: 'post',
- url: '/manage/user/login.do',
- data: loginInfo
- });
- }
- // 检查登录接口的数据是不是合法
- checkLoginInfo(loginInfo){
- let username = $.trim(loginInfo.username),
- password = $.trim(loginInfo.password);
- // 判断用户名为空
- if(typeof username !== 'string' || username.length ===0){
- return {
- status: false,
- msg: '用户名不能为空!'
- }
- }
- // 判断密码为空
- if(typeof password !== 'string' || password.length ===0){
- return {
- status: false,
- msg: '密码不能为空!'
- }
- }
- return {
- status : true,
- msg : '验证通过'
- }
- }
- // 退出登录
- logout(){
- return _mm.request({
- type : 'post',
- url : '/user/logout.do'
- });
- }
- getUserList(pageNum){
- return _mm.request({
- type : 'post',
- url : '/manage/user/list.do',
- data : {
- pageNum : pageNum
- }
- });
- }
- }
- export default User;
4.产品接口product-service
- import MUtil from 'util/mm.jsx'
- const _mm = new MUtil();
- class Product{
- // 获取商品列表
- getProductList(listParam){
- let url = '',
- data = {};
- if(listParam.listType === 'list'){
- url = '/manage/product/list.do';
- data.pageNum = listParam.pageNum;
- }else if(listParam.listType === 'search'){
- url = '/manage/product/search.do';
- data.pageNum = listParam.pageNum;
- data[listParam.searchType] = listParam.keyword;
- }
- return _mm.request({
- type : 'post',
- url : url,
- data : data
- });
- }
- // 获取商品详情
- getProduct(productId){
- return _mm.request({
- type : 'post',
- url : '/manage/product/detail.do',
- data : {
- productId : productId || 0
- }
- });
- }
- // 检查保存商品的表单数据
- checkProduct(product){
- let result = {
- status: true,
- msg: '验证通过'
- };
- // 判断用户名为空
- if(typeof product.name !== 'string' || product.name.length ===0){
- return {
- status: false,
- msg: '商品名称不能为空!'
- }
- }
- // 判断描述不能为空
- if(typeof product.subtitle !== 'string' || product.subtitle.length ===0){
- return {
- status: false,
- msg: '商品描述不能为空!'
- }
- }
- // 验证品类ID
- if(typeof product.categoryId !== 'number' || !(product.categoryId > 0)){
- return {
- status: false,
- msg: '请选择商品品类!'
- }
- }
- // 判断商品价格为数字,且大于0
- if(typeof product.price !== 'number' || !(product.price >= 0)){
- return {
- status: false,
- msg: '请输入正确的商品价格!'
- }
- }
- // 判断库存为数字,且大于或等于0
- if(typeof product.stock !== 'number' || !(product.stock >= 0)){
- return {
- status: false,
- msg: '请输入正确的库存数量!'
- }
- }
- return result;
- }
- // 保存商品
- saveProduct(product){
- return _mm.request({
- type : 'post',
- url : '/manage/product/save.do',
- data : product
- });
- }
- // 变更商品销售状态
- setProductStatus(productInfo){
- return _mm.request({
- type : 'post',
- url : '/manage/product/set_sale_status.do',
- data : productInfo
- });
- }
- //查找一级品类列表
- getCategoryList(parentCategoryId){
- return _mm.request({
- type : 'post',
- url : '/manage/category/get_category.do',
- data : {
- //没有传的话默认值就是0
- categoryId : parentCategoryId || 0
- }
- });
- }
- // 新增品类
- saveCategory(category){
- return _mm.request({
- type : 'post',
- url : '/manage/category/add_category.do',
- data : category
- });
- }
- // 修改品类名称
- updateCategoryName(category){
- return _mm.request({
- type : 'post',
- url : '/manage/category/set_category_name.do',
- data : category
- });
- }
- }
- export default Product;
5.订单接口order-service.jx
- import MUtil from 'util/mm.jsx'
- const _mm = new MUtil();
- class Order{
- // 获取订单列表
- getOrderList(listParam){
- let url = '',
- data = {};
- if(listParam.listType === 'list'){
- url = '/manage/order/list.do';
- data.pageNum = listParam.pageNum;
- }else if(listParam.listType === 'search'){
- url = '/manage/order/search.do';
- data.pageNum = listParam.pageNum;
- data.orderNo = listParam.orderNo;
- }
- return _mm.request({
- type : 'post',
- url : url,
- data : data
- });
- }
- // 获取订单详情
- getOrderDetail(orderNumber){
- return _mm.request({
- type : 'post',
- url : '/manage/order/detail.do',
- data : {
- orderNo : orderNumber
- }
- });
- }
- sendGoods(orderNumber){
- return _mm.request({
- type : 'post',
- url : '/manage/order/send_goods.do',
- data : {
- orderNo : orderNumber
- }
- });
- }
- }
- export default Order;
6.解决跨域问题
在webpack.config里边 devserverr 里边的proxy配置即可解决

7.页面引入和使用
- import Statistic from 'service/statistic-service.jsx'
- const _statistic = new Statistic();
- _statistic.getHomeCount().then(res => {
- this.setState(res);
- }, errMsg => {
- _mm.errorTips(errMsg);
- });
React后台管理系统-后台接口封装的更多相关文章
- React后台管理系统-ajax请求封装
1.新建文件夹 util , 在util里边新建 mm.jsx文件 2.使用jquery里边的ajax发送请求,回调用promise,返回一个promise对象 request(param){ ...
- 使用react全家桶制作博客后台管理系统
前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基于react全家桶(React.React-r ...
- 使用React全家桶搭建一个后台管理系统
引子 学生时代为了掌握某个知识点会不断地做习题,做总结,步入岗位之后何尝不是一样呢?做业务就如同做习题,如果‘课后’适当地进行总结,必然更快地提升自己的水平. 由于公司采用的react+node的技术 ...
- 使用react全家桶制作博客后台管理系统 网站PWA升级 移动端常见问题处理 循序渐进学.Net Core Web Api开发系列【4】:前端访问WebApi [Abp 源码分析]四、模块配置 [Abp 源码分析]三、依赖注入
使用react全家桶制作博客后台管理系统 前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基 ...
- 《React后台管理系统实战 :一》:目录结构、引入antd、引入路由、写login页面、使用antd的form登录组件、form前台验证、高阶函数/组件
实战 上接,笔记:https://blog.csdn.net/u010132177/article/details/104150177 https://gitee.com/pasaulis/react ...
- React版/Vue版都齐了,开源一套【特别】的后台管理系统...
本项目主要基于Elux+Antd构建,包含React版本和Vue版本,旨在提供给大家一个简单基础.开箱即用的后台管理系统通用模版,主要包含运行环境.脚手架.代码风格.基本Layout.状态管理.路由管 ...
- react后台管理系统路由方案及react-router原理解析
最近做了一个后台管理系统主体框架是基于React进行开发的,因此系统的路由管理,选用了react-router(4.3.1)插件进行路由页面的管理配置. 实现原理剖析 1.hash的方式 ...
- 【共享单车】—— React后台管理系统开发手记:主页面架构设计
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- Vue + Element-ui实现后台管理系统(4)---封装一个ECharts组件的一点思路
封装一个ECharts组件的一点思路 有关后台管理系统之前写过三遍博客,看这篇之前最好先看下这三篇博客.另外这里只展示关键部分代码,项目代码放在github上: mall-manage-system ...
随机推荐
- POJ3274-Gold Balanced Lineup
题目链接:点击打开链接 Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16978 ...
- Python网络爬虫(三)
AJAX学习 AJAX=Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).通俗来说,AJAX是一种无需加载整个网页的情况下,通过在后台与服务器 ...
- jquery——整屏滚动
从这里下载了滚轮事件插件:https://github.com/jquery/jquery-mousewheel 函数节流:js中有些事件的触发频率非常高,在短时间内多次触发执行绑定函数,比如mous ...
- Hive 基本语法操练(四):Hive 复合类型
hive语法中主要提供了以下复合数据类型: 1)Structs: structs内部的数据可以通过DOT(.)来存取.例如,表中一列c的类型为STRUCT{a INT; b INT},我们可以通过c. ...
- eclipse配置android开发环境并搭建第一个helloWord工程
一.搭建Android在eclipse下环境 一.JDK(不用安装 下载地址: http://www.xp510.com/xiazai/Application/program/23625.ht ...
- Fiddler 抓包工具总结(转)
Fiddler 抓包工具总结 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Statistics 请求的性能数据分析 3). Inspectors 查看数据内容 4). Au ...
- ECMAScript 原始值和引用值
原始值和引用值 在ECMAScript中,变量可以存在两种类型的值,即原始值和引用值 原始值 存储
- vue-resource的使用
之前使用axios post请求不能向后台发送数据,然后使用了vue-resource这个插件 import Vue from 'vue' import VueResource from 'vue- ...
- pm2部署node应用
背景: 很早就知道了pm2的强大功能,部署,多进程部署,负载均衡等等,但是一直没有取尝试使用,每次写完代码就没关心部署的事了.最近有空就想着把pm2的部署流程走一遍,顺便整理出来. 环境: 1.本地: ...
- Centos install ICU, INTL for php
1. Install ICU from source wget http://download.icu-project.org/files/icu4c/56.1/icu4c-56_1-src.tgz ...