修正 Telegram 控制器與帳戶服務相關邏輯

This commit is contained in:
2025-05-20 16:07:35 +07:00
parent 5e392ae581
commit 92ad9debe8
6 changed files with 60 additions and 40 deletions
+13 -6
View File
@@ -29,20 +29,27 @@ 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']);
$TgGroupUsers=TgGroupUsers::GetUsername($res['username']); //获取用户资料
if ($TgGroupUsers) {
$res=array_merge($res, ['user_id' => $TgGroupUsers->user_id,'chat_id'=>$msg['message_chat_id'],'token'=>$msg['token']]); //加入用户和群组ID
$Ledger=Ledger::AddIncome($res); //写入收支记录
$CheckUser=UserAccount::CheckUserId($res); //检查该user_id是否有账户资料,有的话处理余额,没有的话添加相关账户资料
if ($CheckUser){
$balance=Ledger::UpdateUser($res); //更新账号余额
$data=array_merge($res,['comm' => 2,'balance' => $balance,'token' => $msg['token'],'chat_id'=>$msg['chat_id']]); //
$balance=UserAccount::UpdateUser($res); //更新账号余额
$data=array_merge($res,['comm' => 2,'balance' => $balance,'token' => $msg['token']]); //添加资料到数组
$Msg=TgMsg::MakeMsg($data); //构成发送数据
return $Msg;
// dd($Msg);
$res=UserFun::CurlPost($Msg['url'],$Msg['text']); //发送Tg消息
dd($res);
return $res;
} else {
$balance=UserAccount::CreatUser($res); //新建用户账号,并写入充值金额
$res=array_merge($res,['balance' => $balance]);
//发送Tg通知
$Msg=TgMsg::MakeMsg($res); //构成发送数据
dd($Msg);
$res=CurlPost($Msg);
return $res;
}
} else {
return $res['usernamer'].'-该用户不存在';
@@ -56,11 +63,11 @@ public static function comm_2($msg){
private static function parseLedgerMessage(string $text): ?array
{
if (preg_match('/@(\w+)\s*([+-])\s*(\d+(?:\.\d+)?)/', $text, $matches)) {
$usernamer = $matches[1];
$username = $matches[1];
$type = $matches[2] === '+' ? '0' : '1';
$amount = (float) str_replace(',', '', $matches[3]);
return compact('usernamer', 'type', 'amount');
return compact('username', 'type', 'amount');
}
return null; // 格式不符
+14 -5
View File
@@ -5,13 +5,13 @@ class TgMsg
{
public static function MakeMsg($data){
$url = "https://api.telegram.org/bot" . $data['token'] . "/sendMessage";
$MsgTxt['url'] = "https://api.telegram.org/bot" . $data['token'] . "/sendMessage";
// $keyb['inline_keyboard'][0][0]['text'] = '点击查询账单';
// $keyb['inline_keyboard'][0][0]['url'] = 'https://'.$BotUrl.'/bill/' . $data['message_from_id'] . '/' . $data['message_chat_id'] . '/T/Inquiry';
// $keyb = json_encode($keyb);
$MsgTxt['chat_id'] = $data['chat_id'];
// $MsgTxt['reply_markup'] = $keyb;
$MsgTxt['text'] = self::{'comm_' . $data['comm']}($data);
$MsgTxt['text']= self::{'comm_' . $data['comm']}($data);
return $MsgTxt;
}
@@ -20,7 +20,16 @@ public static function Comm_1($data) {
}
public static function Comm_2($data) {
$Msg = $data['username'].'-你已经成功充值 '.$data['amount'].',当前余额为'.$data['balance'];
$Msg['chat_id'] = $data['chat_id'];
$Msg['text'] = $data['username'].'-你已经成功充值 '.$data['amount'];
$Msg['reply_markup'] = json_encode([
'inline_keyboard' => [[[
'text' => '查看余额及详情',
'url' => 'https://test.com'
]]]
]);
;
return $Msg;
}
}
+8 -6
View File
@@ -69,13 +69,15 @@ public static function CurlPost(string $url, array $data = []): array
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // POST的資料
$response = curl_exec($ch);
$err = curl_error($ch);
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
if ($err) {
return ['ok' => false, 'error' => $err];
}
return json_decode($response, true);
return [
'ok' => $errno === 0,
'error' => $error,
'code' => $errno,
'body' => $response,
];
}
}