7.3API design

7.3 API design

API文档

RESTful API文档

部分API正式说明

HOST: https://bobwu.cn/api/

查看餐厅菜单[GET]

应用场景

该接口返回某餐厅菜单的具体内容,商户和顾客都可以通过该接口主动获得菜单内容,完成下一步的业务逻辑。

需要调用接口的情况:

接口url

/menu/:{restaurant_id}

请求参数
字段名 变量名 必填 类型 示例值 描述
餐厅ID restaurant_id String abcd1234abcd1234 系统分配的餐厅唯一标识
返回结果

状态码200:成功

{
	"food_id": "888",
	"food_name": "chicken",
	"description": "nice, nice",
	"price": "88.8",
	"sales": "50"
}
字段名 变量名 必填 类型 示例值 描述
菜品ID food_id String 888 系统分配的菜品唯一标识
菜品名称 food_name String chicken 商家编辑的菜品名称
描述 description String nice, nice 商家编辑的菜品描述
价格 price Real 88.8 商家编辑的菜品价格
销量 sales Int 50 菜品本月的销量
示例代码
Vue+Axios
let axiosInstance = axios.create({
  baseURL: 'https://bobwu.cn/api',
});
// 获取id为abcd1234abcd1234的餐厅的菜单
axiosInstance.get('/menu/abcd1234abcd1234')
  .then(response => {
    console.log(response);
  })
  .catch(error => {
    console.log(error);
  });

小程序
let baseURL = 'https://bobwu.cn/api'
// 获取id为abcd1234abcd1234的餐厅的菜单
wx.request({
  url: baseURL + '/menu/abcd1234abcd1234',
  method: 'GET',
  success: response => {
    console.log(response);
  },
  fail: error => {
    console.log(error);
  },
  complete: ()=>{}
});
系统顺序图

提交订单[POST]

应用场景

该接口由小程序端顾客发起,向商家提交一个新的订单。

需要调用接口的情况:

接口url

/restaurant/orders?restaurant_id=?&table_id=?

请求参数

body:

{
	foods: [
		{
			"food_id": "111",
			"num": "1"
		},
		{
			"food_id":"222",
			"num":"2"
		}
	]
}
字段名 变量名 必填 类型 示例值 描述
餐厅ID restaurant_id String abcd1234 系统分配的餐厅唯一标识
餐桌ID table_id String 111 系统分配的餐桌标识
菜品列表 foods Object   需要提交的菜品及其数量的列表
菜品ID food_id String 222 系统分配的菜品标识
菜品数量 num Int 1 某种菜品的数量
返回结果

状态码201:成功

示例代码
小程序
let baseURL = 'https://bobwu.cn/api'
// 在餐桌id为111的顾客向id为abcd1234的餐厅提交订单
wx.request({
  url: baseURL + '/restaurant/orders?restaurant_id=abcd1234?&table_id=111',
  method: 'POST',
  data: {
    foods: [
		{
			"food_id": "111",
			"num": "1"
		},
		{
			"food_id":"222",
			"num":"2"
		}
	]
  },
  success: response => {
    console.log(response);
  },
  fail: error => {
    console.log(error);
  },
  complete: ()=>{}
});
系统顺序图