获取电话归属地

cshtml:

            <label>
                <span>呼入电话</span>
                <input type="text" name="Phone" value="<%=order.Phone%>" readonly="readonly" />
            </label>
            <label>
                <span>电话归属地</span>
                <span id="Attribution" style="text-align: left"></span>
            </label>

attribution.aspx?phone= xxxx:

<%@ Page Language="C#" ContentType="text/plain" %>
<%string attribution = Items["Attribution"].ToString();%>
  <%if(attribution.Contains(" ")) {
      string[] area = attribution.Split(' ');%>
[{
Province: "<%=area[0] %>",
City: "<%=area[1].Substring(0, area[1].Length-1) %>"
},]
 <%} else {%>
[{
Province: " ",
City: "<%=attribution %>"
},]
<% } %>

 

Jquery:

 //获取号码归属地,包括手机号码和固定电话
        function setAttribution(phoneNumber) {
            if (phoneNumber) {
                $.get('attribution.aspx?phone=' + phoneNumber, function (attribution) {
                    attribution = eval(attribution);
                    $('#Attribution').html(attribution[0].Province + " " + attribution[0].City);
                })
            }
        }

        var attribution = '<%=order.attribution %>', phone = $('input[name="Phone"]');

        if (attribution == "") {//若为新用户,根据号码获取归属地
            setAttribution(phone.val());
        }
        //若号码改变,重新获取号码归属地
        phone.change(function () {
            setAttribution(phone.val());

        });

 

C#:

        /// <summary>
        /// 获取电话号码归属地,包括手机号码和固话号码
        /// </summary>
        /// <returns></returns>
        [Request("GET", "attribution.aspx")]
        public View Attribution(string phone) {
            HtmlView view = new HtmlView();
            view.Items["Attribution"] = User.GetAttribution(phone);
            return view;
        }


public string GetAttribution(string phone) {
            Attribution attribution = new Attribution();
            if(phone.StartsWith("0")) {
                phone = phone.Substring(0, phone.Length - 8);
                attribution = DatabaseFactory.Create("Callcenter").CreateQuery("SELECT DISTINCT MobileArea FROM Attribution WHERE AreaCode = @AreaCode", phone).ExecuteEntity<Attribution>();
            } else {
                phone = phone.Substring(0, 7);//取手机号码前面七位进行查询
                attribution = DatabaseFactory.Create("Callcenter").CreateQuery("SELECT MobileArea FROM Attribution WHERE MobileNumber = @MobileNumber", phone).ExecuteEntity<Attribution>();
            }
            string mobileArea = "";
            if(attribution != null) {
                mobileArea = attribution.MobileArea;
            }
            return mobileArea;
        }

 

转载于:https://www.cnblogs.com/sxhNicole/p/4583274.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值