npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

phone-service

v4.0.6

Published

手机号查询运营商,查询归属地

Downloads

101

Readme

通过手机号查询运营商以及号码归属地

Build Status via Travis CI Coverage Status NPM version

在很多行业很多项目,比如电商,比如金融,比如O2O等,在用户注册这一块会经常用到手机号。如何判断一个手机号是否存在?在特殊产品需求条件下,我们需要区分用户的运营商(移动、联通、电信),甚至区分用户省份,乃至城市,来方便产品,运营同学对用户进行地区化,个性化服务。为了解决这样的业务、产品需求,就动手写了这个模块。

安装:npm install phone-service

API

插件编写

工具函数

util

API使用例子

[query]: 查询手机号详细信息, 异步函数

可选参数option说明

/**
*option = > {
	parallel:2,//插件通道并发请求数,默认是2,当为0时使用最大(所有可用插件)并发数同时发起查询请求。
	timeout:null,//响应超时时间
	model:0,//获取手机号码信息模式,目前支持3种:0->只获取手机号运营商,1->获取手机号运营商以及省份,2->获取手机号运营商、省份以及城市(地级市)
	plugins:[]//指定使用哪些插件通道(当某些通道出问题时,用户可以指定只用这些通道)
}
*/

查询手机号运营商

var phoneService = require('phone-service');
var option = {};
phoneService.query(15900000000,option,function(err,data){
	//data=>{supplier:"中国移动",phone:15900000000,abbreviation:'China_Mobile'}
});

// promise
phoneService.query(15900000000,option)
	.then(function (data) {
		//data=>{supplier:"中国移动",phone:15900000000,abbreviation:'China_Mobile'}
	})

查询手机号运营商以及省份

var phoneService = require('phone-service');
var option = {model:1};
phoneService.query(15900000000,option,function(err,data){
	//data=>{supplier:"中国移动",province:'广东',phone:15900000000,abbreviation:'China_Mobile'}
});

查询手机号运营商、省份以及城市(地级市)

var phoneService = require('phone-service');
var option = {model:2};
phoneService.query(15900000000,option,function(err,data){
	//data=>{supplier:"中国移动",province:'广东',city:'中山',phone:15900000000,abbreviation:'China_Mobile'}
});
var phoneService = require('phone-service');
phoneService.isPhone(15900000000) === true
var phoneService = require('phone-service');
phoneService.isChinaMobile(15900000000) === true
var phoneService = require('phone-service');
phoneService.isChinaTelecom(15900000000) === false;
var phoneService = require('phone-service');
phoneService.isChinaUnicom(15900000000) === false;

插件编写(高级功能)

插件扩展开发

如果模块自带的插件不满足或者被插件对应的平台屏蔽了,使用者可以自己开发插件来完成工作。

//插件开发 demo
var phoneService = require('phone-service');
var util = phoneService.util;
var plugin = phoneService.plugin;//插件对象
var pluginMock = {
	name:'navy',//插件名字
	parse:function(phone,cb){
		//数据解析,返回json.当所编写的插件:
		//1.只返回手机号服务商(即插件的model定义为0)时,返回的对象中必须包含key:util.getSupplierKey()
		//2.返回手机号服务商和归属省份(即插件的model定义为1)时,返回的对象中必须包含key:util.getSupplierKey(),util.getProvinceKey()
		//3.返回手机号服务商和归属省份以及归属城市(即插件的model定义为2)时,返回的对象中必须包含key:util.getSupplierKey(),util.getProvinceKey(),util.getCityKey()
		var result = {};
		result[util.getSupplierKey()] = '移动';
		result[util.getProvinceKey()] = '广东';
		result[util.getCityKey()] = '广州';
		cb(null,result);
	}
	model:2,//model可取值:0,1,2,说明请看上面parse函数
	url:function(phone){
		return 'http://xxxx.com?phone='+phone;//返回请求的url
	},
	option:{
		//node request module request method param.
		headers:{"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/41.0.2272.76 Chrome/41.0.2272.76 Safari/537.36"},
		encoding:null
	}
};
plugin.add(pluginMock);//注册插件
plugin.check('navy',function(err,data){
	if(!err){
		//plugin is ok;
	}
})
//使用刚才注册的插件
phoneService.query(15900000000,{plugins:['navy']},function(err,data){
	//todo
})

当模块自带的插件或者使用者自己开发的插件出问题时(比如被插件对应的平台屏蔽),这时候在调用api前可以卸载有问题的模块

plugin.delete('navy');//卸载自己写的插件
plugin.delete('360');//卸载模块自带的插件
//调用query方式时将不再使用360插件了
phoneService.query(15900000000,function(err,data){
	//todo
})

工具对象

var phoneService = require('phone-service');
phoneService.util.getSupplierKey();//获取服务商返回的key
phoneService.util.getProvinceKey();//获取省份返回的key
phoneService.util.getCityKey();//获取城市(地级市)返回的key

test

//test
npm test
//code coverage
//npm run cov

code coverage

=============================== Coverage summary ===============================
Statements   : 87.41% ( 354/405 )
Branches     : 80.83% ( 156/193 )
Functions    : 95.95% ( 71/74 )
Lines        : 87.41% ( 354/405 )
================================================================================

发布logs

  • 4.0.0 query api 支持promise方式调用
  • 3.0.1 添加parallel参数为0时开启最大(当前支持插件)并发数同时发起查询请求
  • 3.0.0 修复重大的bug,plugin下try catch 没有return。建议立马升级至3.0.0。