PassServer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Servers\Common;
  3. /**
  4. * Redis数据缓存类
  5. */
  6. class PassServer
  7. {
  8. /**
  9. * 错误信息
  10. * @var string
  11. */
  12. private $errorMsg = '';
  13. private $password = '';
  14. private function __construct($password = '', $encrypt = '')
  15. {
  16. $this->setPassword($password);
  17. $this->setEncrypt($encrypt);
  18. }
  19. /**
  20. * 创建对象
  21. * @param string $password
  22. * @param string $encrypt
  23. * @return PassServer
  24. */
  25. static function creatServer($password = '', $encrypt = '')
  26. {
  27. return new PassServer($password, $encrypt);
  28. }
  29. /**
  30. * @return string
  31. */
  32. public function getPassword(): string
  33. {
  34. return $this->password;
  35. }
  36. /**
  37. * @param string $password
  38. */
  39. public function setPassword($password = ''): void
  40. {
  41. if (!empty($password)) $this->password = $password;
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getEncrypt(): string
  47. {
  48. return $this->encrypt;
  49. }
  50. /**
  51. * @param string $encrypt
  52. */
  53. public function setEncrypt($encrypt = ''): void
  54. {
  55. if (!empty($encrypt)) $this->encrypt = $encrypt;
  56. }
  57. private $encrypt = '';
  58. /**
  59. * @return string
  60. */
  61. public function getErrorMsg(): string
  62. {
  63. return $this->errorMsg;
  64. }
  65. /**
  66. * 加密字符串
  67. * @param $length
  68. * @return null|string
  69. */
  70. public function getRandChar($length = 6)
  71. {
  72. $str = null;
  73. $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  74. $max = strlen($strPol) - 1;
  75. for ($i = 0; $i < $length; $i++) {
  76. $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
  77. }
  78. return $str;
  79. }
  80. /**
  81. * 创建会员密码
  82. * @return array|false
  83. */
  84. function creatPassword()
  85. {
  86. if (empty($this->password)) {
  87. $this->errorMsg = "密码不能为空";
  88. return false;
  89. }
  90. if (empty($this->encrypt)) $this->encrypt = $this->getRandChar();
  91. $password = $this->memberPassword();
  92. return ['password' => $password, 'encrypt' => $this->encrypt];
  93. }
  94. /**
  95. * 验证密码
  96. * @param $member_pass
  97. * @return bool
  98. */
  99. public function verifyPass($member_pass)
  100. {
  101. $str = $this->memberPassword();
  102. return $member_pass == $str;
  103. }
  104. /**
  105. * 密码加密
  106. * @return string
  107. */
  108. private function memberPassword()
  109. {
  110. $str = md5($this->password) . md5($this->encrypt);
  111. return md5($str);
  112. }
  113. /**
  114. * 生成会员登录token
  115. * @param $m_id
  116. * @return string
  117. */
  118. public function getMemberToken($m_id)
  119. {
  120. $token = md5($m_id . 'token' . time());
  121. return $token;
  122. }
  123. }