Browse Source

接入 Mefree

urban 2 weeks ago
parent
commit
fa4f73a3af
3 changed files with 110 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 45 0
      app/Task/GetEnergyPlatformBalance.php
  3. 64 0
      app/function.php

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.idea

+ 45 - 0
app/Task/GetEnergyPlatformBalance.php

@@ -285,6 +285,51 @@ class GetEnergyPlatformBalance
                             }
                         }
                     }
+
+                    // MeFree.NET 平台
+                    elseif($v['platform_name'] == 6){
+
+                        // API 配置信息
+                        $base_url = "https://api.mefree.net";        // Mefree API 基础地址
+                        $api_key = $v['platform_uid'];              // 替换为实际的 API Key
+                        $secret_key = $v['platform_apikey'];        // 替换为实际的 Secret Key
+
+                        // 获取账户信息
+                        $request_path = "/api/available"; // API 请求路径
+                        $response = send_request("GET", $request_path, $base_url, $api_key, $secret_key);
+
+                        // 处理返回结果
+                        if ($response['code'] == 0) {
+
+                            $balance = $response['data'] <= 0 ?0:$response['data'];
+
+                            $updatedata2['platform_balance'] = $balance;
+
+                            if($balance <= $v['alert_platform_balance'] && $v['alert_platform_balance'] > 0 && strtotime($v['last_alert_time']) + 600 <= strtotime(nowDate())){
+                                $updatedata2['last_alert_time'] = nowDate();
+
+                                //余额通知管理员
+                                if($v['tg_notice_obj'] && !empty($v['tg_notice_obj'])){
+                                    $replytext = "能量平台(MeFree.NET),余额不足,请立即前往平台充值!\n"
+                                        ."能量平台ID:".$v['rid']."\n"
+                                        ."平台用户UID:".$v['platform_uid']."\n"
+                                        ."当前余额:".$balance."\n"
+                                        ."告警金额:".$v['alert_platform_balance']."\n\n"
+                                        ."不处理会一直告警通知!间隔10分钟告警一次";
+
+                                    $sendlist = explode(',',$v['tg_notice_obj']);
+
+                                    foreach ($sendlist as $x => $y) {
+                                        $sendmessageurl = 'https://api.telegram.org/bot'.$v['bot_token'].'/sendMessage?chat_id='.$y.'&text='.urlencode($replytext).'&parse_mode=HTML';
+
+                                        Get_Pay($sendmessageurl);
+                                    }
+                                }
+                            }
+
+                            EnergyPlatform::where('rid',$v['rid'])->update($updatedata2);
+                        }
+                    }
                 }
             }
             

+ 64 - 0
app/function.php

@@ -561,4 +561,68 @@ function remove0x($value)
         return substr($value, 2);
     }
     return $value;
+}
+
+
+
+/**
+ * 生成 Mefree API 的签名
+ * @param string $timestamp UTC 时间戳 (ISO 8601 格式)
+ * @param string $method HTTP 方法 (GET/POST)
+ * @param string $request_path API 请求路径(含参数)
+ * @param string $secret_key API 的 Secret Key
+ * @return string 返回 Base64 编码的签名
+ */
+function generate_signature($timestamp, $method, $request_path, $secret_key) {
+    // 拼接待签名字符串
+    $string_to_sign = $timestamp . $method . $request_path;
+
+    // 使用 HMAC-SHA256 算法对字符串进行加密
+    $hash = hash_hmac('sha256', $string_to_sign, $secret_key, true);
+
+    // 返回 Base64 编码的签名
+    return base64_encode($hash);
+}
+
+/**
+ * 发起 Mefree API 请求
+ * @param string $method HTTP 方法 (GET/POST)
+ * @param string $request_path API 请求路径(含查询参数)
+ * @param string $base_url API 基础 URL
+ * @param string $api_key API Key
+ * @param string $secret_key API Secret Key
+ * @return mixed 返回 API 响应内容
+ */
+function send_request($method, $request_path, $base_url, $api_key, $secret_key) {
+    // 当前 UTC 时间戳
+    $timestamp = gmdate("Y-m-d\TH:i:s.v\Z");
+
+    // 生成签名
+    $signature = generate_signature($timestamp, $method, $request_path, $secret_key);
+
+    // 设置请求头
+    $headers = [
+        "Content-Type: application/json",
+        "MF-ACCESS-KEY: {$api_key}",
+        "MF-ACCESS-SIGN: {$signature}",
+        "MF-ACCESS-TIMESTAMP: {$timestamp}",
+    ];
+
+    // 初始化 CURL
+    $ch = curl_init();
+    curl_setopt($ch, CURLOPT_URL, $base_url . $request_path); // 设置 URL
+    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);        // 设置请求方法
+    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);          // 设置请求头
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);          // 返回响应内容
+
+    // 执行请求
+    $response = curl_exec($ch);
+    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);      // 获取 HTTP 状态码
+    curl_close($ch);
+
+    // 返回结果
+    return [
+        "status_code" => $http_code,
+        "response" => $response
+    ];
 }