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
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.4
README
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 thisinvitation code
UserId
can be used for encoding, and the encoded string can also be used to push backId
This is what I think:
- When the user registers, query the database to enter whether the mobile phone exists (normal operation, not less)
- After the mobile phone number verification is correct, store the user login information in the
login table
- Then use the newly added data table of
LoginForm
to auto-incrementId
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
- By default, it is six digits
- Also removed
O
0
I
1
Y
Z
- After removing the wrong characters that are easy to distinguish with the naked eye, (26 + 10) - 6 = 30 bit
- 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
- default the following configuration
- You can mess up the dictionary, then the meaning of each character will change
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
- Generally speaking, 6 digits are enough for large projects
- 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 |