feat: 实现联通话费余量只读面板
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package unicom
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var bareKeyRE = regexp.MustCompile(`([{\[,]\s*)([A-Za-z_][A-Za-z0-9_]*)\s*:`)
|
||||
|
||||
func parseJSONP(body string) (map[string]any, error) {
|
||||
text := strings.TrimSpace(body)
|
||||
if i := strings.Index(text, "("); i >= 0 && strings.Contains(text, ")") {
|
||||
text = text[i+1 : strings.LastIndex(text, ")")]
|
||||
text = strings.TrimSpace(text)
|
||||
}
|
||||
if text == "" {
|
||||
return nil, errors.New("接口没有返回结果")
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(text))
|
||||
dec.UseNumber()
|
||||
var out map[string]any
|
||||
if err := dec.Decode(&out); err == nil {
|
||||
return out, nil
|
||||
}
|
||||
fixed := bareKeyRE.ReplaceAllString(text, `$1"$2":`)
|
||||
dec = json.NewDecoder(strings.NewReader(fixed))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&out); err != nil {
|
||||
return nil, errors.New("接口结果无法解析")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func asString(v any) string {
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
return t
|
||||
case json.Number:
|
||||
return t.String()
|
||||
case float64:
|
||||
return strconv.FormatFloat(t, 'f', -1, 64)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user