diff --git a/internal/unicom/client.go b/internal/unicom/client.go index dc3a5dc..e276235 100644 --- a/internal/unicom/client.go +++ b/internal/unicom/client.go @@ -46,6 +46,7 @@ type Client struct { loginReferer string fee billAPI plan billAPI + checkURL string qrSec string loginReady bool } @@ -216,11 +217,18 @@ func (c *Client) Query(ctx context.Context, phone string) (Snapshot, error) { if err != nil { return Snapshot{}, err } + if phone == "" { + phone = c.syncMall(ctx) + } else { + c.syncMall(ctx) + } extra := url.Values{ "ticket": {""}, "ticketPhone": {""}, "ticketChannel": {""}, - "userNumber": {""}, + } + if phone != "" { + extra.Set("userNumber", phone) } if c.plan.Language != "" { extra.Set("language", c.plan.Language) @@ -257,7 +265,7 @@ func (c *Client) finishQR(ctx context.Context) (QRStatus, error) { if err := c.land(ctx, servicePage); err != nil { return QRStatus{}, err } - return QRStatus{Message: "登录成功"}, nil + return QRStatus{Message: "登录成功", Result: LoginResult{Phone: phoneFromPIW(asString(data["piw"]))}}, nil } func (c *Client) prepareLogin(ctx context.Context) error { @@ -353,6 +361,9 @@ func (c *Client) prepareBill(ctx context.Context) error { texts = append(texts, js) } nav := resolveNav(texts) + if c.checkURL == "" { + c.checkURL = findCheckURL(texts) + } if nav == "" { return errors.New("查询页没有导航接口") } @@ -610,6 +621,91 @@ func (c *Client) setDomainCookie(name, value string) { }}) } +func (c *Client) firstCookie(name string) string { + for _, host := range []string{"www.10010.com", "uac.10010.com", "mxx.client.10010.com", "iservice.10010.com"} { + if value := c.cookie(host, name); value != "" { + return value + } + } + return "" +} + +func (c *Client) syncMall(ctx context.Context) string { + if c.checkURL == "" { + return "" + } + jut := c.firstCookie("JUT") + if jut == "" { + return "" + } + body, err := c.postForm(ctx, c.checkURL+"?="+milli(), servicePage, url.Values{ + "_uop_id": {c.firstCookie("_uop_id")}, + "jutThird": {jut}, + "procode": {c.firstCookie("userprocode")}, + }) + if err != nil { + return "" + } + var wrap struct { + IsLogin bool `json:"isLogin"` + UserInfo map[string]any `json:"userInfo"` + } + if json.Unmarshal(body, &wrap) != nil || !wrap.IsLogin || wrap.UserInfo == nil { + return "" + } + c.setDomainCookie("loginflag", "true") + if prov := pad3(asString(wrap.UserInfo["provincecode"])); prov != "" { + c.setDomainCookie("userprocode", prov) + } + if city := asString(wrap.UserInfo["citycode"]); city != "" { + c.setDomainCookie("citycode", city) + c.setDomainCookie("city", city) + } + if enc := asString(wrap.UserInfo["encryptPhoneNumber"]); enc != "" { + c.setDomainCookie("u_account", url.QueryEscape(enc)) + } + debugf("商城登录状态已同步") + phone := asString(wrap.UserInfo["usernumber"]) + if len(phone) == 11 { + return phone + } + return "" +} + +func findCheckURL(texts []string) string { + const marker = "/mall/service/check/checklogin" + for _, text := range texts { + if strings.Contains(text, marker) { + return "https://www.10010.com" + marker + } + } + return "" +} + +func phoneFromPIW(raw string) string { + text, err := url.QueryUnescape(raw) + if err != nil || text == "" { + text = raw + } + var wrap struct { + RME struct { + Phone string `json:"u"` + } `json:"rme"` + } + if json.Unmarshal([]byte(text), &wrap) != nil || len(wrap.RME.Phone) != 11 { + return "" + } + return wrap.RME.Phone +} + +func pad3(s string) string { + s = strings.TrimSpace(s) + for len(s) > 0 && len(s) < 3 { + s = "0" + s + } + return s +} + func (c *Client) dropAccount() { u, err := url.Parse(uacOrigin + "/") if err != nil { diff --git a/internal/unicom/format.go b/internal/unicom/format.go index cc7e1a7..94cbbfe 100644 --- a/internal/unicom/format.go +++ b/internal/unicom/format.go @@ -13,6 +13,8 @@ type Snapshot struct { Balance string `json:"balance,omitempty"` BalanceNegative bool `json:"balanceNegative,omitempty"` MonthFee string `json:"monthFee,omitempty"` + CarryOver string `json:"carryOver,omitempty"` + MonthIn string `json:"monthIn,omitempty"` DataRemain string `json:"dataRemain,omitempty"` DataUsed string `json:"dataUsed,omitempty"` VoiceTotal string `json:"voiceTotal,omitempty"` @@ -41,12 +43,25 @@ func BuildSnapshot(phone string, balanceRaw, planRaw json.RawMessage) Snapshot { var balance map[string]any _ = json.Unmarshal(balanceRaw, &balance) if balance != nil { - snap.Balance = textOf(balance["canusefeecust"]) + snap.Balance = textOf(balance["curntbalancecust"]) if snap.Balance == "" { - snap.Balance = textOf(balance["curntbalancecust"]) + snap.Balance = textOf(balance["canusefeecust"]) } snap.BalanceNegative = strings.HasPrefix(snap.Balance, "-") - snap.MonthFee = textOf(balance["realfeecust"]) + if truthy(balance["newVersion"]) { + snap.MonthFee = textOf(balance["realfeecustnew"]) + snap.CarryOver = textOf(balance["newCarryForwardFromLastMonth"]) + snap.MonthIn = textOf(balance["newDepositForTheMonth"]) + } + if snap.MonthFee == "" { + snap.MonthFee = textOf(balance["realfeecust"]) + } + if snap.CarryOver == "" { + snap.CarryOver = textOf(balance["carryForwardFromLastMonth"]) + } + if snap.MonthIn == "" { + snap.MonthIn = textOf(balance["depositForTheMonth"]) + } if owe := textOf(balance["allbowefeecust"]); owe != "" && owe != "0" && owe != "0.00" { snap.Extras = append(snap.Extras, Extra{Name: "欠费", Used: owe, Fee: owe}) } @@ -177,6 +192,17 @@ func trimNum(v float64) string { return s } +func truthy(v any) bool { + switch t := v.(type) { + case bool: + return t + case string: + return t == "true" || t == "1" + default: + return false + } +} + func textOf(v any) string { switch t := v.(type) { case string: diff --git a/internal/unicom/format_test.go b/internal/unicom/format_test.go index 7390f2b..162adf1 100644 --- a/internal/unicom/format_test.go +++ b/internal/unicom/format_test.go @@ -28,3 +28,20 @@ func TestBuildSnapshot(t *testing.T) { t.Fatalf("resources %#v", snap.Resources) } } + +func TestOfficialFeeFields(t *testing.T) { + balance := []byte(`{ + "newVersion": true, + "curntbalancecust": "261.09", + "canusefeecust": "731.41", + "realfeecust": "470.32", + "realfeecustnew": "361.00", + "newCarryForwardFromLastMonth": "453.88", + "carryForwardFromLastMonth": "563.20", + "newDepositForTheMonth": "168.21" + }`) + snap := BuildSnapshot("", balance, nil) + if snap.Balance != "261.09" || snap.MonthFee != "361.00" || snap.CarryOver != "453.88" || snap.MonthIn != "168.21" { + t.Fatalf("%#v", snap) + } +} diff --git a/internal/unicom/script_test.go b/internal/unicom/script_test.go index 63379ad..db5a056 100644 --- a/internal/unicom/script_test.go +++ b/internal/unicom/script_test.go @@ -1,10 +1,18 @@ package unicom import ( + "net/url" "strings" "testing" ) +func TestPhoneFromPIW(t *testing.T) { + raw := url.QueryEscape(`{"rme":{"u":"13800000000"}}`) + if phoneFromPIW(raw) != "13800000000" { + t.Fatal(phoneFromPIW(raw)) + } +} + func TestJSONPCallbackName(t *testing.T) { name := jsonpCallback() if !strings.HasPrefix(name, "jQuery") || !strings.Contains(name, "_") { diff --git a/main.go b/main.go index b4c54be..cd357a8 100644 --- a/main.go +++ b/main.go @@ -214,7 +214,7 @@ func (a *App) qrPoll(w http.ResponseWriter, r *http.Request) { return } if !status.Waiting && !status.Expired { - a.acceptLogin("") + a.acceptLogin(status.Result.Phone) } writeJSON(w, http.StatusOK, map[string]any{ "ok": true, diff --git a/web/index.html b/web/index.html index f94672c..4d6b5b9 100644 --- a/web/index.html +++ b/web/index.html @@ -83,10 +83,10 @@