studio451/yii2-json-rpc-2.0-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

studio451/yii2-json-rpc-2.0-api

Composer 安装命令:

composer require studio451/yii2-json-rpc-2.0-api

包简介

JSON RPC 2.0 for Yii2 API with CRUD+ actions

README 文档

README

Table of Contents

Features:

  1. CRUD API actions
  2. List API action
  3. DeleteAll API action
  4. CORS Support

Using

Easiest way to use in 4 steps:

  1. Install via composer

    in ./composer.json add into 'require' section

    "studio451/yii2-json-rpc-2.0-api": "1.*"

    and in console/terminal run

    composer update
  2. Use namespace in your controller for CRUD+ actions

    use \studio451\yii2jsonrpc2api\ActiveController;

    change extends class to

    class ModelController extends \studio451\yii2jsonrpc2api\ActiveController {
    
        public $modelClass = 'app\models\Model';    
        public $dataFilter = 'app\models\ModelFilter';
    
        //BODY or empty
        ~~~
    }
  3. Make json request to controller (used pretty urls without index.php). Request method MUST be POST and Content-type MUST be application/json.

CRUD+ API actions

To call the JSONRPC API, you can use this feature:

function sendToJsonrpc2(url, method, params, callback){
    
    let command = {"jsonrpc": "2.0", "id": createUUID(), "method": method, "params": params};

    command = JSON.stringify(command);

    let request = {
        type: "POST",
        dataType: 'JSON',
        url: '/jsonrpc2/v1/' + url + '?access-token=wKiPsMKbAm1a7Gg09rZ2qg28UWyWMiXN',
        contentType: "application/json",
        data: command
    };

    $.ajax(request).done(function (data) {
        if (callback)
        {
            callback(data);
        }
    });
}

function createUUID() {
    const fmt = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
    const rnd = Array.from(crypto.getRandomValues(new Uint8Array(32))).map(n => n & 0xf);
    const rk = (c, r) => ((c == 'x' ? r : (r & 0x3 | 0x8)).toString(16));
    return fmt.replace(/[xy]/g, c => rk(c, rnd.pop()));
}

Example Create action

request:

{
  "type": "POST",
  "dataType": "JSON",
  "url": "/jsonrpc2/v1/author",
  "contentType": "application/json",
  "data": {
    "jsonrpc": "2.0",
    "id": "6187d757-9a9b-469b-87d7-54b080304099",
    "method": "create",
    "params": {
      "firstName": "Ray",
      "secondName": "Bradbury"
    }
  }
}

and response will be:

{
  "jsonrpc": "2.0",
  "id": "6187d757-9a9b-469b-87d7-54b080304099",
  "method": "create",
  "timestamp": 1559548947,
  "result": {
    "firstName": "Ray",
    "secondName": "Bradbury",
    "id": 1,
    "books": []
  }
}

Example Update action

request:

{
  "type": "POST",
  "dataType": "JSON",
  "url": "/jsonrpc2/v1/author",
  "contentType": "application/json",
  "data": {
    "jsonrpc": "2.0",
    "id": "982f4f47-e72e-4b3c-90f5-f8422a5f90b1",
    "method": "update",
    "params": {
      "id": 1,
      "description": "Ray Douglas Bradbury"
    }
  }
}

and response will be:

{
  "jsonrpc": "2.0",
  "id": "982f4f47-e72e-4b3c-90f5-f8422a5f90b1",
  "method": "update",
  "timestamp": 1559548983,
  "result": {
    "id": 1,
    "firstName": "Ray",
    "secondName": "Bradbury",
    "description": "Ray Douglas Bradbury",
    "time": 0,
    "status": 1,
    "books": []
  }
}

Example View action

request:

{
  "type": "POST",
  "dataType": "JSON",
  "url": "/jsonrpc2/v1/author",
  "contentType": "application/json",
  "data": {
    "jsonrpc": "2.0",
    "id": "3670b63a-55d3-40a9-965e-7f61a72858b0",
    "method": "view",
    "params": {
      "id": 1
    }
  }
}

and response will be:

{
  "jsonrpc": "2.0",
  "id": "3670b63a-55d3-40a9-965e-7f61a72858b0",
  "method": "view",
  "timestamp": 1559549627,
  "result": {
    "id": 1,
    "firstName": "Ray",
    "secondName": "Bradbury",
    "description": null,
    "time": 0,
    "status": 1,
    "books": [
      {
        "id": 1,
        "id_author": 1,
        "title": "Fahrenheit 451",
        "description": null,
        "time": 0,
        "status": 1
      }
    ]
  }
}

Example Delete action

request:

{
  "type": "POST",
  "dataType": "JSON",
  "url": "/jsonrpc2/v1/author",
  "contentType": "application/json",
  "data": {
    "jsonrpc": "2.0",
    "id": "5e53b745-25f4-4039-87ab-e72acc5fd827",
    "method": "delete",
    "params": {
      "id": 1
    }
  }
}

and response will be:

{
  "jsonrpc": "2.0",
  "id": "5e53b745-25f4-4039-87ab-e72acc5fd827",
  "method": "delete",
  "timestamp": 1559549032,
  "result": true
}

Example DeleteAll action

request:

{
  "type": "POST",
  "dataType": "JSON",
  "url": "/jsonrpc2/v1/book",
  "contentType": "application/json",
  "data": {
    "jsonrpc": "2.0",
    "id": "cd340790-fce7-468b-a5dd-47f1e0a227c0",
    "method": "delete-all",
    "params": {}
  }
}

and response will be:

{
  "jsonrpc": "2.0",
  "id": "cd340790-fce7-468b-a5dd-47f1e0a227c0",
  "method": "delete-all",
  "timestamp": 1559550202,
  "result": true
}

List API action

Based on Yii2 ActiveDataProvider and used \studio451\yii2jsonrpc2api\ActiveController::dataFilter

Example List action

request:

{
  "type": "POST",
  "dataType": "JSON",
  "url": "/jsonrpc2/v1/book",
  "contentType": "application/json",
  "data": {
    "jsonrpc": "2.0",
    "id": "d159328b-7b55-4b3b-88d5-f97eafce13f9",
    "method": "list",
    "params": {
      "filter": {
        "and": [
          {
            "title": {
              "like": "Fah"
            }
          },
          {
            "id_author": 1
          }
        ]
      },
      "pagination": {
        "pageSize": 25
      },
      "sort": {
        "defaultOrder": {
          "id": 4
        }
      }
    }
  }
}

and response will be:

{
  "jsonrpc": "2.0",
  "id": "d159328b-7b55-4b3b-88d5-f97eafce13f9",
  "method": "list",
  "timestamp": 1559549743,
  "result": {
    "models": [
      {
        "id": 1,
        "id_author": 1,
        "title": "Fahrenheit 451",
        "description": null,
        "time": 0,
        "status": 1
      }
    ],
    "count": 1,
    "totalCount": 1
  }
}

More info:

Contacts

info@studio451.ru

Yii2

studio451/yii2-json-rpc-2.0-api 适用场景与选型建议

studio451/yii2-json-rpc-2.0-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.04k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rpc」 「json」 「crud」 「json-rpc」 「yii」 「jsonrpc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 studio451/yii2-json-rpc-2.0-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 studio451/yii2-json-rpc-2.0-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 studio451/yii2-json-rpc-2.0-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-06-03