|
@@ -0,0 +1,181 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Servers;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 密码服务
|
|
|
|
|
+ */
|
|
|
|
|
+class PassServer
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 错误信息
|
|
|
|
|
+ * @var string
|
|
|
|
|
+ */
|
|
|
|
|
+ private $errorMsg = '';
|
|
|
|
|
+
|
|
|
|
|
+ private $password = '';
|
|
|
|
|
+
|
|
|
|
|
+ private $encrypt = '';
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @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
|
|
|
|
|
+ */
|
|
|
|
|
+ function memberPassword()
|
|
|
|
|
+ {
|
|
|
|
|
+ $str = md5($this->password) . md5($this->encrypt);
|
|
|
|
|
+ return md5($str);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成登录token
|
|
|
|
|
+ * @param $id
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getRiderToken($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ $token = md5($id . 'riderToken' . time());
|
|
|
|
|
+ return $token;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成会员登录token
|
|
|
|
|
+ * @param $m_id
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getMemberToken($m_id)
|
|
|
|
|
+ {
|
|
|
|
|
+ $token = md5($m_id . 'memberToken' . time());
|
|
|
|
|
+ return $token;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成登录token
|
|
|
|
|
+ * @param $id
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getShopToken($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ $token = md5($id . 'shopToken' . time());
|
|
|
|
|
+ return $token;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成登录token
|
|
|
|
|
+ * @param $id
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getAgentToken($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ $token = md5($id . 'agentToken' . time());
|
|
|
|
|
+ return $token;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|