feat: 提交广电个人中心只读面板
This commit is contained in:
@@ -0,0 +1,425 @@
|
||||
package cbn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"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, prov, city string, balance, bill, resource, subscribe json.RawMessage) Snapshot {
|
||||
snap := Snapshot{
|
||||
Resources: []Resource{},
|
||||
Extras: []Extra{},
|
||||
Bills: []Bill{},
|
||||
}
|
||||
if sub := beanOf(subscribe); sub != nil {
|
||||
if phone == "" {
|
||||
phone = asString(sub["accessNum"])
|
||||
}
|
||||
if opened := asString(sub["openDate"]); len(opened) >= 10 {
|
||||
snap.OpenedAt = opened[:10]
|
||||
}
|
||||
if offers, ok := sub["offerList"].([]any); ok {
|
||||
for _, item := range offers {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if asString(m["isMain"]) == "1" {
|
||||
snap.PackageName = asString(m["offerName"])
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if res := beanOf(resource); res != nil {
|
||||
if list, ok := res["userResList"].([]any); ok {
|
||||
for _, item := range list {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
snap.Resources = append(snap.Resources, formatResource(m))
|
||||
}
|
||||
}
|
||||
if list, ok := res["userExtResList"].([]any); ok {
|
||||
for _, item := range list {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
used := getFloatNumber(jsNumber(atof(asString(m["extTotalValue"])) / 1024 / 1024))
|
||||
fee := toFixed2(atof(asString(m["extDiscntFee"])) / 100)
|
||||
snap.Extras = append(snap.Extras, Extra{Name: "套外国内流量", Used: used, Fee: fee})
|
||||
}
|
||||
}
|
||||
}
|
||||
if bal := beanOf(balance); bal != nil {
|
||||
snap.Cycle = formatCycle(asString(bal["curCycleId"]))
|
||||
spay := asString(bal["spayFee"])
|
||||
if spay != "" && spay != "0" {
|
||||
snap.Balance = "-" + fenToYuan(spay)
|
||||
snap.BalanceNegative = true
|
||||
} else if asString(bal["balance"]) != "" {
|
||||
snap.Balance = fenToYuan(asString(bal["balance"]))
|
||||
}
|
||||
if asString(bal["currealFee"]) != "" {
|
||||
snap.MonthFee = fenToYuan(asString(bal["currealFee"]))
|
||||
}
|
||||
}
|
||||
snap.Bills = formatBills(bill)
|
||||
snap.DataTotal, snap.DataUsed, snap.DataRemain, snap.DataExtra = SummarizeTraffic(snap.Resources, snap.Extras)
|
||||
snap.VoiceTotal, snap.VoiceRemain = SummarizeVoice(snap.Resources)
|
||||
snap.Phone = strings.TrimSpace(phone)
|
||||
snap.PhoneMasked = MaskPhone(snap.Phone)
|
||||
snap.Region = joinRegion(prov, city)
|
||||
if snap.PackageName == "" {
|
||||
for _, item := range snap.Resources {
|
||||
if item.Name != "" {
|
||||
snap.PackageName = item.Name
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return snap
|
||||
}
|
||||
|
||||
func SummarizeTraffic(resources []Resource, extras []Extra) (total, used, remain, extra string) {
|
||||
var totalN, usedN, remainN, extraN float64
|
||||
var hasData bool
|
||||
for _, item := range resources {
|
||||
if item.Unit != "G" {
|
||||
continue
|
||||
}
|
||||
hasData = true
|
||||
totalN += atof(item.Total)
|
||||
usedN += atof(item.Used)
|
||||
remainN += atof(item.Remain)
|
||||
}
|
||||
for _, item := range extras {
|
||||
extraN += atof(item.Used)
|
||||
}
|
||||
if !hasData && len(extras) == 0 {
|
||||
return "", "", "", ""
|
||||
}
|
||||
return getFloatNumber(toFixed2(totalN)), getFloatNumber(toFixed2(usedN)), getFloatNumber(toFixed2(remainN)), getFloatNumber(toFixed2(extraN))
|
||||
}
|
||||
|
||||
func SummarizeVoice(resources []Resource) (total, remain string) {
|
||||
var totalN, remainN float64
|
||||
var has bool
|
||||
for _, item := range resources {
|
||||
if item.Unit != "分钟" {
|
||||
continue
|
||||
}
|
||||
has = true
|
||||
totalN += atof(item.Total)
|
||||
remainN += atof(item.Remain)
|
||||
}
|
||||
if !has {
|
||||
return "", ""
|
||||
}
|
||||
return formatCount(totalN), formatCount(remainN)
|
||||
}
|
||||
|
||||
func formatCount(n float64) string {
|
||||
if n == math.Trunc(n) {
|
||||
return strconv.FormatInt(int64(n), 10)
|
||||
}
|
||||
return getFloatNumber(toFixed2(n))
|
||||
}
|
||||
|
||||
func formatResource(m map[string]any) Resource {
|
||||
busi := asString(m["busiType"])
|
||||
name := asString(m["discntName"])
|
||||
high := asString(m["highFee"])
|
||||
remainRaw := asString(m["balance"])
|
||||
addup := asString(m["addupValue"])
|
||||
if addup == "" {
|
||||
addup = "0"
|
||||
}
|
||||
item := Resource{Name: name}
|
||||
if busi == "5" {
|
||||
item.Unit = "G"
|
||||
item.Total = getFloatNumber(toFixed2(atof(high) / 1024 / 1024))
|
||||
item.Remain = getFloatNumber(toFixed2(atof(remainRaw) / 1024 / 1024))
|
||||
item.Used = subFloatToInt(item.Total, item.Remain)
|
||||
} else {
|
||||
if busi == "1" {
|
||||
item.Unit = "分钟"
|
||||
} else {
|
||||
item.Unit = "条"
|
||||
}
|
||||
item.Total = zero(high)
|
||||
item.Remain = zero(remainRaw)
|
||||
item.Used = zero(addup)
|
||||
}
|
||||
item.Percent = percent(item.Used, item.Total)
|
||||
return item
|
||||
}
|
||||
|
||||
func formatBills(raw json.RawMessage) []Bill {
|
||||
root := object(raw)
|
||||
if root == nil {
|
||||
return []Bill{}
|
||||
}
|
||||
list, ok := root["contentList"].([]any)
|
||||
if !ok {
|
||||
if data, ok := root["data"].(map[string]any); ok {
|
||||
list, _ = data["contentList"].([]any)
|
||||
}
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return []Bill{}
|
||||
}
|
||||
out := make([]Bill, 0, len(list))
|
||||
for _, item := range list {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
group := Bill{Title: asString(m["title"]), Items: []BillItem{}}
|
||||
if rows, ok := m["list"].([]any); ok {
|
||||
for _, row := range rows {
|
||||
rm, ok := row.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
group.Items = append(group.Items, BillItem{Name: asString(rm["name"]), Value: asString(rm["value"])})
|
||||
}
|
||||
}
|
||||
out = append(out, group)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func MaskPhone(phone string) string {
|
||||
if len(phone) == 11 {
|
||||
return phone[:3] + "****" + phone[7:]
|
||||
}
|
||||
return phone
|
||||
}
|
||||
|
||||
func joinRegion(prov, city string) string {
|
||||
prov = strings.TrimSpace(prov)
|
||||
city = strings.TrimSpace(city)
|
||||
switch {
|
||||
case prov != "" && city != "":
|
||||
return prov + " " + city
|
||||
case prov != "":
|
||||
return prov
|
||||
default:
|
||||
return city
|
||||
}
|
||||
}
|
||||
|
||||
func formatCycle(v string) string {
|
||||
if len(v) == 6 {
|
||||
return v[:4] + "-" + v[4:]
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func fenToYuan(s string) string {
|
||||
neg := strings.HasPrefix(s, "-")
|
||||
if neg {
|
||||
s = strings.TrimPrefix(s, "-")
|
||||
}
|
||||
n, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return getFloatNumber(toFixed2(atof(s) / 100))
|
||||
}
|
||||
sign := ""
|
||||
if neg {
|
||||
sign = "-"
|
||||
}
|
||||
return fmt.Sprintf("%s%d.%02d", sign, n/100, n%100)
|
||||
}
|
||||
|
||||
func beanOf(raw json.RawMessage) map[string]any {
|
||||
root := object(raw)
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
if bean, ok := root["intfResultBean"].(map[string]any); ok {
|
||||
return bean
|
||||
}
|
||||
if data, ok := root["data"].(map[string]any); ok {
|
||||
if bean, ok := data["intfResultBean"].(map[string]any); ok {
|
||||
return bean
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func object(raw json.RawMessage) map[string]any {
|
||||
if len(bytesTrim(raw)) == 0 {
|
||||
return nil
|
||||
}
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func bytesTrim(raw json.RawMessage) []byte {
|
||||
return []byte(strings.TrimSpace(string(raw)))
|
||||
}
|
||||
|
||||
func asString(v any) string {
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
return t
|
||||
case float64:
|
||||
if math.IsNaN(t) || math.IsInf(t, 0) {
|
||||
return ""
|
||||
}
|
||||
if t == math.Trunc(t) && math.Abs(t) < 1e16 {
|
||||
return strconv.FormatInt(int64(t), 10)
|
||||
}
|
||||
return strconv.FormatFloat(t, 'f', -1, 64)
|
||||
case json.Number:
|
||||
return t.String()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func atof(s string) float64 {
|
||||
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func zero(s string) string {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return "0"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func jsNumber(f float64) string {
|
||||
return strconv.FormatFloat(f, 'f', -1, 64)
|
||||
}
|
||||
|
||||
func toFixed2(f float64) string {
|
||||
return strconv.FormatFloat(f, 'f', 2, 64)
|
||||
}
|
||||
|
||||
func getFloatNumber(v string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range v {
|
||||
if (r >= '0' && r <= '9') || r == '.' || r == '|' {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
t := strings.TrimLeft(b.String(), "0")
|
||||
if !strings.Contains(t, ".") {
|
||||
t += ".00"
|
||||
}
|
||||
if strings.HasPrefix(t, ".") {
|
||||
t = "0" + t
|
||||
}
|
||||
t += "00"
|
||||
for i := 0; i < len(t); i++ {
|
||||
if t[i] != '.' || i+3 > len(t) {
|
||||
continue
|
||||
}
|
||||
j := i
|
||||
for j > 0 && t[j-1] >= '0' && t[j-1] <= '9' {
|
||||
j--
|
||||
}
|
||||
if j < i {
|
||||
return t[j : i+3]
|
||||
}
|
||||
}
|
||||
return "0.00"
|
||||
}
|
||||
|
||||
func subFloatToInt(a, b string) string {
|
||||
ra := decimalPlaces(a)
|
||||
rb := decimalPlaces(b)
|
||||
r := ra
|
||||
if rb > r {
|
||||
r = rb
|
||||
}
|
||||
scale := math.Pow10(r)
|
||||
af := atof(a)
|
||||
bf := atof(b)
|
||||
return strconv.FormatFloat((af*scale-bf*scale)/scale, 'f', r, 64)
|
||||
}
|
||||
|
||||
func decimalPlaces(s string) int {
|
||||
i := strings.IndexByte(s, '.')
|
||||
if i < 0 {
|
||||
return 0
|
||||
}
|
||||
return len(s) - i - 1
|
||||
}
|
||||
|
||||
func percent(used, total string) float64 {
|
||||
u := atof(used)
|
||||
t := atof(total)
|
||||
if t <= 0 {
|
||||
return 0
|
||||
}
|
||||
p := u / t * 100
|
||||
if p < 0 {
|
||||
return 0
|
||||
}
|
||||
if p > 100 {
|
||||
return 100
|
||||
}
|
||||
return math.Round(p*10) / 10
|
||||
}
|
||||
Reference in New Issue
Block a user