# 导航

Menu API 用于获取店铺的导航菜单，以及某个菜单内的层级菜单项。两个 endpoint 匿名访客都可以调用。

所有 Ajax API 请求应使用[多语言 URL](/zh-CN/docs/theme/references/ajax-api/#locale-aware-urls)，以便为访客提供一致的体验。

## 获取导航菜单列表

```text
GET /{locale}/api/front/menus
```

获取店铺的导航菜单列表，每个菜单都带层级菜单项。没有参数。

### 请求示例

**请求示例**

```javascript
fetch(window.SHOPLAZZA.routes.root + '/api/front/menus')
  .then((response) => response.json())
  .then((data) => {
    // do something...
  });
```

### 响应

**数据结构 | 示例**

- `data` object[]

  响应数据主体。

  *   Array \[

  - `id` string

    导航菜单的 ID。
  - `title` string

    导航菜单的名称。
  - `category` number
  - `children` object[]

    该导航项下的子项。

    *   Array \[

    - `id` number

      导航项的 ID。
    - `title` string

      导航项展示的文字。
    - `origin_type` number
    - `type` string

      导航项指向的资源类型，例如 `home`、`page`、`collection`。
    - `source_id` string

      导航项指向资源的 ID。
    - `url` string

      导航项指向的相对地址。
    - `pid` number

      父级导航项的 ID，`0` 表示顶级导航项。
    - `status` number
    - `disabled` number
    - `target` string

      导航项的打开方式，例如 `_blank` 表示新标签页打开。
    - `url_obj` object

      导航项的结构化链接信息。

      - `type` string

        导航项指向的资源类型，例如 `home`、`page`、`collection`。
      - `source_id` string

        导航项指向资源的 ID。
      - `url` string

        导航项指向的相对地址。
      - `title` string

        导航项展示的文字。
    - `expanded` boolean
    *   \]
  *   \]
- `msg` string

  响应返回的结果说明。
- `state` number

  请求结果标识，`success` 或 `0` 表示请求成功。

```json
{
  "data": [
    {
      "id": "header",
      "title": "Main menu",
      "category": 1,
      "children": [
        {
          "id": 83850217,
          "title": "Home",
          "origin_type": 15,
          "type": "home",
          "source_id": "0",
          "url": "/",
          "pid": 0,
          "status": 0,
          "disabled": 0,
          "target": "",
          "url_obj": {
            "type": "home",
            "source_id": "0",
            "url": "/",
            "title": ""
          },
          "expanded": true
        },
        {
          "id": 83850219,
          "title": "New Arrivals",
          "origin_type": 3,
          "type": "page",
          "source_id": "0",
          "url": "",
          "pid": 0,
          "status": 0,
          "disabled": 0,
          "target": "",
          "url_obj": {
            "type": "page",
            "source_id": "0",
            "url": "",
            "title": ""
          },
          "expanded": true,
          "children": [
            {
              "id": 83850221,
              "title": "Collection",
              "origin_type": 2,
              "type": "collection",
              "source_id": "0",
              "url": "",
              "pid": 83850219,
              "status": 0,
              "disabled": 0,
              "target": "",
              "url_obj": {
                "type": "collection",
                "source_id": "0",
                "url": "",
                "title": ""
              },
              "expanded": true
            }
          ]
        }
      ]
    }
  ],
  "msg": "请求成功",
  "state": 0
}
```

## 获取单个导航菜单

```text
GET /{locale}/api/front/menus/{menu_id}
```

获取单个导航菜单及其层级菜单项。`menu_id` 取菜单的 handle，例如 `header` 或 `footer`。

### 请求示例

**请求示例**

```javascript
fetch(window.SHOPLAZZA.routes.root + '/api/front/menus/header')
  .then((response) => response.json())
  .then((data) => {
    // do something...
  });
```

### 请求参数

- `menu_id` string

  菜单的 handle，例如 `header`。

### 响应

**数据结构 | 示例**

- `data` object

  响应数据主体。

  - `id` string

    导航菜单的 ID。
  - `title` string

    导航菜单的名称。
  - `category` number
  - `children` object[]

    该导航项下的子项。

    *   Array \[

    - `id` number

      导航项的 ID。
    - `title` string

      导航项展示的文字。
    - `origin_type` number
    - `type` string

      导航项指向的资源类型，例如 `home`、`page`、`collection`。
    - `source_id` string

      导航项指向资源的 ID。
    - `url` string

      导航项指向的相对地址。
    - `pid` number

      父级导航项的 ID，`0` 表示顶级导航项。
    - `status` number
    - `disabled` number
    - `target` string

      导航项的打开方式，例如 `_blank` 表示新标签页打开。
    - `url_obj` object

      导航项的结构化链接信息。

      - `type` string

        导航项指向的资源类型，例如 `home`、`page`、`collection`。
      - `source_id` string

        导航项指向资源的 ID。
      - `url` string

        导航项指向的相对地址。
      - `title` string

        导航项展示的文字。
    - `expanded` boolean
    *   \]
- `msg` string

  响应返回的结果说明。
- `state` number

  请求结果标识，`success` 或 `0` 表示请求成功。

```json
{
  "data": {
    "id": "header",
    "title": "Main menu",
    "category": 1,
    "children": [
      {
        "id": 83850217,
        "title": "Home",
        "origin_type": 15,
        "type": "home",
        "source_id": "0",
        "url": "/",
        "pid": 0,
        "status": 0,
        "disabled": 0,
        "target": "",
        "url_obj": {
          "type": "home",
          "source_id": "0",
          "url": "/",
          "title": ""
        },
        "expanded": true
      },
      {
        "id": 83850219,
        "title": "New Arrivals",
        "origin_type": 3,
        "type": "page",
        "source_id": "0",
        "url": "",
        "pid": 0,
        "status": 0,
        "disabled": 0,
        "target": "",
        "url_obj": {
          "type": "page",
          "source_id": "0",
          "url": "",
          "title": ""
        },
        "expanded": true,
        "children": [
          {
            "id": 83850221,
            "title": "Collection",
            "origin_type": 2,
            "type": "collection",
            "source_id": "0",
            "url": "",
            "pid": 83850219,
            "status": 0,
            "disabled": 0,
            "target": "",
            "url_obj": {
              "type": "collection",
              "source_id": "0",
              "url": "",
              "title": ""
            },
            "expanded": true
          }
        ]
      }
    ]
  },
  "msg": "请求成功",
  "state": 0
}
```
