1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Servers;
- use App\Models\InviteCode;
- /**
- * 图片OSS管理
- */
- class InviteCodeServer
- {
- static private $server = '';
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return InviteCodeServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new InviteCodeServer();
- }
- return self::$server;
- }
- /**
- * 获取推荐码
- * @return mixed
- */
- function getInviteCode()
- {
- $redis_on_name = 'invite_code_id' ;
- $invite_code_id = RedisDataServer::creatServer()->getData($redis_on_name);
- if(empty($invite_code_id)){
- $invite_info = InviteCode::where('status', 0)->select(['id','code'])->first();
- RedisDataServer::creatServer()->setData($redis_on_name,$invite_info->{'id'},'str',1800,false);
- }else{
- ++$invite_code_id;
- RedisDataServer::creatServer()->setData($redis_on_name,$invite_code_id,'str',1800,false);
- $invite_info = InviteCode::where('id','>',$invite_code_id)->where('status', 0)->select(['id','code'])->first();
- }
- $invite_info->update(['status'=>1]);
- return $invite_info->{'code'};
- }
- /**
- * 预生成编码
- */
- function setInviteCode()
- {
- if (file_exists(public_path('code/data-with-code-cache.json'))) {
- $json = file_get_contents(public_path('code/data-with-code-cache.json'));
- $json = json_decode($json, true);
- } else {
- $json = [];
- }
- for ($i = 1; $i <= 50000; ++$i) {
- $invite_code = '';
- $is_ok = true;
- while ($is_ok) {
- $invite_code = rand(10000000, 99999999);
- if (!in_array($invite_code, $json)) {
- $is_ok = false;
- $json[] = $invite_code;
- }
- }
- InviteCode::create(['code' => $invite_code, 'status' => 0]);
- }
- file_put_contents(public_path('code/data-with-code-cache.json'), json_encode($json));
- }
- }
|