<%@ WebHandler Language="C#" Class="signIn" %> using System; using System.Web; using eMIS.Data; using System.Net; using System.Data; /* * Create by cjx,2018-3-22 * */ public class signIn : IHttpHandler { public void ProcessRequest(HttpContext context) { string loginid = context.Request["u"]; string jd = context.Request["jd"]; string wd = context.Request["wd"]; string bz = context.Request["bz"]; try { BaiduAddress address = Geocoding("gcj02ll", jd, wd); double d1, d2; if (!double.TryParse(jd, out d1) || !double.TryParse(wd, out d2)) { throw new Exception(string.Format("获取的坐标不正确,请再试一次({0},{1})", jd, wd)); } //可以透过代理服务器 string userIP = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (userIP == null || userIP == "") { //没有代理服务器,如果有代理服务器获取的是代理服务器的IP userIP = context.Request.ServerVariables["REMOTE_ADDR"]; } //1000米之内 if (gpsDistance(Convert.ToDouble(jd), Convert.ToDouble(wd), 118.785220, 31.891915) > 500 //水阁路 腾讯 31.891915,118.785220 百度: 118.791883, 31.898178 && gpsDistance(Convert.ToDouble(wd), Convert.ToDouble(jd), 118.694916, 32.162725) > 600) //星火路 腾讯32.162725,118.694916 百度118.699602, 32.167885 { throw new Exception("不在签到范围之内!"); } ReportCoord(loginid, jd, wd, address.Address, address.Province, address.City, address.CityCode, bz, userIP); context.Response.ContentType = "text/plain"; context.Response.Write("ok"); } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } } private double gpsDistance(double lng1,double lat1, double lng2, double lat2) { double distance = 0; double lonRes = 102900, latRes = 110000; distance = Math.Sqrt(Math.Abs(lat1 - lat2) * latRes * Math.Abs(lat1 - lat2) * latRes + Math.Abs(lng1 - lng2) * lonRes * Math.Abs(lng1 - lng2) * lonRes); //System.out.println( "两点间距离:" + distance ); return distance; } public string ReportCoord(string userLogin, string longitude, string latitude, string address, string province, string city, string cityCode,string bz,string ip) { string sql = ""; eDbManager manager = new eDbManager(eMIS.Data.WebConfigManager.DefaultDb); eDbOperator dbo = manager.CreateDbOperator(); try { int affectNum = dbo.ExecuteProcedure("p_wx_signIn", userLogin, longitude, latitude, address, province, city, cityCode, bz,ip,2); return affectNum > 0 ? "ok" : "err"; } catch (Exception ex) { // dbo.RollbackTrans(); //return ex.Message; throw ex; } } /// /// 通过经纬度获取地址、省市等 /// /// 包括:bd09ll(百度经纬度坐标)、gcj02ll(国测局经纬度坐标)、wgs84ll( GPS经纬度) /// 经度 /// 纬度 private BaiduAddress Geocoding(string coordtype, string lng, string lat) { string url = "http://api.map.baidu.com/geocoder/v2/?ak=twibmGXUQR0KyWjCXQ7ElGAM&callback=renderReverse&location={0},{1}&output=xml&pois=0&coordtype={2}"; System.Net.HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format(url, lat, lng,coordtype)); request.Timeout = 5000; request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()); // string xmlstr = sr.ReadToEnd(); System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(sr); BaiduAddress addr = new BaiduAddress(); while (xmlReader.Read()) { if (xmlReader.IsStartElement()) { switch (xmlReader.Name.ToLower()) { case "formatted_address": addr.Address = xmlReader.ReadString(); break; case "city": addr.City = xmlReader.ReadString(); break; case "citycode": addr.CityCode = xmlReader.ReadString(); break; case "province": addr.Province = xmlReader.ReadString(); break; case "lat": addr.Location.Latitude = xmlReader.ReadString(); break; case "lng": addr.Location.Longitude = xmlReader.ReadString(); break; } } } return addr; } class BaiduAddress { private LocationClass _location = new LocationClass(); public BaiduAddress() { _location = new LocationClass(); } private string _address = string.Empty; private string _province = string.Empty; private string _city = string.Empty; private string _cityCode = string.Empty; public string Address { set { _address = value; } get { return _address; } } public string Province { set { _province = value; } get { return _province; } } public string City { set { _city = value; } get { return _city; } } public string CityCode { set { _cityCode = value; } get { return _cityCode; } } public LocationClass Location { get { return _location; } } public class LocationClass { private string _lng = string.Empty; private string _lat = string.Empty; public string Longitude { set { _lng = value; } get { return _lng; } } public string Latitude { set { _lat = value; } get { return _lat; } } } } private class QQAddress { private int _status; private string _message = ""; public int status { set { _status = value; } get { return _status; } } public string message { set { _message = value; } get { return _message; } } } /* private QQAddress QQGeocoding(string coordtype, string lng, string lat) { string url = "http://apis.map.qq.com/ws/geocoder/v1/?key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&location={0},{1}&output=json&coordtype={2}"; System.Net.HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format(url, lat, lng,coordtype)); request.Timeout = 5000; request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()); // string xmlstr = sr.ReadToEnd(); System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(sr); QQAddress addr = new QQAddress(); while (xmlReader.Read()) { if (xmlReader.IsStartElement()) { switch (xmlReader.Name.ToLower()) { case "formatted_address": addr.Address = xmlReader.ReadString(); break; case "city": addr.City = xmlReader.ReadString(); break; case "citycode": addr.CityCode = xmlReader.ReadString(); break; case "province": addr.Province = xmlReader.ReadString(); break; case "lat": addr.Location.Latitude = xmlReader.ReadString(); break; case "lng": addr.Location.Longitude = xmlReader.ReadString(); break; } } } return addr; }*/ }