Giter Site home page Giter Site logo

hyid's Introduction

hyid

hidden your id. 隐藏你的ID.

使用场景

当我们的应用需要提供一个无状态通过ID获取信息的接口,例如 GET /user/{id} ,此类接口只要递增ID进行请求,就可以得到我们数据库中所有公开信息,这很显然不是我们想看到的。 hyid 可以帮助你隐藏我们不希望用户看到的ID字段,或者其他数字字段。

安装

composer require 96qbhy/hyid

laravel or lumen

  1. 注册服务提供者 : Qbhy\Hyid\ServiceProvider::class
  2. 发布配置文件(lumen可以自行复制 config/hyid.php 或者安装 vendor:publish 命令): php artisan vendor:publish --provider=Qbhy\Hyid\ServiceProvider

使用

配置

HYID_SECRET=qbhy
HYID_OFFSET=1996
HYID_RANDOM_LENGTH=6

.env , HYID_RANDOM_LENGTH 值建议不超过 6

代码示例

class User extends Model{
    use Qbhy\Hyid\HyidAble;
    
    // or 
    public function getUserId($userId){
        return hyid($userId);
    }
    
    // or
    public function toArray(){
        $data = parent::toArray();

        $data['id'] = hyid()->encode($data['id'])
        
        return $data;
    }
}
// decode

解码后得到原始的ID

 public function userinfo($id){
        return User::query()->findOrFail(hyid()->decode($id))->toArray();
 }

UserController

非 laravel 或者 lumen 环境下使用

// 非 laravel or lumen 下,可以自行实例化 Hyid 类
$secret = 'qbhy';
$offset = 1996;
$hyid = new Hyid($secret,$offset,4);

$encodedId = $hyid->encode(1);
$id = $hyid->decode($encodedId); // 1

[email protected]
qbhy/hyid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.