-
Notifications
You must be signed in to change notification settings - Fork 3
/
phonequery.go
112 lines (84 loc) · 2.01 KB
/
phonequery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"flag"
"fmt"
"log"
"os"
"net/http"
"strings"
"github.com/devfeel/dotweb"
"github.com/zhujq/phonedata"
)
type App struct {
Web *dotweb.DotWeb
}
type ResBody struct {
Status string `json:"status"`
PhoneNum string `json:"PhoneNum"`
Province string `json:"Province"`
City string `json:"City "`
ZipCode string `json:"ZipCode"`
AreaZone string `json:"AreaZone"`
CardType string `json:"CardType"`
}
func NewApp() *App {
var a = &App{}
a.Web = dotweb.New()
return a
}
var app = NewApp()
func main() {
var err error
var (
version = flag.Bool("version", false, "version v1.0")
port = flag.Int("port", 8080, "listen port.")
)
flag.Parse()
if *version {
fmt.Println("v1.0")
os.Exit(0)
}
if err != nil {
log.Fatal(err)
os.Exit(1)
}
InitRoute(app.Web.HttpServer)
log.Println("Start China Phone Query Server on ", *port)
app.Web.StartServer(*port)
}
func indexHandler(ctx dotweb.Context) error {
phonenum := ctx.QueryString("phonenum")
if phonenum == "" {
log.Println("ERROR: 没有提供电话号码")
return ctx.WriteString("欢迎使用中国电话号码查询系统,请在网址后输入 /?phonenum=电话号码 查询,如https://phone.zhujq.ml/?phonenum=13988888888")
}
var message = ResBody{
Status: "failed",
PhoneNum: "",
Province: "",
City: "",
ZipCode: "",
AreaZone: "",
CardType: "",
}
log.Println(phonenum)
message.PhoneNum = phonenum
if strings.HasPrefix(phonenum,"86"){
phonenum = strings.TrimPrefix(phonenum,"86")
}
phoneresult, err := phonedata.Find(phonenum)
if err != nil {
log.Println("error:", err)
return ctx.WriteJsonC(http.StatusNotFound, message)
}
message.Status = "success"
message.Province = phoneresult.Province
message.City = phoneresult.City
message.ZipCode = phoneresult.ZipCode
message.AreaZone = phoneresult.AreaZone
message.CardType = phoneresult.CardType
return ctx.WriteJson(message)
}
func InitRoute(server *dotweb.HttpServer) {
server.GET("/", indexHandler)
}