123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wangsai
- * Date: 2019/9/5
- * Time: 16:55
- */
- namespace App\Support;
- trait UserAuth
- {
- public static function random($length = 6, $type = 'string', $convert = 0)
- {
- $config = array(
- 'number' => '1234567890',
- 'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
- 'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
- 'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
- );
- if (!isset($config[$type]))
- $type = 'string';
- $string = $config[$type];
- $code = '';
- $strlen = strlen($string) - 1;
- for ($i = 0; $i < $length; $i++) {
- $code .= $string{mt_rand(0, $strlen)};
- }
- if (!empty($convert)) {
- $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
- }
- return $code;
- }
- /**
- * 验证sign
- * @param $userId
- * @param $ttl
- * @param $random
- * @param $sign
- * @return bool
- */
- public static function validSign($userId,$ttl,$random,$sign){
- $params = array('user_id'=> $userId, 'ttl'=> $ttl, 'random'=> $random);
- $makesign = self::getSignature($params);
- if($makesign == $sign) {
- return true;
- }
- return false;
- }
- public static function getSignature($params)
- {
- // 按数组键名 正序排序
- ksort($params);
- $tem = array();
- foreach ($params as $k => $v) {
- if ($k !== 'sign') {
- $tem[] = "$k=$v";
- }
- }
- $sk = implode('&', $tem);
- return md5($sk);
- }
- }
|