一、前言

不知道大家有没有遇到过,自己做的系统最后业务操作得自己来。对于那种一条数据要一个一个点进去操作得,作为程序员哪能忍,顺手就要做一个批量操作的功能!

但是,要做功能还得改UI界面,为了偷懒,直接做成命令型的,反正也是给自己用的,最好就是用Windows的命令直接操作。

二、思路

1.PHP项目下自定义指令

2.将指令启动做成windows批处理

3.批处理文件添加到环境变量

三、自定义指令

项目使用ThinkPHP框架,通过继承Command实现一个符合业务的指令,以下为演示代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
protected function execute(Input $input, Output $output)
{
$envMap = [
[
"name" => "\033[31m▲开发环境\033[0m",
"database" => "xxx.xx.xxx.xxx",
],
[
"name" => "\033[33m★生产环境\033[0m",
"database" => "yyy.yyy.yyy.yy"
]
];

$envMapReset = array_column($envMap, null, "database");
$currEnv = Env::get("database.hostname") ?? "";
$currEnvName = $envMapReset[$currEnv]["name"] ?? "";

if (!$currEnvName) {
$output->error("\033[33m当前未连接数据库~\033[0m");die;
}

//$res = $output->ask($input, "当前数据库为1,是否继续");
//$isContinue = $output->choice($input, "当前是".$currEnvName. ",是否继续?",["A.继续", "B.取消"]);
$isContinue = $output->confirm($input, "当前".$currEnvName. ",是否继续?");

if ($isContinue) {
$excelPath = $output->ask($input, "\033[32m请输入表格地址:\033[0m");
$excelObj = new Excel();

// 导入获取数据
$baseImportPath = "E:\\4work\\01_Upload_Price\\01_Import\\";
$excelData = $excelObj->importExcel($baseImportPath.$excelPath, 0);

$goodsItemArr = Excel::formattingCells($excelData, $this->importDict);

$orderArr = array_column($goodsItemArr, 'sn');
$orderMap[] = ['sn', 'in', $orderArr];
$fields = ['sn, user_coupon_id'];
$orderList = Db::name('order')->field($fields)->where($orderMap)->select();
$orderListReset = array_column($orderList, null, 'sn');

$noMatchOrder = [];
$ReMatchOrder = [];

foreach($goodsItemArr as $key => &$value) {
$transactionPrice = $value['transaction_price'];
$currOrder = $orderListReset[$value['sn']];
$currOrderCouponId = $currOrder['user_coupon_id'] ?? 0;

$coupon = $this->automaticMatch($currOrder, $transactionPrice);
$couponValue = $coupon['coupon_value'] ?? 0;
if ($currOrderCouponId != 0) {
if ($couponValue == 0 && $transactionPrice >= 200) {
$noMatchOrder[] = $value['sn'];
} else {
$value['coupon_price'] = $couponValue;
$ReMatchOrder[] = $value['sn'];
}
}
}

$output->writeln("\033[31m未匹配到加价券订单:\033[0m". implode(",", $noMatchOrder) . ";");

$output->newLine();

$output->writeln("\033[36m重新匹配的加价券订单:\033[0m". implode(",", $ReMatchOrder) . ";");

$output->newLine();

$savePath = "E:\\4work\\01_Upload_Price\\02_Output\\";
$filePath = $excelObj->outPutToPath($goodsItemArr, $this->outputDict, $savePath);

$output->writeln("导出地址:". $filePath);
}
}

四、添加批处理

在TP的项目根目录下新建一个批处理文件,并将绝对路径复制进入,比如php_think.bat,可以把下面代码改成自己的保存就可以了。

1
2
@echo off
php "E:\2setsoft\1dev\phpstudy_pro\WWW\1work\hhhs_admin\think" %*

五、添加环境变量

打开“我的电脑”->“属性”->“高级系统设置”->“环境变量”, 找到系统变量下的“path”,然后将创建了批处理的bat的绝对路径粘贴到Path中。

六、运行

打开电脑的cmd面板,直接输入创建的批处理名称,比如我的是php_think.bat,在面板中输入php_think就会调起项目中的指令,后面再带上PHP项目指令名称。

image-20250416172110092

七、写在后面

其实这样有一个问题,就是我们项目中的所有指令都暴露在CMD之下了,所以,留下一个延伸点,如何限制不同的系统用户操作命令权限?

image-20250408192801332