Commit 0e0eadd3 authored by 王源's avatar 王源 🎧

更新readme.md

parent b02b2c17
.idea
.env
/vendor
composer.lock
Copyright (c) 2006-2021 meibuyu library
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Meibuyu Library
美不语微服务官方接口库
### 1、如何使用
在使用的项目下的composer.json 加入以下内容
```
"repositories": {
"meibuyu/micro": {
"type": "path",
"url": "path/to/micro",//本库的具体地址,随意找个地方git clone下来
"options": {
"symlink": true
}
},
}
```
然后在使用的项目下执行
```
composer require meibuyu/micro @dev
```
# Meibuyu Rpc Library
美不语微服务RPC接口库
## 1、安装
详细版本请看[https://packagist.org/packages/meibuyu/rpc](https://packagist.org/packages/meibuyu/rpc)
```bash
// 开发时可使用开发版本 dev-xxxx
// 正式上线请打标签,使用正式vA.B.C版本
composer require meibuyu/rpc
```
---
### 2、鉴权注解使用方法
> 使用时必须接入用户服务
> 权限名会拼接env文件中的APP_NAME属性,请注意唯一性
> 所有权限必须存在于用户服务的权限表中,若不存在,请联系管理员添加权限
##### 1、@AutoPerm
在控制器头部添加@AutoPerm注解,为该控制器下所有的方法添加鉴权功能,生成的权限名为`蛇形控制名_蛇形方法名`
```
/**
* @AutoPerm()
*/
class UserInfoController {}
```
参数:
> 1. prefix, 前缀(字符串),默认为蛇形控制名(user_info)
> 2. exclude, 要排除的方法名(字符串数组),默认为空
```
/**
* @AutoPerm(prefix="user", exclude={"getUser"})
*/
class UserInfoController {}
```
## 2、使用Rpc
##### 2、@Perm
在控制器中的方法头部添加@Perm注解,为当前方法添加鉴权功能,生成权限名为`蛇形控制名_蛇形方法名`
```
/**
* @Perm()
*/
function getUser {}
```
参数:
> name, 前缀(字符串),默认为蛇形方法名(user)
```
/**
* @Perm("get_user")
*/
function getUser {}
```
### 3、对集合获取值、然后调用rpc方法获取数据后,重新赋值
#### 1)、获取值,设置值
```
$list = Task::with(['c'=>function($q){
$q->select(['d','y']);
}])->select(['c','v'])->->paginate(2);
/*
* 假设拿到的数据是这个
* $list = [['c' => ['d' => 4, 'y' => 9], 'v' => '5'], ['c'=>'','v'=>'8'], ['c' => ['d' => 6, 'y' => 10], 'v' => '7']];
*/
$user_ids = get_collection_values($list, 'c.d');
// 去RPC拿用户数据
$users = $this->userService->getByIdList($hello);
//重新赋值给列表
put_collection_values($list, $users, 'c.d', 'user', 'id');
/*
* 则新的数据如下
* $list = [['c' => ['d' => 4,'user'=>['id'=>4,'name'=>'张三'], 'y' => 9], 'v' => '5'],
['c'=>'','v'=>'8'],
['c' => ['d' => 6, ,'user'=>['id'=>6,'name'=>'王五']'y' => 10], 'v' => '7']];
*/
```
#### 2)、判断各种值和更新各种值
```
// 使用第一步获取的列表
### 1. 创建代理消费者类,在微服务项目config/autoload/services配置文件内进行配置
> 每个服务只需要配置一次,按需配置
> 详细配置可查看[hyperf自动创建代理消费者类文档](https://hyperf.wiki/2.1/#/zh-cn/json-rpc?id=%e8%87%aa%e5%8a%a8%e5%88%9b%e5%bb%ba%e4%bb%a3%e7%90%86%e6%b6%88%e8%b4%b9%e8%80%85%e7%b1%bb)
```php
<?php
declare(strict_types=1);
// askModel文件 新增获取值,和设置值方法
$registry = [
'protocol' => 'consul',
'address' => env('CONSUL_URI', 'http://consul:8500'),
];
* 动态判断
* @param $currentUserId
return [
'consumers' => [
[
// name 需与服务提供者的 name 属性相同,硬性规定name是接口名去掉Interface
'name' => 'UserService',
// 服务接口名, 即服务接口类
'service' => Meibuyu\Rpc\Service\Interfaces\User\UserServiceInterface::class,
// 从consul服务中心获取节点信息
'registry' => $registry,
]
]
];
```
### 2. 在代码中调用
```php
/**
* @Inject()
* 依赖注入用户服务类
* @var UserServiceInterface
*/
public function getCanDelete($currentUserId)
private $userService;
public function test()
{
$this->attributes['can_delete'] = $this->user_id == $currentUserId ? true : false;
// 调用用户服务get方法
$user = $this->userService->get(1);
}
```
---
/**新增time_show属性
* @return string 返回友好时间值
*/
public function getTimeShowAttribute()
## 3、创建Rpc
### 1. 新建Rpc服务接口
- 拉取本项目至本地,在对应src/Service/Interfaces文件夹中创建接口文件
- 文件以项目名分文件夹,文件名**必须**以Interface结束,且注意分配相关项目的命名空间
- 方法名**必须**写注释,参数和返回数据类型必须写,方便使用者使用
```php
<?php
namespace Meibuyu\Rpc\Service\Interfaces\Test;
interface TestServiceInterface
{
if ($this->status == 2) {
return '已完成';
}
$time = time();
if ($this->status === 0) {
$time = $time - strtotime($this->start_time);
return human_time($time) . "后开始";
}
if ($this->status === 1) {
$time = $time - strtotime($this->end_time);
if ($time > 0) {
return "超时" . human_time($time);
}
return human_time($time) . "后结束";
}
/**
* 测试接口
* @param int $id ID
* @param string $name 名称
* @return array
*/
public function test(int $type, string $number): array;
}
```
### 2. 为已有服务接口加入新方法/修改已有方法
- 在现有接口类文件上编写新方法,或者修改已有方法
- 方法名必须写注释,参数和返回数据类型必须写,方便使用者使用
### 3. 在服务提供方的项目代码中实现接口类方法
> 类名**必须**是接口类去除Interface后的名称
> 详细配置可查看[hyperf定义服务提供者文档](https://hyperf.wiki/2.1/#/zh-cn/json-rpc?id=%e5%ae%9a%e4%b9%89%e6%9c%8d%e5%8a%a1%e6%8f%90%e4%be%9b%e8%80%85)
```php
<?php
namespace App\Rpc;
/**
* 设置结束时间
* @param string $time
* @RpcService(name="TestService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="consul")
*/
public function setFinishAttribute($time = '')
class TestService implements TestServiceInterface
{
$this->attributes['finish'] = $time ? $time : today();
/**
* 测试接口
* @param int $id ID
* @param string $name 名称
* @return array
*/
public function test(int $type, string $number): array
{
// 实现方法
return $name . $id;
}
}
```
// TaskRepositoryEloquent 文件
// 获取列表方法
.. .....
$currentUserId = Auth::id();
foreach ($list->items() as $item) {
$item->getCanDelete($currentUserId);//结果会新增 can_delete属性
$item->time_show;//自动调用getTimeShowAttribute方法,并设置值
$item->finish=today();//自动调用setFinishAttribute方法,并设置值
}
return $list;//集合自动转数组,会带上新的三个属性,和分页数据
```
### 4、新方法说明
#### 1)、human_time 友好的显示时间
用法:
```
human_time(time()-strtotime('2020-06-06 12:12'));
//12分钟等等根据计算值自动显示时分秒,不足一分钟显示秒,不足一小时显示分
//不足一天显示小时
//可以自行测试
```
#### 2)、info 输出数据到控制台,支持任何数据,自动换行 方便测试
用法:
```
//支持多参数
info('aaa',[1,2,3],new stdClass(){$a=1;},collect([1,23,4]));
info(1);
---
### 4. 发布分支/发布版本
- 在接口类和方法实现后,提交代码至develop分支
- 服务提供者和服务使用者,同时更新项目的composer.json文件中meibuyu/rpc版本至开发版本dev-develop,进行测试
- 测试完成,提交合并请求至master分支,并让此项目维护者合并,并且打版本发布
- 服务提供者和服务使用者,同时更新项目的composer.json文件中meibuyu/rpc版本至正式版本
```base
// "meibuyu/rpc": "dev-master", // 开发版本
// "meibuyu/rpc": "~1.0.0", // 正式版本
// 更新指定包
composer update meibuyu/rpc
```
......@@ -10,6 +10,7 @@
}
],
"require": {
"php": ">=7.2"
},
"autoload": {
"psr-4": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment