feat: 实现联通话费余量只读面板
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
package unicom
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Snapshot struct {
|
||||
Phone string `json:"phone,omitempty"`
|
||||
PhoneMasked string `json:"phoneMasked,omitempty"`
|
||||
PackageName string `json:"packageName,omitempty"`
|
||||
Balance string `json:"balance,omitempty"`
|
||||
BalanceNegative bool `json:"balanceNegative,omitempty"`
|
||||
MonthFee string `json:"monthFee,omitempty"`
|
||||
DataRemain string `json:"dataRemain,omitempty"`
|
||||
DataUsed string `json:"dataUsed,omitempty"`
|
||||
VoiceTotal string `json:"voiceTotal,omitempty"`
|
||||
VoiceRemain string `json:"voiceRemain,omitempty"`
|
||||
Resources []Resource `json:"resources"`
|
||||
Extras []Extra `json:"extras"`
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
Name string `json:"name"`
|
||||
Used string `json:"used"`
|
||||
Remain string `json:"remain"`
|
||||
Total string `json:"total"`
|
||||
Unit string `json:"unit"`
|
||||
Percent float64 `json:"percent"`
|
||||
}
|
||||
|
||||
type Extra struct {
|
||||
Name string `json:"name"`
|
||||
Used string `json:"used"`
|
||||
Fee string `json:"fee"`
|
||||
}
|
||||
|
||||
func BuildSnapshot(phone string, balanceRaw, planRaw json.RawMessage) Snapshot {
|
||||
snap := Snapshot{Phone: strings.TrimSpace(phone), PhoneMasked: MaskPhone(phone), Resources: []Resource{}}
|
||||
var balance map[string]any
|
||||
_ = json.Unmarshal(balanceRaw, &balance)
|
||||
if balance != nil {
|
||||
snap.Balance = textOf(balance["canusefeecust"])
|
||||
if snap.Balance == "" {
|
||||
snap.Balance = textOf(balance["curntbalancecust"])
|
||||
}
|
||||
snap.BalanceNegative = strings.HasPrefix(snap.Balance, "-")
|
||||
snap.MonthFee = textOf(balance["realfeecust"])
|
||||
if owe := textOf(balance["allbowefeecust"]); owe != "" && owe != "0" && owe != "0.00" {
|
||||
snap.Extras = append(snap.Extras, Extra{Name: "欠费", Used: owe, Fee: owe})
|
||||
}
|
||||
}
|
||||
var plan map[string]any
|
||||
_ = json.Unmarshal(planRaw, &plan)
|
||||
if plan != nil {
|
||||
snap.PackageName = textOf(plan["packageName"])
|
||||
if snap.Phone == "" {
|
||||
snap.Phone = textOf(plan["mobile"])
|
||||
snap.PhoneMasked = MaskPhone(snap.Phone)
|
||||
}
|
||||
if remain := textOf(plan["canUseFlowAll"]); remain != "" {
|
||||
snap.DataRemain = remain + unitSuffix(textOf(plan["canuseFlowAllUnit"]))
|
||||
}
|
||||
snap.Resources = resourcesFromPlan(plan)
|
||||
snap.VoiceTotal, snap.VoiceRemain = voiceHeadline(snap.Resources)
|
||||
}
|
||||
return snap
|
||||
}
|
||||
|
||||
func resourcesFromPlan(plan map[string]any) []Resource {
|
||||
blocks, _ := plan["resources"].([]any)
|
||||
out := []Resource{}
|
||||
for _, block := range blocks {
|
||||
item, _ := block.(map[string]any)
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
details, _ := item["details"].([]any)
|
||||
for _, raw := range details {
|
||||
detail, _ := raw.(map[string]any)
|
||||
if detail == nil || textOf(detail["hide"]) == "true" {
|
||||
continue
|
||||
}
|
||||
unit := unitFor(textOf(detail["elemType"]))
|
||||
total := textOf(detail["total"])
|
||||
used := textOf(detail["use"])
|
||||
remain := textOf(detail["remain"])
|
||||
if unit == "GB" {
|
||||
total = mbToGB(total)
|
||||
used = mbToGB(used)
|
||||
remain = mbToGB(remain)
|
||||
}
|
||||
name := textOf(detail["addUpItemName"])
|
||||
if name == "" {
|
||||
name = textOf(detail["feePolicyName"])
|
||||
}
|
||||
out = append(out, Resource{
|
||||
Name: name,
|
||||
Used: used,
|
||||
Remain: remain,
|
||||
Total: total,
|
||||
Unit: unit,
|
||||
Percent: percent(used, total),
|
||||
})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func unitFor(elemType string) string {
|
||||
switch elemType {
|
||||
case "1":
|
||||
return "分钟"
|
||||
case "2":
|
||||
return "条"
|
||||
case "3":
|
||||
return "GB"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func voiceHeadline(list []Resource) (total, remain string) {
|
||||
var sumTotal, sumRemain float64
|
||||
var seen bool
|
||||
for _, item := range list {
|
||||
if item.Unit != "分钟" {
|
||||
continue
|
||||
}
|
||||
t, err1 := strconv.ParseFloat(item.Total, 64)
|
||||
r, err2 := strconv.ParseFloat(item.Remain, 64)
|
||||
if err1 != nil || err2 != nil {
|
||||
continue
|
||||
}
|
||||
sumTotal += t
|
||||
sumRemain += r
|
||||
seen = true
|
||||
}
|
||||
if !seen {
|
||||
return "", ""
|
||||
}
|
||||
return trimNum(sumTotal), trimNum(sumRemain)
|
||||
}
|
||||
|
||||
func mbToGB(raw string) string {
|
||||
v, err := strconv.ParseFloat(strings.TrimSpace(raw), 64)
|
||||
if err != nil {
|
||||
return raw
|
||||
}
|
||||
return trimNum(v / 1024)
|
||||
}
|
||||
|
||||
func percent(used, total string) float64 {
|
||||
u, err1 := strconv.ParseFloat(used, 64)
|
||||
t, err2 := strconv.ParseFloat(total, 64)
|
||||
if err1 != nil || err2 != nil || t <= 0 {
|
||||
return 0
|
||||
}
|
||||
p := u / t * 100
|
||||
if p < 0 {
|
||||
return 0
|
||||
}
|
||||
if p > 100 {
|
||||
return 100
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func trimNum(v float64) string {
|
||||
s := strconv.FormatFloat(v, 'f', 2, 64)
|
||||
s = strings.TrimRight(s, "0")
|
||||
s = strings.TrimRight(s, ".")
|
||||
if s == "" || s == "-" {
|
||||
return "0"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func textOf(v any) string {
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
return strings.TrimSpace(t)
|
||||
case float64:
|
||||
return trimNum(t)
|
||||
case json.Number:
|
||||
return t.String()
|
||||
case bool:
|
||||
if t {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func unitSuffix(unit string) string {
|
||||
if unit == "" {
|
||||
return ""
|
||||
}
|
||||
return " " + unit
|
||||
}
|
||||
|
||||
func MaskPhone(phone string) string {
|
||||
phone = strings.TrimSpace(phone)
|
||||
if len(phone) < 7 {
|
||||
return phone
|
||||
}
|
||||
return phone[:3] + "****" + phone[len(phone)-4:]
|
||||
}
|
||||
Reference in New Issue
Block a user