PasswordService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/9/16 下午4:58
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\common\services\password;
  10. use app\common\exceptions\PaymentException;
  11. use app\common\facades\Setting;
  12. use app\common\models\MemberShopInfo;
  13. class PasswordService
  14. {
  15. //todo 后台登陆密码、会员登陆密码、会员支付密码间公用关系,拆解模型、整理模型(还需要一点点梳理啊)
  16. /**
  17. * 支付密码总开关,如果关闭全部不需要密码验证
  18. *
  19. * @return bool
  20. */
  21. public function masterSwitch()
  22. {
  23. return (bool)Setting::get('pay_password.pay_state');
  24. }
  25. /**
  26. * 是否开启多位数密码
  27. *
  28. * @return bool
  29. */
  30. public function multipleSwitch()
  31. {
  32. return (bool)Setting::get('pay_password.pay_multiple');
  33. }
  34. /**
  35. * 验证虚拟币操作方式是否需要密码验证,需要返回 true,不需要返回 false
  36. *
  37. * 虚拟币类型,如:balance point love
  38. * @param string $property
  39. *
  40. * 虚拟币操作方式,如:pay transfer withdraw
  41. * @param string $operate
  42. *
  43. * @return bool
  44. */
  45. public function isNeed($property = '', $operate = '')
  46. {
  47. if (!$this->masterSwitch()) return false;
  48. return $this->propertySwitch($property, $operate);
  49. }
  50. /**
  51. * 虚拟币操作方式开关状态,开启 true,关闭 false
  52. *
  53. * @param string $property
  54. * @param string $operate
  55. *
  56. * @return bool
  57. */
  58. private function propertySwitch($property, $operate)
  59. {
  60. $setting = Setting::get("pay_password.{$property}") ?: [];
  61. return $setting ? in_array($operate, $setting) : false;
  62. }
  63. //todo 该方法应该可以提到 会员yzMember模型中
  64. public function checkPayPassword($memberId, $password)
  65. {
  66. if (!$this->masterSwitch()) throw (new PaymentException())->settingClose();
  67. $memberModel = $this->yzMember($memberId);
  68. if (!$memberModel->hasPayPassword()) throw (new PaymentException())->notSet();
  69. if (!$this->passwordCheck($password, $memberModel->pay_password, $memberModel->salt)) throw (new PaymentException())->passwordError();
  70. }
  71. /**
  72. * @param int $memberId
  73. *
  74. * @return MemberShopInfo
  75. */
  76. private function yzMember($memberId)
  77. {
  78. return MemberShopInfo::select('pay_password', 'salt')->where('member_id', $memberId)->first();
  79. }
  80. /**
  81. * 密码验证
  82. *
  83. * @param string $salt
  84. * @param string $password
  85. * @param string $sha1_value
  86. *
  87. * @return bool
  88. */
  89. public function check($password, $sha1_value, $salt)
  90. {
  91. return $sha1_value == $this->make($password, $salt) ? true : false;
  92. }
  93. /**
  94. * 生成哈希加密密码值
  95. *
  96. * @param string $salt
  97. * @param string $password
  98. *
  99. * @return string
  100. */
  101. public function make($password, $salt)
  102. {
  103. return sha1("{$password}-{$salt}");
  104. }
  105. /**
  106. * 创建密码
  107. * @param $password
  108. * @return array
  109. */
  110. public function create($password)
  111. {
  112. $salt = $this->randNum(8);
  113. return ['password' => $this->make($password, $salt), 'salt' => $salt];
  114. }
  115. /**
  116. * 获取随机字符串
  117. * @param number $length 字符串长度
  118. * @param boolean $numeric 是否为纯数字
  119. * @return string
  120. */
  121. public function randNum($length, $numeric = FALSE)
  122. {
  123. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  124. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  125. if ($numeric) {
  126. $hash = '';
  127. } else {
  128. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  129. $length--;
  130. }
  131. $max = strlen($seed) - 1;
  132. for ($i = 0; $i < $length; $i++) {
  133. $hash .= $seed{mt_rand(0, $max)};
  134. }
  135. return $hash;
  136. }
  137. /**
  138. * 前端支付密码验证
  139. *
  140. * @param $password
  141. * @param $sha1_value
  142. * @param $salt
  143. * @return bool
  144. */
  145. public function passwordCheck($password, $sha1_value, $salt)
  146. {
  147. //最新验证方式
  148. if ($sha1_value == sha1("{$password}-{$salt}")) {
  149. return true;
  150. }
  151. //原前端修改密码
  152. if ($sha1_value == sha1("{$password}-{$salt}-")) {
  153. return true;
  154. }
  155. //原后端修改密码
  156. if (config('app.framework') != 'platform') {
  157. global $_W;
  158. $authkey = $_W['config']['setting']['authkey'];
  159. if ($sha1_value == sha1("{$password}-{$salt}-{$authkey}")) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. }