Skip to content

Commit 722b286

Browse files
committed
新增进制转换类
1 parent cf4e734 commit 722b286

File tree

10 files changed

+91
-169
lines changed

10 files changed

+91
-169
lines changed

Logger.php

Lines changed: 0 additions & 132 deletions
This file was deleted.

base/BaseTransform.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* @author carlo<[email protected]>
5+
*/
6+
7+
class BaseTransform
8+
{
9+
private $alphabet;
10+
11+
private $base;
12+
public function __construct(string $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'){
13+
$this -> alphabet = $alphabet;//设置进制
14+
$this -> base = strlen($alphabet);//计算进制数
15+
}
16+
17+
/**
18+
* 编码
19+
* @param int $id
20+
* @return string
21+
*/
22+
public function encode(int $id)
23+
{
24+
$shortenedId = '';
25+
26+
if($id == 0){
27+
$shortenedId = $this -> alphabet{0};
28+
}
29+
while($id > 0){
30+
$remainder = $id % $this -> base;
31+
$id = ($id-$remainder) / $this -> base;
32+
if($remainder == 0){
33+
$shortenedId = $this -> alphabet{0} . $shortenedId;
34+
}else{
35+
$shortenedId = $this -> alphabet{$remainder} . $shortenedId;
36+
}
37+
}
38+
return $shortenedId;
39+
}
40+
41+
/**
42+
* 解码
43+
* @param string $base_num
44+
* @return float|int
45+
*/
46+
public function decode(string $base_num){
47+
$raw_base = $base_num;
48+
$data_length = strlen($raw_base);
49+
$dec_data = 0;
50+
for($i = 0; $i < $data_length; $i++){//每一位分别读取
51+
$multiply = 1;
52+
for($j = $data_length - 1; $j > $i; $j--){//构造当前位需要乘的数
53+
if($i == ($data_length - 1)){
54+
$multiply = 1;
55+
}else{
56+
$multiply = $multiply * $this -> base;
57+
}
58+
}
59+
$dec_data += strpos($this -> alphabet, $raw_base{$i}) * $multiply; //计算当前位数代表的十进制数,然后累加
60+
}
61+
return $dec_data;
62+
}
63+
}

base/demo.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
4+
include 'BaseTransform.php';
5+
$b = new BaseTransform();
6+
$e = $b->encode(0);
7+
$d = $b->decode($e);
8+
var_dump($e,$d);
9+

common_helper.php

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ function is_json($string)
1616
}
1717
}
1818

19-
if (!function_exists('is_not_json')) {
20-
/**
21-
* 判断数据不是JSON格式
22-
*
23-
* @param $str
24-
* @return bool
25-
*
26-
*/
27-
function is_not_json($str)
28-
{
29-
return is_null(json_decode($str));
30-
}
31-
}
32-
3319
if (!function_exists('create_erweima')) {
3420
/**
3521
* 利用google api生成二维码图片
@@ -336,15 +322,15 @@ function is_date($time)
336322
}
337323

338324
if (!function_exists('get_uuid')) {
339-
/**
340-
* 获取唯一id
341-
*
342-
* @return string
343-
*
344-
*/
345-
function get_uuid()
325+
/**
326+
* 获取唯一id
327+
*
328+
* @param string $prefix
329+
* @return string
330+
*/
331+
function get_uuid($prefix='')
346332
{
347-
return md5(uniqid(md5(microtime(true)), true));
333+
return uniqid($prefix, true);
348334
}
349335
}
350336

@@ -532,21 +518,16 @@ function get_from_ip()
532518
*/
533519
function sign($params, $secret_key)
534520
{
535-
ksort($params);
536-
$sign = '';
537-
foreach ($params as $key => $val) {
538-
$sign .= $key . $val;
539-
}
540-
$sign .= 'secret_key' . $secret_key;
541-
$sign = md5($sign);
542-
543-
return $sign;
521+
unset($params['sign']);
522+
ksort($params);
523+
$params['secret_key'] = $secret_key;
524+
return md5(http_build_query($params));
544525
}
545526
}
546527

547528
if (!function_exists('transformTime')) {
548529
/**
549-
* 时间戳
530+
* 时间戳换算成距今时间
550531
*
551532
* @param int $time
552533
* @return string
@@ -1098,4 +1079,4 @@ function add_tree(&$items, $pid, $name)
10981079
'name' => $name,
10991080
];
11001081
}
1101-
}
1082+
}

info.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<p align="center"><img src="https://www.aiku.fun/assets/images/logo.png"></p>
1+
<p align="center"><img src="https://www.aiju.fun/assets/images/logo.png"></p>
22
<p align="center">
33

44
</p>
55

66
## 简介
77
#### 爱酷饭公共代码
88
## 目录结构
9-
>- get_phone 手机号码归属地类库
9+
>- phone 手机号码归属地类库
10+
>- ip ip归属地类库
1011
>- .editorconfig git配置文件
1112
>- .gitattributes git配置文件
1213
>- .gitignore git配置文件

ip_test.php renamed to ip/demo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* @create 2019-03-21 16:36:10
66
* @author jiangbingjie<[email protected]>
77
*/
8-
require_once 'ip/Ip_location.php';
8+
require_once 'Ip_location.php';
99
$ip_location = new Ip_location();
1010
$ip = "180.76.6.130";
1111
$location = $ip_location->getlocation($ip);
12-
var_dump($location);
12+
var_dump($location);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)