ExpinBot/.svn/pristine/eb/ebfd4245d3995a8b1ff99879e93...

62 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services;
use App\Models\Ledger;
use App\Models\tg_comm;
use App\Models\TgGroupUsers;
use App\Models\UserAccount;
use Illuminate\Support\Facades\Storage;
class TgComm
{
public static function TgComm($msg){
$tg_comm = include Storage::disk('dataconfig')->path('TgComm.php');
foreach ($tg_comm as $v) {
if (str_starts_with($msg['message_text'], $v['command'])){
$res['ok']= true;
$res['id']= $v['id'];
$res['command']= $v['command'];
return $res; //有的话 返回指令ID
}
}
}
public static function comm_1($msg){
}
//@’指令的处理
public static function comm_2($msg){
$res = self::parseLedgerMessage($msg['message_text']); //
if ($res) {
$TgGroupUsers=TgGroupUsers::GetUsername($res['usernamer']);
if ($TgGroupUsers) {
$res=array_merge($res, ['user_id' => $TgGroupUsers->user_id,'chat_id'=>$msg['message_chat_id']]); //加入用户和群组ID
$Ledger=Ledger::AddIncome($res); //写入收支记录
//检查该user_id是否有账户资料有的话处理余额,没有的话添加相关账户资料
$CheckUserId=UserAccount::CheckUserId($TgGroupUsers->user_id);
} else {
return $res['usernamer'].'-该用户不存在';
}
} else {
return false;
}
}
//拆解【@XXX +1000】字串
private static function parseLedgerMessage(string $text): ?array
{
if (preg_match('/@(\w+)\s*([+-])\s*(\d+(?:\.\d+)?)/', $text, $matches)) {
$usernamer = $matches[1];
$type = $matches[2] === '+' ? '0' : '1';
$amount = $matches[3];
return compact('usernamer', 'type', 'amount');
}
return false; // 格式不符
}
}