123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Servers\Common;
- /**
- * 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);
- }
- /**
- * 生成会员登录token
- * @param $m_id
- * @return string
- */
- public function getMemberToken($m_id)
- {
- $token = md5($m_id . 'token' . time());
- return $token;
- }
- }
|