jundayw/laravel-render-provider

laravel render provider

v3.0.3 2022-01-12 07:31 UTC

This package is auto-updated.

Last update: 2024-10-29 05:53:36 UTC


README

命令行下, 执行 composer 命令安装:

composer require jundayw/laravel-render-provider

authentication package that is simple and enjoyable to use.

对象方法

替换键值

replace(string $oldKey, string $newKey): $this

$this->replace('message','msg');

隐藏键值

hidden(mixed $hiddens): $this

$this->hidden('message');
$this->hidden('message','data');
$this->hidden(['message','data']);

移除键值

forget(mixed $forgets): $this

$this->forget('message');
$this->forget('message','data');
$this->forget(['message','data']);

追加数据

with(string $key, mixed $value): $this

$this->with('message','ok');

追加数据

withXxx($value): $this

$this->withMsg('ok');

重置对象

reset(): $this

$this->reset();

刷新对象及宏

flush(): $this

$this->flush();

批量赋值

data(array $data = [], bool $append = false): $this

$this->data(['message'=>'message','state'=>true]);

获取所有数据

all(bool $hidden = true): array

$this->all();
$this->all(true);   // 隐藏键值已过滤
$this->all(false);  // 隐藏键值未过滤

获取值

get(string $key): mixed

$this->get('message');

数据响应

response(?callable $response = null): mixed

$this->response();

JSON

json(?int $status = 200, ?array $headers = [], ?int $options = JSON_UNESCAPED_UNICODE): $this

$this->json();

JSONP

jsonp(?string $callback = 'jsonp', ?int $status = 200, ?array $headers = [], ?int $options = JSON_UNESCAPED_UNICODE): $this

$this->jsonp();

宏:内置成功

success(?string $message = 'SUCCESS', ?string $url = null, mixed $data = null): $this

$this->success();

宏:内置失败

error(?string $error = 'ERROR', ?string $url = null, mixed $errors = null): $this

$this->error();

macro($name, $macro): mixed

Render::macro('sign',function($name){
    return $this->with($name,md5(http_build_query($this->all())));
});
// 获取签名数据
$data = Render::reset()->success('ok')->sign('token')->all();
// 响应数据
return Render::success('ok')->sign('token')->response();

使用场景

开箱即用

return Render::success('ok', 'url...', 'data...')->response();
{
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "timestamp": "2022-01-10T06:04:29Z"
}
return Render::error('error', 'url...', 'data...')->response();
{
    "state": false,
    "error": "error",
    "url": "url...",
    "errors": "data...",
    "timestamp": "2022-01-10T06:03:50Z"
}

替换键值

将响应数据中键值 timestamp 替换为 time

return Render::success('success', 'url...', 'data...')
    ->replace('timestamp', 'time')
    ->response();
{
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "time": "2022-01-10T06:09:21Z"
}

移除键值

若响应数据中键值 timestamp、url 不需要,可将其移除

return Render::success('success', 'url...', 'data...')
    ->forget('timestamp', 'url')
    ->response();
{
    "state": true,
    "message": "ok",
    "data": "data..."
}

追加数据

若响应数据中需要新增字段,可使用 with 方法

return Render::success('success', 'url...', 'data...')
    ->with('appid', '...id...')
    ->with('appkey', '...key...')
    ->response();
{
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "timestamp": "2022-01-10T06:15:08Z",
    "appid": "...id...",
    "appkey": "...key..."
}

隐藏键值

若响应数据中需要对敏感数据进行处理,可使用 hidden 方法

return Render::success('success', 'url...', 'data...')
    ->with('appid', '...id...')
    ->with('appkey', '...key...')
    ->hidden('appkey')
    ->response();
{
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "timestamp": "2022-01-10T06:20:04Z",
    "appid": "...id..."
}

扩展签名

将响应数据及 appid、appkey 进行签名,并且响应数据中不显示 appkey 字段

Render::macro('sign', function($name) {
    $data = $this->all(false);// 获取所有数据包含隐藏字段 appkey
    return $this->with($name, md5(http_build_query($data)));// 数据签名方式可根据具体业务自定义
});
return Render::success('ok', 'url...', 'data...')
    ->with('appid', '...id...')
    ->with('appkey', '...key...')
    ->hidden('appkey')
    ->sign('token')
    ->response();
{
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "timestamp": "2022-01-10T06:30:18Z",
    "appid": "...id...",
    "token": "f6ef314a3c1acd6e80f6e3b1858b6778"
}

响应场景

默认响应数据格式 json

return Render::success('ok', 'url...', 'data...')
    ->json()
    ->response();
{
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "timestamp": "2022-01-10T06:37:26Z"
}

响应数据格式 jsonp

return Render::success('ok', 'url...', 'data...')
    ->jsonp()
    ->response();
jsonp({
    "state": true,
    "message": "ok",
    "url": "url...",
    "data": "data...",
    "timestamp": "2022-01-10T06:36:42Z"
});

扩展响应数据格式:宏方法扩展

Render::macro('format', function(callable $callable){
    $this->format = function($data) use ($callable){
        return $callable($data);
    };
    return $this;
});
return Render::success('ok', 'url...', 'data...')
    ->format(function($data){
        return var_export($data, true);// 根据响应格式实现具体方法即可
    })
    ->response();
array (
  'state' => true,
  'message' => 'ok',
  'url' => 'url...',
  'data' => 'data...',
  'timestamp' => '2022-01-10T06:49:45Z',
)

扩展响应数据格式:response 方法扩展

return Render::success('ok', 'url...', 'data...')
    ->response(function($data){
        return var_export($data, true);
    });
array (
  'state' => true,
  'message' => 'ok',
  'url' => 'url...',
  'data' => 'data...',
  'timestamp' => '2022-01-10T06:51:50Z',
)

宏场景

RenderFacade

应用包已扩展 success/error 方法,若不适用业务场景,可通过 Render::flush() 方法格式化后自行定义。

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Jundayw\LaravelRenderProvider\Support\Facades\Render;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Render::flush();
        Render::macro('success', function(?string $message = 'SUCCESS', ?string $url = null, mixed $data = null) {
            return $this->data([
                'state' => true,
                'message' => $message,
                'url' => $url,
                'data' => $data,
                'timestamp' => date('Y-m-d\TH:i:s\Z'),
            ], true);
        });
        Render::macro('error', function(?string $error = 'ERROR', ?string $url = null, mixed $errors = null) {
            return $this->data([
                'state' => false,
                'error' => $error,
                'url' => $url,
                'errors' => $errors,
                'timestamp' => date('Y-m-d\TH:i:s\Z'),
            ], true);
        });
    }
}

调用方式

return Render::reset()->success('ok', 'url...', 'data...')->with('code', 4)->response();
return Render::reset()->error('error', 'url...', 'data...')->with('code', 4)->response();

ResponseFacade

namespace App\Providers;

use Illuminate\Support\Facades\Response;
use Illuminate\Support\ServiceProvider;
use Jundayw\LaravelRenderProvider\Support\Facades\Render;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Response::macro('success', function(?string $message = 'SUCCESS', ?string $url = null, mixed $data = null) {
            return Render::reset()
                ->data([
                    'state' => true,
                    'message' => $message,
                    'url' => $url,
                    'data' => $data,
                    'timestamp' => date('Y-m-d\TH:i:s\Z'),
                ], true);
        });
        Response::macro('error', function(?string $error = 'ERROR', ?string $url = null, mixed $errors = null) {
            return Render::reset()
                ->data([
                    'state' => false,
                    'error' => $error,
                    'url' => $url,
                    'errors' => $errors,
                    'timestamp' => date('Y-m-d\TH:i:s\Z'),
                ], true);
        });
    }
}

调用方式

return response()->success('ok', 'url...', 'data...')->with('code', 4)->response();
return response()->error('error', 'url...', 'data...')->with('code', 4)->response();

其他场景

批量赋值场景

$data = [
    'state' => true,
    'message' => 'SUCCESS',
];
return Render::data($data)
    ->with('code', 200)
    ->response();
{
    "state": true,
    "message": "SUCCESS",
    "code": 200
}

链式操作优先级

为防止目标数据与预期数据不一致,推荐链式操作优先级:

取值场景

$render = Render::reset()       // 防止数据混淆
    ->data([], false)           // 批量覆盖模式
    //->success()->error()      // 方法优先级相同
    ->data([], true)            // 批量追加模式
    ->with('forget', 'forget')
    ->with('hidden', 'hidden')
    ->with('code', 200)
    ->forget('forget')->hidden('hidden')->replace('code', 'status');// 方法优先级相同

return $render->get('status');
return $render->all();
return $render
    ->json()->jsonp()           // 方法优先级相同
    ->response();

输出响应场景

return Render::reset()          // 防止数据混淆
    ->data([], false)           // 批量覆盖模式
    //->success()->error()      // 方法优先级相同
    ->data([], true)            // 批量追加模式
    ->with('forget', 'forget')
    ->with('hidden', 'hidden')
    ->with('code', 200)
    ->forget('forget')->hidden('hidden')->replace('code', 'status')// 方法优先级相同
    ->json()->jsonp()           // 方法优先级相同
    ->response();               // response 为防止数据混淆,内部已经调用 reset() 方法