Skip to content

用户接口

类说明

Users 类提供用户信息管理相关的接口,包括获取用户信息、更新用户资料、上传封面和头像等功能。

使用说明:

  • 所有需要授权的接口都需要在请求头中携带 X-WP-NonceAuthorization: Bearer {token}
  • Nonce 可以从页面全局变量 ajax_object.wp_rest_nonce 获取
  • Token 通过登录接口获取

获取用户信息

Method : GET

URL : /wp-json/vtheme/v1/users/{id}

Auth required : False

URL Parameters :

  • id : [integer] 用户ID

Success Response

Code : 200 OK

Content example :

json
{
  "id": 1,
  "display_name": "张三",
  "description": "个人简介",
  "user_registered": "2023-01-01 12:00:00",
  "user_url": "",
  "avatar_url": "http://example.com/avatar.jpg",
  "cover_url": "http://example.com/cover.jpg",
  "post_count": 10,
  "follower_count": 100,
  "following_count": 50,
  "nickname": "张三"
}

注意:

  • 非本人访问时,只返回公开信息
  • 本人访问时,额外返回 nicknameuser_emailgendermobile 等敏感信息

Error Response

Code : 404 Not Found

Content example :

json
{
  "code": "user_not_found",
  "message": "User does not exist",
  "data": {
    "status": 404
  }
}

更新用户信息

Method : POST

URL : /wp-json/vtheme/v1/users/{id}

Auth required : True

Headers :

  • X-WP-Nonce:
  • Authorization: Bearer
  • Content-Type: application/json

URL Parameters :

  • id : [integer] 用户ID(必须是当前登录用户或管理员)

Body :

json
{
  "nickname": "新昵称",
  "email": "newemail@example.com",
  "description": "新的个人简介",
  "gender": "1",
  "mobile": "13800138000"
}

参数说明:

  • nickname : [string] 昵称(可选)
  • email : [string] 邮箱(可选,需符合邮箱格式)
  • description : [string] 个人简介(可选)
  • gender : [string] 性别,0=保密,1=男,2=女(可选)
  • mobile : [string] 手机号(可选)

Success Response

Code : 200 OK

Content example :

json
{
  "id": 1,
  "display_name": "新昵称",
  "nickname": "新昵称",
  "user_email": "newemail@example.com",
  "description": "新的个人简介",
  "gender": "1",
  "mobile": "13800138000"
}

Error Response

Code : 401 Unauthorized

Content example :

json
{
  "code": "unauthorized",
  "message": "No permission to modify this user",
  "data": {
    "status": 401
  }
}

Code : 403 Forbidden (CSRF验证失败)

Content example :

json
{
  "code": "invalid_nonce",
  "message": "CSRF verification failed",
  "data": {
    "status": 403
  }
}

Code : 404 Not Found

Content example :

json
{
  "code": "user_not_found",
  "message": "User does not exist",
  "data": {
    "status": 404
  }
}

用户上传封面

Method : POST

URL : /wp-json/vtheme/v1/users/cover/upload

Auth required : True

Headers :

  • Authorization: Bearer

Form Data :

  • image : [file] 图片文件 (JPG/PNG/GIF/WebP, 最大5MB)

Success Response

Code : 200 OK

Content example (启用审核) :

json
{
  "success": true,
  "message": "封面已提交,等待审核",
  "attachment_id": 123,
  "status": "pending"
}

Content example (未启用审核) :

json
{
  "success": true,
  "message": "封面上传成功",
  "attachment_id": 123,
  "status": "approved"
}

Error Response

Code : 400 Bad Request

Content example :

json
{
  "code": "pending_exists",
  "message": "You already have a cover pending review",
  "data": {
    "status": 400
  }
}

Code : 401 Unauthorized

Content example :

json
{
  "code": "unauthorized",
  "message": "Please login first",
  "data": {
    "status": 401
  }
}

获取用户封面

Method : GET

URL : /wp-json/vtheme/v1/users/{user_id}/cover

Auth required : False

URL Parameters :

  • user_id : [integer] 用户ID

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "url": "http://example.com/cover.jpg",
  "status": "approved"
}

status 可能的值:

  • approved : 已通过
  • pending : 待审核
  • rejected : 已拒绝
  • none : 无封面

管理员获取待审核封面列表

Method : GET

URL : /wp-json/vtheme/v1/admin/covers/pending

Auth required : True (需要管理员权限)

Query Parameters :

  • page : [integer] 页码,默认1
  • per_page : [integer] 每页数量,默认20

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "items": [
    {
      "user_id": 1,
      "user_name": "张三",
      "cover_url": "http://example.com/cover.jpg",
      "submitted_at": "2023-01-01 12:00:00"
    }
  ],
  "total": 10,
  "page": 1,
  "per_page": 20
}

Error Response

Code : 403 Forbidden

Content example :

json
{
  "code": "unauthorized",
  "message": "No permission",
  "data": {
    "status": 403
  }
}

管理员审核封面

Method : POST

URL : /wp-json/vtheme/v1/admin/covers/{user_id}/review

Auth required : True (需要管理员权限)

URL Parameters :

  • user_id : [integer] 用户ID

Body :

json
{
  "action": "approve"
}

参数说明:

  • action : [string] 操作类型,approve=通过,reject=拒绝

Success Response

Code : 200 OK

Content example (通过) :

json
{
  "success": true,
  "message": "审核通过"
}

Content example (拒绝) :

json
{
  "success": true,
  "message": "审核拒绝"
}

注意: 拒绝时会删除媒体库中的图片

Error Response

Code : 400 Bad Request

Content example :

json
{
  "code": "no_pending",
  "message": "No pending cover to review",
  "data": {
    "status": 400
  }
}

Code : 403 Forbidden

Content example :

json
{
  "code": "unauthorized",
  "message": "No permission",
  "data": {
    "status": 403
  }
}

管理员获取待审核头像列表

Method : GET

URL : /wp-json/vtheme/v1/admin/avatars/pending

Auth required : True (需要管理员权限)

Query Parameters :

  • page : [integer] 页码,默认1
  • per_page : [integer] 每页数量,默认20

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "items": [
    {
      "user_id": 1,
      "user_name": "张三",
      "avatar_url": "http://example.com/avatar.jpg",
      "submitted_at": "2023-01-01 12:00:00"
    }
  ],
  "total": 5,
  "page": 1,
  "per_page": 20
}

管理员审核头像

Method : POST

URL : /wp-json/vtheme/v1/admin/avatars/{user_id}/review

Auth required : True (需要管理员权限)

URL Parameters :

  • user_id : [integer] 用户ID

Body :

json
{
  "action": "approve"
}

参数说明:

  • action : [string] 操作类型,approve=通过,reject=拒绝

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "message": "审核通过"
}

注意: 拒绝时会删除媒体库中的图片