获取电话归属地

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;
        }

 

你可能感兴趣的:(获取)