Skip to content

Commit bb56b87

Browse files
committed
refactor: perform different tools according to the operation
1 parent c9b3ce5 commit bb56b87

File tree

4 files changed

+162
-39
lines changed

4 files changed

+162
-39
lines changed

laravel/app/Helper/functions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,29 @@ function isName($name)
8383
return preg_match('/^[\x{2E80}-\x{FE4F}]{2,16}$/u', $name);
8484
}
8585
}
86+
87+
/**
88+
* 下划线转驼峰
89+
* @param $str
90+
* @return string|string[]|null
91+
*/
92+
function convertUnderline($str)
93+
{
94+
$str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function($matches) {
95+
return strtoupper($matches[2]);
96+
}, $str);
97+
return $str;
98+
}
99+
100+
/**
101+
* 驼峰转下划线
102+
* @param $str
103+
* @return string|string[]|null
104+
*/
105+
function humpToLine($str)
106+
{
107+
$str = preg_replace_callback('/([A-Z]{1})/', function($matches) {
108+
return '_'.strtolower($matches[0]);
109+
}, $str);
110+
return $str;
111+
}

laravel/app/Http/Controllers/Bus/ToolController.php

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,31 @@
99
namespace App\Http\Controllers\Bus;
1010

1111

12+
use App\Http\Repository\ToolRepository;
1213
use Illuminate\Http\Request;
1314

1415
class ToolController extends CommonController
1516
{
16-
/**
17-
* 字符串转十六进制函数
18-
*
19-
* @param Request $request
20-
*
21-
* @return string
22-
*/
23-
public function stringToHex(Request $request)
17+
public function tool(Request $request)
2418
{
25-
$str = $request->input('input');
26-
$hex = "";
27-
for ($i = 0; $i < strlen($str); $i++) {
28-
$hex .= dechex(ord($str[$i]));
29-
}
30-
$hex = strtoupper($hex);
19+
$func = $request->input('operation');
20+
$input = $request->input('input');
21+
// 下划线转驼峰
22+
$convert = convertUnderline($func);
3123

32-
return $this->out(200, ['output' => $hex]);
33-
}
24+
$toolRepository = ToolRepository::getInstent();
25+
if ($convert && $input) {
26+
if (is_callable([$toolRepository, $convert])) {
27+
$out = $toolRepository->$convert($input);
28+
if (!$out) {
29+
return $this->out(4009);
30+
}
3431

35-
/**
36-
* 十六进制转字符串函数
37-
*
38-
* @param $request
39-
*
40-
* @return string
41-
*/
42-
public function hexToString(Request $request)
43-
{
44-
$hex = strtoupper($request->input('input'));
45-
$hex = str_replace('\\X', '', $hex);
46-
if (!preg_match("/^[A-Fa-f0-9]+$/", $hex)) {
47-
return $this->out(1006);
32+
return $this->out(200, ['output' => $out]);
33+
}
4834
}
49-
$str = "";
50-
for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
51-
$str .= chr(hexdec($hex[$i].$hex[$i + 1]));
52-
}
53-
if (preg_match('~[\x{4e00}-\x{9fa5}]+~u', $str, $tmp)) {
54-
return $this->out(200, ['output' => (string)$str]);
55-
}
56-
return $this->out(4009);
35+
36+
return $this->out(1006);
5737
}
5838

5939
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Desc: TaskRepository 仓库类
4+
* User: lisgroup
5+
* Date: 18-11-03
6+
* Time: 22:06
7+
*/
8+
9+
namespace App\Http\Repository;
10+
11+
12+
use App\Models\CronTask;
13+
use App\Models\Line;
14+
use Illuminate\Support\Facades\Log;
15+
use QL\QueryList;
16+
17+
class ToolRepository
18+
{
19+
private static $instance;
20+
21+
22+
public static function getInstent($conf = [])
23+
{
24+
if (!isset(self::$instance) || !(self::$instance instanceof TaskRepository)) {
25+
self::$instance = new static($conf);
26+
}
27+
return self::$instance;
28+
}
29+
30+
/**
31+
* 字符串转十六进制函数
32+
*
33+
* @param $str
34+
*
35+
* @return string
36+
*/
37+
public function stringToHex($str)
38+
{
39+
$hex = "";
40+
for ($i = 0; $i < strlen($str); $i++) {
41+
$hex .= dechex(ord($str[$i]));
42+
}
43+
44+
return strtoupper($hex);
45+
}
46+
47+
/**
48+
* 十六进制转字符串函数
49+
*
50+
* @param $input
51+
*
52+
* @return string
53+
*/
54+
public function hexToString($input)
55+
{
56+
$hex = strtoupper($input);
57+
$hex = str_replace('\\X', '', $hex);
58+
if (!preg_match("/^[A-Fa-f0-9]+$/", $hex)) {
59+
return $this->out(1006);
60+
}
61+
$str = "";
62+
for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
63+
$str .= chr(hexdec($hex[$i].$hex[$i + 1]));
64+
}
65+
if (preg_match('~[\x{4e00}-\x{9fa5}]+~u', $str, $tmp)) {
66+
return (string)$str;
67+
}
68+
return '';
69+
}
70+
71+
/**
72+
* 加密的密钥为:客户个人中心的openid经过md5后结果为小写取前16位
73+
* @param $input
74+
* @return false|string
75+
*/
76+
public function openidSecret($input)
77+
{
78+
return substr(md5($input), 0, 16);
79+
}
80+
81+
public function base64Encode($input)
82+
{
83+
return base64_encode($input);
84+
}
85+
86+
public function base64Decode($input)
87+
{
88+
return base64_decode($input);
89+
}
90+
91+
public function urlEncode($input)
92+
{
93+
return urlencode($input);
94+
}
95+
96+
public function urlDecode($input)
97+
{
98+
return urldecode($input);
99+
}
100+
101+
/**
102+
* TaskRepository constructor.
103+
* @param $config
104+
*/
105+
private function __construct($config)
106+
{
107+
if (!empty($config)) {
108+
foreach ($config as $key => $value) {
109+
$this->$key = $value;
110+
}
111+
}
112+
}
113+
114+
private function __clone()
115+
{
116+
117+
}
118+
}

laravel/routes/web.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
Route::any('test', 'NewApiController@jwt');
4949

5050
// 工具类接口
51-
Route::any('hex-to-string', 'ToolController@hexToString');
52-
Route::any('string-to-hex', 'ToolController@stringToHex');
51+
Route::any('tool', 'ToolController@tool');
5352

5453
// 1. 获取七牛上传操作的 token
5554
Route::get('getToken', 'AutoController@getToken');

0 commit comments

Comments
 (0)