123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace App\Servers;
- /**
- * Redis数据缓存类
- */
- class PassServer
- {
- /**
- * 错误信息
- * @var string
- */
- private $errorMsg = '';
- private $password = '';
- private function __construct($password = '', $encrypt = '')
- {
- $this->setPassword($password);
- $this->setEncrypt($encrypt);
- }
- /**
- * 创建对象
- * @param string $password
- * @param string $encrypt
- * @return PassServer
- */
- static function creatServer($password = '', $encrypt = '')
- {
- return new PassServer($password, $encrypt);
- }
- /**
- * @return string
- */
- public function getPassword(): string
- {
- return $this->password;
- }
- /**
- * @param string $password
- */
- public function setPassword($password = ''): void
- {
- if (!empty($password)) $this->password = $password;
- }
- /**
- * @return string
- */
- public function getEncrypt(): string
- {
- return $this->encrypt;
- }
- /**
- * @param string $encrypt
- */
- public function setEncrypt($encrypt = ''): void
- {
- if (!empty($encrypt)) $this->encrypt = $encrypt;
- }
- private $encrypt = '';
- /**
- * @return string
- */
- public function getErrorMsg(): string
- {
- return $this->errorMsg;
- }
- /**
- * 加密字符串
- * @param $length
- * @return null|string
- */
- public function getRandChar($length = 6)
- {
- $str = null;
- $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
- $max = strlen($strPol) - 1;
- for ($i = 0; $i < $length; $i++) {
- $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
- }
- return $str;
- }
- /**
- * 创建会员密码
- * @return array|false
- */
- function creatPassword()
- {
- if (empty($this->password)) {
- $this->errorMsg = "密码不能为空";
- return false;
- }
- if (empty($this->encrypt)) $this->encrypt = $this->getRandChar();
- $password = $this->memberPassword();
- return ['password' => $password, 'encrypt' => $this->encrypt];
- }
- /**
- * 验证密码
- * @param $member_pass
- * @return bool
- */
- public function verifyPass($member_pass)
- {
- $str = $this->memberPassword();
- return $member_pass == $str;
- }
- /**
- * 密码加密
- * @return string
- */
- private function memberPassword()
- {
- $str = md5($this->password) . md5($this->encrypt);
- return md5($str);
- }
- /**
- * 获取推荐码
- * @return int|string
- */
- public function getInviteCode()
- {
- return InviteCodeServer::creatServer()->getInviteCode();
- }
- /**
- * 生成会员登录token
- * @param $m_id
- * @return string
- */
- public function getMemberToken($m_id)
- {
- $token = md5($m_id . 'token' . time());
- return $token;
- }
- public function setSecretKey($private_key, $key_num = 0)
- {
- $key_num = $key_num % 20;
- if ($key_num == 0) $key_num = 15;
- $private_one = substr($private_key, 0, $key_num);
- $private_two = substr($private_key, $key_num, 5);
- $private_three = substr($private_key, $key_num + 5);
- $new_private = $private_three . $private_one . $private_two;
- return $new_private;
- }
- /**
- * @param $private_key
- * @param int $key_num
- * @return string
- */
- public function getSecretKey($private_key, $key_num = 0)
- {
- $key_num = $key_num % 20;
- if ($key_num == 0) $key_num = 15;
- $private_length = strlen($private_key);
- $three_length = $private_length - $key_num - 5;
- $private_three = substr($private_key, 0, $three_length);
- $private_two = substr($private_key, -5);
- $private_one = substr($private_key, $three_length, $key_num);
- $new_private = $private_one . $private_two . $private_three;
- return $new_private;
- }
- }
|