373 lines
8.7 KiB
Go
373 lines
8.7 KiB
Go
package cmnet
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Snapshot struct {
|
|
Phone string `json:"phone,omitempty"`
|
|
PhoneMasked string `json:"phoneMasked,omitempty"`
|
|
Region string `json:"region,omitempty"`
|
|
PackageName string `json:"packageName,omitempty"`
|
|
OpenedAt string `json:"openedAt,omitempty"`
|
|
Cycle string `json:"cycle,omitempty"`
|
|
Balance string `json:"balance,omitempty"`
|
|
BalanceNegative bool `json:"balanceNegative,omitempty"`
|
|
MonthFee string `json:"monthFee,omitempty"`
|
|
DataTotal string `json:"dataTotal,omitempty"`
|
|
DataUsed string `json:"dataUsed,omitempty"`
|
|
DataRemain string `json:"dataRemain,omitempty"`
|
|
DataExtra string `json:"dataExtra,omitempty"`
|
|
VoiceTotal string `json:"voiceTotal,omitempty"`
|
|
VoiceRemain string `json:"voiceRemain,omitempty"`
|
|
Resources []Resource `json:"resources"`
|
|
Extras []Extra `json:"extras"`
|
|
Bills []Bill `json:"bills"`
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
type Bill struct {
|
|
Title string `json:"title"`
|
|
Items []BillItem `json:"items"`
|
|
}
|
|
|
|
type BillItem struct {
|
|
Name string `json:"name"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
func BuildSnapshot(phone, provName string, realRaw, planRaw json.RawMessage) Snapshot {
|
|
snap := Snapshot{
|
|
Phone: strings.TrimSpace(phone),
|
|
PhoneMasked: MaskPhone(phone),
|
|
Region: provName,
|
|
PackageName: "中国移动",
|
|
Resources: []Resource{},
|
|
Extras: []Extra{},
|
|
Bills: []Bill{},
|
|
}
|
|
var real map[string]any
|
|
_ = json.Unmarshal(realRaw, &real)
|
|
if real != nil {
|
|
snap.Balance = text(real["realBalanceFee"])
|
|
snap.MonthFee = text(real["realFee"])
|
|
if owe := text(real["oweFee"]); owe != "" && owe != "0" && owe != "0.00" {
|
|
snap.Extras = append(snap.Extras, Extra{Name: "欠费", Used: owe, Fee: owe})
|
|
}
|
|
snap.Bills = []Bill{{
|
|
Title: "话费",
|
|
Items: []BillItem{
|
|
{Name: "话费余额", Value: snap.Balance},
|
|
{Name: "当月消费", Value: snap.MonthFee},
|
|
{Name: "欠费", Value: text(real["oweFee"])},
|
|
},
|
|
}}
|
|
}
|
|
snap.Resources = resourcesFromPlan(planRaw)
|
|
snap.DataTotal, snap.DataUsed, snap.DataRemain, snap.DataExtra, snap.VoiceTotal, snap.VoiceRemain = officialTotals(planRaw)
|
|
return snap
|
|
}
|
|
|
|
func resourcesFromPlan(raw json.RawMessage) []Resource {
|
|
var plans []struct {
|
|
Arr []struct {
|
|
MealName string `json:"mealName"`
|
|
ResInfos []struct {
|
|
SecResInfos []struct {
|
|
ResConName string `json:"resConName"`
|
|
ResConInfo struct {
|
|
TotalMeal string `json:"totalMeal"`
|
|
UseMeal string `json:"useMeal"`
|
|
BalMeal string `json:"balMeal"`
|
|
Unit string `json:"unit"`
|
|
} `json:"resConInfo"`
|
|
} `json:"secResInfos"`
|
|
} `json:"resInfos"`
|
|
} `json:"arr"`
|
|
}
|
|
if json.Unmarshal(raw, &plans) != nil {
|
|
return []Resource{}
|
|
}
|
|
out := []Resource{}
|
|
for _, plan := range plans {
|
|
for _, meal := range plan.Arr {
|
|
for _, res := range meal.ResInfos {
|
|
for _, sec := range res.SecResInfos {
|
|
info := sec.ResConInfo
|
|
name := sec.ResConName
|
|
if meal.MealName != "" {
|
|
name = meal.MealName
|
|
if sec.ResConName != "" {
|
|
name = meal.MealName + " · " + sec.ResConName
|
|
}
|
|
}
|
|
item := scaleResource(name, info.UseMeal, info.BalMeal, info.TotalMeal, info.Unit)
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func scaleResource(name, used, remain, total, unit string) Resource {
|
|
item := Resource{Name: name}
|
|
switch unit {
|
|
case "01":
|
|
item.Unit = "分钟"
|
|
item.Used, item.Remain, item.Total = num(used), num(remain), num(total)
|
|
case "02":
|
|
item.Unit = "条"
|
|
item.Used, item.Remain, item.Total = num(used), num(remain), num(total)
|
|
case "03":
|
|
item.Unit = "G"
|
|
item.Used, item.Remain, item.Total = kbToG(used), kbToG(remain), kbToG(total)
|
|
case "04":
|
|
item.Unit = "MB"
|
|
item.Used, item.Remain, item.Total = kbToMB(used), kbToMB(remain), kbToMB(total)
|
|
default:
|
|
item.Unit = unit
|
|
item.Used, item.Remain, item.Total = num(used), num(remain), num(total)
|
|
}
|
|
item.Percent = percent(item.Used, item.Total)
|
|
return item
|
|
}
|
|
|
|
type planGroup struct {
|
|
Type any `json:"type"`
|
|
Arr []planMeal `json:"arr"`
|
|
}
|
|
|
|
type planMeal struct {
|
|
ExpireFlag any `json:"expireFlag"`
|
|
ResInfos []planRes `json:"resInfos"`
|
|
}
|
|
|
|
type planRes struct {
|
|
ResCode string `json:"resCode"`
|
|
SecResInfos []planSec `json:"secResInfos"`
|
|
}
|
|
|
|
type planSec struct {
|
|
ResConName string `json:"resConName"`
|
|
ResConInfo planAmount `json:"resConInfo"`
|
|
}
|
|
|
|
type planAmount struct {
|
|
TotalMeal string `json:"totalMeal"`
|
|
UseMeal string `json:"useMeal"`
|
|
BalMeal string `json:"balMeal"`
|
|
Unit string `json:"unit"`
|
|
}
|
|
|
|
// officialTotals follows the shop homepage: voice is resCode 01, and general
|
|
// data is resCode 04 except items whose name starts with "10-" (定向流量).
|
|
// Only the current, unexpired group (type 1, expireFlag not 1) is included.
|
|
func officialTotals(raw json.RawMessage) (dataTotal, dataUsed, dataRemain, dataExtra, voiceTotal, voiceRemain string) {
|
|
var groups []planGroup
|
|
if json.Unmarshal(raw, &groups) != nil {
|
|
return
|
|
}
|
|
var current []planMeal
|
|
found := false
|
|
for _, group := range groups {
|
|
if isOne(group.Type) {
|
|
current = group.Arr
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
return
|
|
}
|
|
var voiceT, voiceR float64
|
|
var voiceHas bool
|
|
var dataT, dataU, dataR float64
|
|
var dataHas bool
|
|
for _, meal := range current {
|
|
if isOne(meal.ExpireFlag) {
|
|
continue
|
|
}
|
|
for _, res := range meal.ResInfos {
|
|
switch res.ResCode {
|
|
case "01":
|
|
for _, sec := range res.SecResInfos {
|
|
voiceHas = true
|
|
voiceT += atof(sec.ResConInfo.TotalMeal)
|
|
voiceR += atof(sec.ResConInfo.BalMeal)
|
|
}
|
|
case "04":
|
|
for _, sec := range res.SecResInfos {
|
|
if strings.HasPrefix(sec.ResConName, "10-") || !numericMeal(sec.ResConInfo.TotalMeal) {
|
|
continue
|
|
}
|
|
kbT, okT := amountKB(sec.ResConInfo.TotalMeal, sec.ResConInfo.Unit)
|
|
kbU, okU := amountKB(sec.ResConInfo.UseMeal, sec.ResConInfo.Unit)
|
|
kbR, okR := amountKB(sec.ResConInfo.BalMeal, sec.ResConInfo.Unit)
|
|
if !okT && !okU && !okR {
|
|
continue
|
|
}
|
|
dataHas = true
|
|
dataT += kbT
|
|
dataU += kbU
|
|
dataR += kbR
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if voiceHas {
|
|
voiceTotal = formatCount(voiceT)
|
|
voiceRemain = formatCount(voiceR)
|
|
}
|
|
if dataHas {
|
|
dataTotal = formatGB(dataT)
|
|
dataUsed = formatGB(dataU)
|
|
dataRemain = formatGB(dataR)
|
|
dataExtra = "0.00"
|
|
}
|
|
return
|
|
}
|
|
|
|
func isOne(v any) bool {
|
|
switch t := v.(type) {
|
|
case string:
|
|
return t == "1"
|
|
case float64:
|
|
return t == 1
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func numericMeal(s string) bool {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return true
|
|
}
|
|
_, err := strconv.ParseFloat(s, 64)
|
|
return err == nil
|
|
}
|
|
|
|
func amountKB(amount, unit string) (float64, bool) {
|
|
s := strings.TrimSpace(amount)
|
|
if s == "" {
|
|
return 0, true
|
|
}
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
switch unit {
|
|
case "04":
|
|
return f * 1024, true
|
|
case "05":
|
|
return f * 1024 * 1024, true
|
|
default:
|
|
return f, true
|
|
}
|
|
}
|
|
|
|
func formatCount(f float64) string {
|
|
if f == math.Trunc(f) {
|
|
return strconv.FormatFloat(f, 'f', 0, 64)
|
|
}
|
|
return strconvFormat(f, 2)
|
|
}
|
|
|
|
func formatGB(kb float64) string {
|
|
return strconv.FormatFloat(kb/1024/1024, 'f', 2, 64)
|
|
}
|
|
|
|
func kbToG(s string) string {
|
|
return fixed(atof(s) / 1024 / 1024)
|
|
}
|
|
|
|
func kbToMB(s string) string {
|
|
return fixed(atof(s) / 1024)
|
|
}
|
|
|
|
func fixed(f float64) string {
|
|
return strconvFormat(f, 2)
|
|
}
|
|
|
|
func num(s string) string {
|
|
f := atof(s)
|
|
if f == math.Trunc(f) {
|
|
return strconvFormat(f, 0)
|
|
}
|
|
return fixed(f)
|
|
}
|
|
|
|
func strconvFormat(f float64, prec int) string {
|
|
s := strconvFormatRaw(f, prec)
|
|
if !strings.Contains(s, ".") {
|
|
return s
|
|
}
|
|
s = strings.TrimRight(strings.TrimRight(s, "0"), ".")
|
|
if s == "" || s == "-" {
|
|
return "0"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func strconvFormatRaw(f float64, prec int) string {
|
|
return strconv.FormatFloat(f, 'f', prec, 64)
|
|
}
|
|
|
|
func atof(s string) float64 {
|
|
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return f
|
|
}
|
|
|
|
func text(v any) string {
|
|
switch t := v.(type) {
|
|
case string:
|
|
return t
|
|
case float64:
|
|
return fixed(t)
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func percent(used, total string) float64 {
|
|
t := atof(total)
|
|
if t <= 0 {
|
|
return 0
|
|
}
|
|
p := atof(used) / t * 100
|
|
if p < 0 {
|
|
return 0
|
|
}
|
|
if p > 100 {
|
|
return 100
|
|
}
|
|
return math.Round(p*10) / 10
|
|
}
|
|
|
|
func MaskPhone(phone string) string {
|
|
if len(phone) == 11 {
|
|
return phone[:3] + "****" + phone[7:]
|
|
}
|
|
return phone
|
|
}
|