diff --git a/README.md b/README.md index 75bd684..c0a1cb3 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # devops-api -Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,提供开发/运维常用操作的HTTP API接口: IP地址查询、工作日节假日判断、微信报警、钉钉报警、2步验证、密码存储、发送邮件、生成随机密码等功能 +Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,提供开发/运维常用操作的HTTP API接口: 手机归属地查询、IP地址查询、工作日节假日判断、微信报警、钉钉报警、2步验证、密码存储、发送邮件、生成随机密码等功能 # 主要功能 +- 手机归属地查询 - IP地址查询 - 工作日节假日判断 - 微信报警 @@ -21,6 +22,7 @@ Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口, - [安装使用](#安装使用) - [依赖](#依赖) - [功能列表](#功能列表) + - [手机归属地查询](#手机归属地查询) - [IP地址查询](#ip地址查询) - [工作日节假日判断](#工作日节假日判断) - [设置节假日和工作日](#设置节假日和工作日) @@ -81,6 +83,7 @@ Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口, - security.conf 安全相关的配置 - twostep.conf 2步验证相关 - weixin.conf 微信报警相关 +- phone.conf 手机归属地查询配置 主配置文件 app.conf 通过include的方式加载其他的配置文件 @@ -169,11 +172,42 @@ go get github.com/chanyipiaomiao/ip2region/binding/golang # API +## 手机归属地查询 + +本功能使用了 [xluohome](https://github.com/xluohome/phonedata) 项目提供的手机归属地数据库 + +首先进入到script目录,执行 get_phone_dat.sh 来下载数据文件,可以定期执行脚本获取最新的. + +```sh +/api/v1/queryphone?phone=手机号 + +phone 要查询的手机号 +``` + +返回: + +```sh +{ + "data": { + "AreaZone": "021", + "CardType": "中国移动", + "City": "上海", + "PhoneNum": "xxxxxxxxxx", + "Province": "上海", + "ZipCode": "200000" + }, + "entryType": "Query Phone Location", + "errmsg": "", + "requestId": "0860edaa-db7f-46ee-ac89-d41eeb2ed80d", + "statuscode": 0 +} +``` + ## IP地址查询 本功能使用了 [狮子的魂](https://gitee.com/lionsoul/ip2region) 项目提供的IP地址数据库. -首先要执行 script 目录下的 gen_ip_region.sh 脚本, 来下载IP地址数据库, 可以定期执行脚本. +首先进入到script目录, 执行 gen_ip_region.sh 脚本, 来下载IP地址数据库, 可以定期执行脚本获取最新的. ```sh diff --git a/common/phone.go b/common/phone.go new file mode 100644 index 0000000..b9e471a --- /dev/null +++ b/common/phone.go @@ -0,0 +1,23 @@ +package common + +import ( + "fmt" + "github.com/astaxie/beego" + "github.com/chanyipiaomiao/phonedata" +) + +func QueryPhone(phone string) (map[string]string, error) { + dbPath := beego.AppConfig.String("phone::dbPath") + if dbPath == "" { + return nil, fmt.Errorf("not found phone dat file") + } + p, err := phonedata.NewPhoneQuery(dbPath) + if err != nil { + return nil, err + } + m, err := p.Query(phone) + if err != nil { + return nil, err + } + return m, nil +} diff --git a/conf/app.conf b/conf/app.conf index b04426f..bc640b3 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -20,4 +20,5 @@ include "weixin.conf" include "twostep.conf" include "email.conf" include "authpassword.conf" -include "security.conf" \ No newline at end of file +include "security.conf" +include "phone.conf" diff --git a/conf/phone.conf b/conf/phone.conf new file mode 100644 index 0000000..9153b52 --- /dev/null +++ b/conf/phone.conf @@ -0,0 +1,2 @@ +[phone] +dbPath = data/phone.dat \ No newline at end of file diff --git a/controllers/common.go b/controllers/common.go index f469eb3..9ca5b01 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -204,3 +204,8 @@ type HolidayController struct { type QueryIPController struct { BaseController } + +// PhoneController 手机归属地查询 +type PhoneController struct { + BaseController +} diff --git a/controllers/phone.go b/controllers/phone.go new file mode 100644 index 0000000..73bd052 --- /dev/null +++ b/controllers/phone.go @@ -0,0 +1,20 @@ +package controllers + +import ( + "devops-api/common" + "fmt" +) + +var ( + queryPhoneEntryType = "Query Phone Location" +) + +func (p *PhoneController) Get() { + phone := p.GetString("phone") + m, err := common.QueryPhone(phone) + if err != nil { + p.JsonError(queryPhoneEntryType, fmt.Sprintf("%s", err), StringMap{}, true) + return + } + p.JsonOK(queryPhoneEntryType, m, true) +} diff --git a/routers/router.go b/routers/router.go index d7ec855..d39c1bf 100644 --- a/routers/router.go +++ b/routers/router.go @@ -42,6 +42,9 @@ func init() { beego.NSNamespace("/queryip", beego.NSRouter("", &controllers.QueryIPController{}), ), + beego.NSNamespace("/queryphone", + beego.NSRouter("", &controllers.PhoneController{}), + ), beego.NSNamespace("/version", beego.NSRouter("", &controllers.VersionController{}), ), diff --git a/script/gen_ip_region.sh b/script/gen_ip_region.sh old mode 100644 new mode 100755 diff --git a/script/get_phone_dat.sh b/script/get_phone_dat.sh new file mode 100755 index 0000000..58d33fc --- /dev/null +++ b/script/get_phone_dat.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +git clone https://github.com/xluohome/phonedata + +[[ -e ../data/phone.dat ]] && mv ../data/phone.dat ../data/phone_`date +%F_%H%M%S`.dat + +cp phonedata/phone.dat ../data/phone.dat + +rm -rf phonedata \ No newline at end of file