123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace service;
- use think\Request;
- use service\JsonService;
- class UtilService
- {
- public static function postMore($params,Request $request = null,$suffix = false)
- {
- if($request === null) $request = Request::instance();
- $p = [];
- $i = 0;
- foreach ($params as $param){
- if(!is_array($param)) {
- $p[$suffix == true ? $i++ : $param] = $request->post($param);
- }else{
- if(!isset($param[1])) $param[1] = null;
- if(!isset($param[2])) $param[2] = '';
- $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
- $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->post($name,$param[1],$param[2]);
- }
- }
- return $p;
- }
- public static function getMore($params,Request $request=null,$suffix = false)
- {
- if($request === null) $request = Request::instance();
- $p = [];
- $i = 0;
- foreach ($params as $param){
- if(!is_array($param)) {
- $p[$suffix == true ? $i++ : $param] = $request->get($param);
- }else{
- if(!isset($param[1])) $param[1] = null;
- if(!isset($param[2])) $param[2] = '';
- $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
- $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->get($name,$param[1],$param[2]);
- }
- }
- return $p;
- }
- public static function encrypt($string, $operation = false, $key = '', $expiry = 0) {
-
- $ckey_length = 6;
-
- $key = md5($key);
-
- $keya = md5(substr($key, 0, 16));
-
- $keyb = md5(substr($key, 16, 16));
-
- $keyc = $ckey_length ? ($operation == false ? substr($string, 0, $ckey_length):
- substr(md5(microtime()), -$ckey_length)) : '';
-
- $cryptkey = $keya.md5($keya.$keyc);
- $key_length = strlen($cryptkey);
-
-
- $string = $operation == false ? base64_decode(substr($string, $ckey_length)) :
- sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
- $string_length = strlen($string);
- $result = '';
- $box = range(0, 255);
- $rndkey = array();
-
- for($i = 0; $i <= 255; $i++) {
- $rndkey[$i] = ord($cryptkey[$i % $key_length]);
- }
-
- for($j = $i = 0; $i < 256; $i++) {
- $j = ($j + $box[$i] + $rndkey[$i]) % 256;
- $tmp = $box[$i];
- $box[$i] = $box[$j];
- $box[$j] = $tmp;
- }
-
- for($a = $j = $i = 0; $i < $string_length; $i++) {
- $a = ($a + 1) % 256;
- $j = ($j + $box[$a]) % 256;
- $tmp = $box[$a];
- $box[$a] = $box[$j];
- $box[$j] = $tmp;
-
- $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
- }
- if($operation == false) {
-
- if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&
- substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
- return substr($result, 26);
- } else {
- return '';
- }
- } else {
-
-
- return $keyc.str_replace('=', '', base64_encode($result));
- }
- }
-
- public static function pathToUrl($path)
- {
- return trim(str_replace(DS, '/', $path),'.');
- }
-
- public static function urlToPath($url)
- {
- return ROOT_PATH.'public/'.trim(str_replace('/',DS,$url),DS);
- }
- public static function timeTran($time)
- {
- $t = time() - $time;
- $f = array(
- '31536000' => '年',
- '2592000' => '个月',
- '604800' => '星期',
- '86400' => '天',
- '3600' => '小时',
- '60' => '分钟',
- '1' => '秒'
- );
- foreach ($f as $k => $v) {
- if (0 != $c = floor($t / (int)$k)) {
- return $c . $v . '前';
- }
- }
- }
-
- public static function sortListTier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
- {
- static $list = [];
- if ($clear) $list = [];
- foreach ($data as $k => $res) {
- if ($res[$field] == $pid) {
- $res['html'] = str_repeat($html, $level);
- $list[] = $res;
- unset($data[$k]);
- self::sortListTier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
- }
- }
- return $list;
- }
-
- public static function rmPublicResource($url,$isPath = false)
- {
- $path = $isPath ? $url : realpath(self::urlToPath($url));
- if(!$path) return JsonService::fail('删除文件不存在!');
- if(!file_exists($path)) return JsonService::fail('删除路径不合法!');
- if(!unlink($path)) return JsonService::fail('删除文件失败!');
- return JsonService::successful();
- }
-
- public static function isWechatBrowser()
- {
- return (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false);
- }
-
- public static function anonymity($name)
- {
- $strLen = mb_strlen($name,'UTF-8');
- $min = 3;
- if($strLen <= 1)
- return '*';
- if($strLen<= $min)
- return mb_substr($name,0,1,'UTF-8').str_repeat('*',$min-1);
- else
- return mb_substr($name,0,1,'UTF-8').str_repeat('*',$strLen-1).mb_substr($name,-1,1,'UTF-8');
- }
-
- public static function setCard($card){
- $city = [11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",21=>"辽宁",22=>"吉林",23=>"黑龙江 ",31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",41=>"河南",42=>"湖北 ",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏 ",61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",71=>"台湾",81=>"香港",82=>"澳门",91=>"国外 "];
- $tip = "";
- $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
- $pass= true;
- if(!$card || !preg_match($match,$card)){
-
- $pass = false;
- }else if(!$city[substr($card,0,2)]){
-
- $pass = false;
- }else{
-
- if(strlen($card) == 18){
- $card = str_split($card);
-
-
- $factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
-
- $parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
- $sum = 0;
- $ai = 0;
- $wi = 0;
- for ($i = 0; $i < 17; $i++)
- {
- $ai = $card[$i];
- $wi = $factor[$i];
- $sum += $ai * $wi;
- }
- $last = $parity[$sum % 11];
- if($parity[$sum % 11] != $card[17]){
- $pass =false;
- }
- }else{
- $pass =false;
- }
- }
- if(!$pass) return false;
- return true;
- }
-
- public static function getDeviceType()
- {
- if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
- return 1;
- }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
- return 2;
- }else{
- return 3;
- }
- }
-
- public static function makeRandomNumber($prefix = false, $random = false)
- {
- if (!$prefix) {
- $prefix = "";
- }
- if (!$random || !is_numeric($random)) {
- $one_random = mt_rand(11111,99999);
- }else{
- $one_random = sprintf("%05d", $random);
- }
- $date_random = date('ymd',time());
- $random_tmp = strlen($one_random);
- $two_randow = str_pad(mt_rand(1, 99999), $random_tmp, '0', STR_PAD_LEFT);
- if (!$random) {
- return $two_randow;
- }else{
- return $prefix.$one_random.$date_random.$two_randow;
- }
- }
- }
|