| 137 | } |
| 138 | |
| 139 | function sendNotification(usageRate, expire, infoList) { |
| 140 | if (!args.alert) return; |
| 141 | let title = args.title || "Sub Info"; |
| 142 | let subtitle = infoList[0]; |
| 143 | let body = infoList.slice(1).join("\n"); |
| 144 | usageRate = usageRate * 100; |
| 145 | |
| 146 | if (resetDay <= today) month += 1; |
| 147 | let resetTime = new Date(year, month, resetDay); |
| 148 | //通知计数器,每月重置日重置 |
| 149 | let notifyCounter = JSON.parse($persistentStore.read(title) || "{}"); |
| 150 | if (!notifyCounter[resetTime]) { |
| 151 | notifyCounter = { |
| 152 | [resetTime]: { usageRate: 80, resetDayLeft: 3, expire: 31, resetDay: 1 }, |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | let count = notifyCounter[resetTime]; |
| 157 | |
| 158 | if (usageRate > count.usageRate && resetDay != today) { |
| 159 | $notification.post( |
| 160 | `${title} | 剩余流量不足${Math.ceil(100 - usageRate)}%`, |
| 161 | subtitle, |
| 162 | body |
| 163 | ); |
| 164 | while (usageRate > count.usageRate) { |
| 165 | if (count.usageRate < 95) { |
| 166 | count.usageRate += 5; |
| 167 | } else { |
| 168 | count.usageRate += 4; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | if (resetDayLeft && resetDayLeft < count.resetDayLeft && resetDay != today) { |
| 173 | $notification.post( |
| 174 | `${title} | 流量将在${resetDayLeft}天后重置`, |
| 175 | subtitle, |
| 176 | body |
| 177 | ); |
| 178 | count.resetDayLeft = resetDayLeft; |
| 179 | } |
| 180 | if (resetDay == today && count.resetDay && usageRate < 5) { |
| 181 | $notification.post(`${title} | 流量已重置`, subtitle, body); |
| 182 | count.resetDay = 0; |
| 183 | } |
| 184 | if (expire) { |
| 185 | let diff = (new Date(expire) - now) / (1000 * 3600 * 24); |
| 186 | if (diff < count.expire) { |
| 187 | $notification.post( |
| 188 | `${title} | 套餐剩余时间不足${Math.ceil(diff)}天`, |
| 189 | subtitle, |
| 190 | body |
| 191 | ); |
| 192 | count.expire -= 5; |
| 193 | } |
| 194 | } |
| 195 | $persistentStore.write(JSON.stringify(notifyCounter), title); |
| 196 | } |