PassServer.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Servers;
  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. * 获取推荐码
  115. * @return int|string
  116. */
  117. public function getInviteCode()
  118. {
  119. return InviteCodeServer::creatServer()->getInviteCode();
  120. }
  121. /**
  122. * 生成会员登录token
  123. * @param $m_id
  124. * @return string
  125. */
  126. public function getMemberToken($m_id)
  127. {
  128. $token = md5($m_id . 'token' . time());
  129. return $token;
  130. }
  131. public function setSecretKey($private_key, $key_num = 0)
  132. {
  133. $key_num = $key_num % 20;
  134. if ($key_num == 0) $key_num = 15;
  135. $private_one = substr($private_key, 0, $key_num);
  136. $private_two = substr($private_key, $key_num, 5);
  137. $private_three = substr($private_key, $key_num + 5);
  138. $new_private = $private_three . $private_one . $private_two;
  139. return $new_private;
  140. }
  141. /**
  142. * @param $private_key
  143. * @param int $key_num
  144. * @return string
  145. */
  146. public function getSecretKey($private_key, $key_num = 0)
  147. {
  148. $key_num = $key_num % 20;
  149. if ($key_num == 0) $key_num = 15;
  150. $private_length = strlen($private_key);
  151. $three_length = $private_length - $key_num - 5;
  152. $private_three = substr($private_key, 0, $three_length);
  153. $private_two = substr($private_key, -5);
  154. $private_one = substr($private_key, $three_length, $key_num);
  155. $new_private = $private_one . $private_two . $private_three;
  156. return $new_private;
  157. }
  158. }