目录

  • 说明

  • 速率和参数配置

  • Url访问权限

  • 登陆

  • 重置令牌

  • 获取用户列表

  • 获取用户详情

说明

控制器统一继承api\controllers\AController,如果不需要系统改写过的CURD,可直接继承common\controllers\ActiveController

速率和参数配置

可自行修改api/config/params.php

<?php
return [
    // api接口token有效期 默认2天
    'user.accessTokenExpire' => 2 * 24 * 3600,
    // token有效期是否验证 默认不验证
    'user.accessTokenValidity' => false,
    // 不需要token验证的方法
    'user.optional' => [
        'login',
    ],
    // 速度控制6秒内访问3次,注意,数组的第一个不要设置1,设置1会出问题,一定要大于2,譬如下面  6秒内只能访问三次
    'user.rateLimit' => [3, 6],
];

Url访问权限

注意在api/config/main.php的urlManager里添加规则,否则访问都是404

            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => [
                        /**
                         * 默认登录测试控制器
                         * http://当前域名/api/site/login
                         */
                        'site',
                        'room',
                        'file',
                        /** ------ 业务相关 ------ **/
                        'v1/default',
                        // ...继续添加你自己的控制器
                    ],
                    'pluralize' => false,// 是否启用复数形式,注意index的复数indices,开启后不直观
                    'extraPatterns' => [
                        'POST login' => 'login',// 登录获取token
                        'GET refresh' => 'refresh',// 重置token
                        'GET search' => 'search',// 测试查询
                        'POST upload-images' => 'upload-images', // 图片上传
                        'POST upload-videos' => 'upload-videos', // 视频上传
                        'POST upload-voices' => 'upload-voices', // 语音上传
                        'POST upload-files' => 'upload-files', // 文件上传
                        'POST upload-base64-img' => 'upload-base64-img', // base64上传 其他上传权限自己添加
                    ],
                ],
            ]

登陆

请求地址(Post)

http://当前域名/api/site/login

参数

参数名说明
username会员账号
password会员密码
group默认是1 代表的是会员 其他比如管理员登陆自行扩展

返回

{
    "code": 200,
    "message": "OK",
    "data": {
        "refresh_token": "c6W-VKjElGy7oqPGo1iP0V3BeZSftJc8",
        "access_token": "mLlHFjSSUltgYbo7f6dgqPcDLhC4ZXj1_1511409494",
        "expiration_time": 172800
         // ... 其他获取用户信息等可以自行扩展
    }
}

重置令牌

请求地址(Get)

http://当前域名/api/site/refresh?accessToken=[登陆获取到accessToken]

参数

参数名说明
refresh_token刷新令牌

返回

{
    "code": 200,
    "message": "OK",
    "data": {
        "refresh_token": "OmkwE4f2l40cD6w5MShMSNNCx1-lEdGI",
        "access_token": "xUvg4ab37IY3eluEJe1LJ3XY3l8rP9yv_1511410013",
        "expiration_time": 172800
    }
}

获取用户列表

请求地址(Get)

http://当前域名/api/v1/default?accessToken=[登陆获取到accessToken]

参数

返回

{
    "code": 200,
    "message": "OK",
    "data": [
        {
            "id": 2,
            "username": "manager",
            "password_hash": "$2y$13$76xaAS4Cy.UqasjdEah2rejsOZgZrpDav7U/cv.dvodctlVOoRa1i",
            "auth_key": "6b3PYZvg9SQxMAJDoNOQK6jGdJBVgHnJ",
            "password_reset_token": "jrLdVAeAWDrHDJ9iF2aOX_gVpneU4Ew9_1500472047",
            "wechat_openid": null,
            "type": 1,
            "nickname": null,
            "realname": null,
            "head_portrait": null,
            "sex": 1,
            "qq": null,
            "email": "1@qq.com",
            "birthday": null,
            "user_money": "0.00",
            "accumulate_money": "0.00",
            "frozen_money": "0.00",
            "user_integral": 0,
            "address_id": "0",
            "visit_count": 1,
            "home_phone": null,
            "mobile_phone": null,
            "passwd_question": null,
            "passwd_answer": null,
            "role": 10,
            "status": 10,
            "last_time": 1500430123,
            "last_ip": "127.0.0.1",
            "city": null,
            "province": null,
            "country": null,
            "updated_at": 1500472047,
            "created_at": 1500430122
        },
        {
            "id": 4,
            "username": "admin",
            "password_hash": "$2y$13$w4NLJHdeTyw/p.maHJxTMu8zqZLHBnjhxG0ZKB5XTxKbTt3SmT.NS",
            "auth_key": "Uu2nJVQGuIHzRXUC581oxRZx3GyUC828",
            "password_reset_token": null,
            "wechat_openid": null,
            "type": 1,
            "nickname": null,
            "realname": null,
            "head_portrait": null,
            "sex": 1,
            "qq": null,
            "email": "1@qq.com",
            "birthday": null,
            "user_money": "0.00",
            "accumulate_money": "0.00",
            "frozen_money": "0.00",
            "user_integral": 0,
            "address_id": "0",
            "visit_count": 1,
            "home_phone": null,
            "mobile_phone": null,
            "passwd_question": null,
            "passwd_answer": null,
            "role": 10,
            "status": 10,
            "last_time": 1505791237,
            "last_ip": "127.0.0.1",
            "city": null,
            "province": null,
            "country": null,
            "updated_at": 1505791237,
            "created_at": 1505791237
        }
    ]
}

获取用户详情

请求地址(Get)

http://当前域名/api/v1/default/[用户id]?accessToken=[登陆获取到accessToken]

参数

返回

{
    "code": 200,
    "message": "OK",
    "data": {
        "id": 2,
        "username": "manager",
        "password_hash": "$2y$13$76xaAS4Cy.UqasjdEah2rejsOZgZrpDav7U/cv.dvodctlVOoRa1i",
        "auth_key": "6b3PYZvg9SQxMAJDoNOQK6jGdJBVgHnJ",
        "password_reset_token": "jrLdVAeAWDrHDJ9iF2aOX_gVpneU4Ew9_1500472047",
        "wechat_openid": null,
        "type": 1,
        "nickname": null,
        "realname": null,
        "head_portrait": null,
        "sex": 1,
        "qq": null,
        "email": "751393839@qq.com",
        "birthday": null,
        "user_money": "0.00",
        "accumulate_money": "0.00",
        "frozen_money": "0.00",
        "user_integral": 0,
        "address_id": "0",
        "visit_count": 1,
        "home_phone": null,
        "mobile_phone": null,
        "passwd_question": null,
        "passwd_answer": null,
        "role": 10,
        "status": 10,
        "last_time": 1500430123,
        "last_ip": "61.174.30.170",
        "city": null,
        "province": null,
        "country": null,
        "updated_at": 1500472047,
        "created_at": 1500430122
    }
}

上次更新:2018-08-14