alroyso hai 1 ano
pai
achega
ed4f2ec0ba
Modificáronse 1 ficheiros con 20 adicións e 1 borrados
  1. 20 1
      app/Models/User.php

+ 20 - 1
app/Models/User.php

@@ -390,7 +390,26 @@ class User extends Authenticatable implements JWTSubject
 
     public function incrementData(int $data): bool
     {// 添加用户流量
-        $this->transfer_enable += $data;
+
+        //用户的 剩余流量
+        $totalTransfer = $this->transfer_enable;
+        $usedTransfer = $this->u + $this->d;
+        $unusedTraffic = $totalTransfer - $usedTransfer; // 计算剩余流量
+
+        // 如果剩余流量为负数,将其设置为0
+        if ($unusedTraffic < 0) {
+            $unusedTraffic = 0;
+            $this->d = 0;
+            $this->u = 0;
+        }
+        // 用户的使用流量超过总流量就
+        if ($unusedTraffic > $totalTransfer) {
+            $unusedTraffic = 0;
+            $this->d = 0;
+            $this->u = 0;
+        }
+
+        $this->transfer_enable = $data + $unusedTraffic;
 
         return $this->save();
     }