新版订单消耗系统

UserAuth.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangsai
  5. * Date: 2019/9/5
  6. * Time: 16:55
  7. */
  8. namespace App\Support;
  9. trait UserAuth
  10. {
  11. public static function random($length = 6, $type = 'string', $convert = 0)
  12. {
  13. $config = array(
  14. 'number' => '1234567890',
  15. 'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  16. 'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
  17. 'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  18. );
  19. if (!isset($config[$type]))
  20. $type = 'string';
  21. $string = $config[$type];
  22. $code = '';
  23. $strlen = strlen($string) - 1;
  24. for ($i = 0; $i < $length; $i++) {
  25. $code .= $string{mt_rand(0, $strlen)};
  26. }
  27. if (!empty($convert)) {
  28. $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
  29. }
  30. return $code;
  31. }
  32. /**
  33. * 验证sign
  34. * @param $userId
  35. * @param $ttl
  36. * @param $random
  37. * @param $sign
  38. * @return bool
  39. */
  40. public static function validSign($userId,$ttl,$random,$sign){
  41. $params = array('user_id'=> $userId, 'ttl'=> $ttl, 'random'=> $random);
  42. $makesign = self::getSignature($params);
  43. if($makesign == $sign) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. public static function getSignature($params)
  49. {
  50. // 按数组键名 正序排序
  51. ksort($params);
  52. $tem = array();
  53. foreach ($params as $k => $v) {
  54. if ($k !== 'sign') {
  55. $tem[] = "$k=$v";
  56. }
  57. }
  58. $sk = implode('&', $tem);
  59. return md5($sk);
  60. }
  61. }