Skip to content

Commit

Permalink
增加手机归属地查询
Browse files Browse the repository at this point in the history
  • Loading branch information
chanyipiaomiao committed Aug 30, 2018
1 parent c3661f7 commit 3680d59
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 3 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# devops-api

Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,提供开发/运维常用操作的HTTP API接口: IP地址查询、工作日节假日判断、微信报警、钉钉报警、2步验证、密码存储、发送邮件、生成随机密码等功能
Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,提供开发/运维常用操作的HTTP API接口: 手机归属地查询、IP地址查询、工作日节假日判断、微信报警、钉钉报警、2步验证、密码存储、发送邮件、生成随机密码等功能

# 主要功能

- 手机归属地查询
- IP地址查询
- 工作日节假日判断
- 微信报警
Expand All @@ -21,6 +22,7 @@ Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,
- [安装使用](#安装使用)
- [依赖](#依赖)
- [功能列表](#功能列表)
- [手机归属地查询](#手机归属地查询)
- [IP地址查询](#ip地址查询)
- [工作日节假日判断](#工作日节假日判断)
- [设置节假日和工作日](#设置节假日和工作日)
Expand Down Expand Up @@ -81,6 +83,7 @@ Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,
- security.conf 安全相关的配置
- twostep.conf 2步验证相关
- weixin.conf 微信报警相关
- phone.conf 手机归属地查询配置

主配置文件 app.conf 通过include的方式加载其他的配置文件

Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions common/phone.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion conf/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ include "weixin.conf"
include "twostep.conf"
include "email.conf"
include "authpassword.conf"
include "security.conf"
include "security.conf"
include "phone.conf"
2 changes: 2 additions & 0 deletions conf/phone.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[phone]
dbPath = data/phone.dat
5 changes: 5 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,8 @@ type HolidayController struct {
type QueryIPController struct {
BaseController
}

// PhoneController 手机归属地查询
type PhoneController struct {
BaseController
}
20 changes: 20 additions & 0 deletions controllers/phone.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
),
Expand Down
Empty file modified script/gen_ip_region.sh
100644 → 100755
Empty file.
9 changes: 9 additions & 0 deletions script/get_phone_dat.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3680d59

Please sign in to comment.