nice-yu/invite-code

Generate a globally unique invite-code code that does not repeat and can be decrypted

v1.2 2023-04-10 07:54 UTC

This package is auto-updated.

Last update: 2024-05-10 10:30:47 UTC


README

简体中文 | English

background

Now more and more business involves social e-commerce, or invites to share However, the invitation codes generated by many friends are still random, which leads to the need for multiple queries (Register to check duplicates, Register to use invitation codes to check information, etc.) So we specially designed the coding scheme of this invitation code User Id can be used for encoding, and the encoded string can also be used to push back Id

This is what I think:
  1. When the user registers, query the database to enter whether the mobile phone exists (normal operation, not less)
  2. After the mobile phone number verification is correct, store the user login information in the login table
  3. Then use the newly added data table of LoginForm to auto-increment Id to generate an invitation code

In this way, the invitation code will not be duplicated

At the same time, the invitation code will not be used as a query where

Because the invitation code can be pushed back to Id

Install

composer require nice-yu/invite-code

Unit Test Information

  • Unit tests with 100% coverage
Invite Code (NiceYu\Tests\InviteCode\InviteCode)
✔ Multiple separator letters [0.14 ms]
✔ Invite code max encode [36.23 ms]
✔ Invite code encode [0.17 ms]
✔ Invite code decode [0.13 ms]
✔ Invite code error capture [4.90 ms]
✔ Invitation code settings [1.67 ms]

Time: 00:00.061, Memory: 6.00 MB

Generate an invitation code

/** Just import */
$class = new \NiceYu\InviteCode\InviteCode();
$class->encode(1);

encryption and decryption

$class = new \NiceYu\InviteCode\InviteCode();

/** encryption */
$class->encode(1);

/** decryption */
$class->decode($app->encode(1));

Change the digits of generated invitation code

  1. By default, it is six digits
  2. Also removed O 0 I 1 Y Z
  3. After removing the wrong characters that are easy to distinguish with the naked eye, (26 + 10) - 6 = 30 bit
  4. Normally:
    30^6 power = 1 billion times
    We can get different 729 million invite codes
$class = new \NiceYu\InviteCode\InviteCode();
$class->encode(729000000);

Modify the configuration of the invitation code

  1. default the following configuration
  2. You can mess up the dictionary, then the meaning of each character will change
  3. Note: Do not modify the dictionary midway, or you need to update it in full
$max = 6;
$complement = array('Y', 'Z');
$dictionaries = array(
    '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
    'S', 'T', 'U', 'V', 'W', 'X'
);

$class = new \NiceYu\InviteCode\InviteCode();
$class->setMax($max)
      ->setComplement($complement)
      >setDictionaries($dictionaries);
$encode = $class->encode(1);

If other digits are required

  1. Generally speaking, 6 digits are enough for large projects
  2. If you need, you can modify it to 7 digits, such as for the calculation of orders, it is also possible
bit max calculation method
5 2430,0000 30^5 power
6 7,2900,0000 30^6 power
7 218,7000,0000 30^7 power