微信开发之 获取用户地址
时间:2016-01-25 12:49:18
收藏:0
阅读:301
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Security; using System.Web.Script.Serialization; using sohovan.com.wxapi; namespace WXKF { public class JsApi { JavaScriptSerializer Jss = new JavaScriptSerializer(); public JsApi() { } #region 验证JsApi权限配置 /// <summary> /// 获取JsApi权限配置的数组/四个参数 /// </summary> /// <param name="Appid">应用id</param> /// <param name="Appsecret">密钥</param> /// <returns>json格式的四个参数</returns> public string GetJsApiInfo(string Appid, string Appsecret) { string timestamp = CommonMethod.CommonMethod.ConvertDateTimeInt(DateTime.Now).ToString();//生成签名的时间戳 string nonceStr = CommonMethod.CommonMethod.GetRandCode(16);//生成签名的随机串 string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.ToString();//当前的地址 string jsapi_ticket = ""; //ticket 缓存7200秒 if (System.Web.HttpContext.Current.Session["jsapi_ticket"] == null) { jsapi_ticket = CommonMethod.CommonMethod.WebRequestPostOrGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + BasicApi.GetTokenSession(Appid, Appsecret) + "&type=jsapi", ""); System.Web.HttpContext.Current.Session["jsapi_ticket"] = jsapi_ticket; System.Web.HttpContext.Current.Session.Timeout = 7200; } else { jsapi_ticket = System.Web.HttpContext.Current.Session["jsapi_ticket"].ToString(); } Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(jsapi_ticket); jsapi_ticket = respDic["ticket"].ToString();//获取ticket string[] ArrayList = { "jsapi_ticket=" + jsapi_ticket, "timestamp=" + timestamp, "noncestr=" + nonceStr, "url=" + url }; Array.Sort(ArrayList); string signature = string.Join("&", ArrayList); signature = FormsAuthentication.HashPasswordForStoringInConfigFile(signature, "SHA1").ToLower(); return "{\"appId\":\"" + Appid + "\", \"timestamp\":" + timestamp + ",\"nonceStr\":\"" + nonceStr + "\",\"signature\":\"" + signature + "\"}"; } #endregion } }
public string appId; public string timestamp; public string nonceStr; public string signature; protected void Page_Load(object sender, EventArgs e) { JsApi jsapi = new JsApi(); string data = jsapi.GetJsApiInfo("***", "***"); JObject jo = (JObject)JsonConvert.DeserializeObject(data); appId = jo["appId"].ToString(); timestamp = jo["timestamp"].ToString(); nonceStr = jo["nonceStr"].ToString(); signature = jo["signature"].ToString(); //Response.Write("<script type=‘text/javascript‘>btn();</script>"); // ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>btn();</script>"); }
<title>请稍等,定位中。。。</title> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script src="Scripts/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=IWf1r4wh9pdpXUPOwGvkLxYW"></script> <script src="Scripts/cvi_busy_lib.js"></script> <script> function wxconfig() { wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: ‘<% =appId%>‘, // 必填,公众号的唯一标识 timestamp: ‘<% =timestamp%>‘, // 必填,生成签名的时间戳 nonceStr: ‘<% =nonceStr%>‘, // 必填,生成签名的随机串 signature: ‘<% =signature%>‘,// 必填,签名,见附录1 jsApiList: [ ‘onMenuShareTimeline‘, ‘onMenuShareAppMessage‘, ‘onMenuShareQQ‘, ‘onMenuShareWeibo‘, ‘getLocation‘, ‘openLocation‘ ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); } var getParam = function (name) { var search = document.location.search; var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g"); var matcher = pattern.exec(search); var items = null; if (null != matcher) { try { items = decodeURIComponent(decodeURIComponent(matcher[1])); } catch (e) { try { items = decodeURIComponent(matcher[1]); } catch (e) { items = matcher[1]; } } } return items; }; wx.ready(function () { wx.getLocation({ type: ‘wgs84‘, // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入‘gcj02‘ success: function (res) { var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。 var speed = res.speed; // 速度,以米/每秒计 var accuracy = res.accuracy; // 位置精度 alert("维度:" + latitude + " 经度:" + longitude); }, cancel: function (res) { alert(res); alert(‘用户拒绝授权获取地理位置‘); } }); }); wxconfig(); </script>
评论(0)