由于项目要根据地理位置知道周边有哪些商店;但Web网址又是到企业微信第三方应用开发的,所以这里只能使用企业微信APP的设置来获取经纬度
文档:链接地址
为了安全性这里去取access_token、jsapi_ticket、生成签名全部在后台实现
public class EnterpriseWeChatToken
{
private string Corpid = ConfigHelper.GetConfigStr("Corpid");
private string Secret = ConfigHelper.GetConfigStr("Secret");
///
/// 请求企业微信Token
///
///
public string ResultAccess_token()
{
try
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?";
StringBuilder str = new StringBuilder();
str.Append(url);
str.Append("corpid=" + Corpid + "&corpsecret=" + Secret + "");
string Tokes = HttpUtils.DoGet(str.ToString(), null).ToString();
dynamic jsonToken = JToken.Parse(Tokes) as dynamic;
if (jsonToken["errcode"] == 0)
{
return jsonToken["access_token"];
}
else
{
return "";
}
}
catch (Exception ex)
{
Log.CreateLogManager().Error("请求token异常:" + ex);
return "";
}
}
}
///
/// 获取企业的jsapi_ticket
///
/// 调用接口凭证:access_token
///
private string GetResultJsapi_ticket(string access_token)
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?";
StringBuilder str = new StringBuilder();
str.Append(url);
str.Append("access_token=" + access_token);
string Tokes = HttpUtils.DoGet(str.ToString(), null).ToString();
dynamic jsonToken = JToken.Parse(Tokes) as dynamic;
if (jsonToken["errcode"] == 0)
{
return jsonToken["ticket"];
}
else
{
return "";
}
}
///
/// 获取时间戳
///
///
public string GetTimestam()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToUInt64(ts.TotalSeconds).ToString();
}
///
/// 获取随机字符串
///
/// 多少位的随机字符串
///
public string GetNoncestr(int codeCount)
{
int rep = 0;
string str = string.Empty;
long num2 = DateTime.Now.Ticks + rep;
rep++;
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
for (int i = 0; i < codeCount; i++)
{
char ch;
int num = random.Next();
if ((num % 2) == 0)
{
ch = (char)(0x30 + ((ushort)(num % 10)));
}
else
{
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
}
str = str + ch.ToString();
}
return str;
}
///
/// 生成签名
///
///
/// 随机字符串
/// 时间戳
/// 请求地址
///
public string GetSignature(string Jsapi_ticket, string Noncestr, string Timestam, string Url)
{
string str = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", Jsapi_ticket, Noncestr, Timestam, Url);
return SHA1HASH(str);
}
///
/// 加密签名
///
///
///
public string SHA1HASH(string Value)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(Value);
byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
str_sha1_out = str_sha1_out.Replace("-", "");
return str_sha1_out;
}
///
/// 生成JsSDK信息
///
///
public object GetJsSDK()
{
object json = new { };
try
{
string Jsapi_ticket = GetResultJsapi_ticket(new EnterpriseWeChatToken().ResultAccess_token());
if (!string.IsNullOrEmpty(Jsapi_ticket))
{
string Url = "http://23005732cx.51mypc.cn/test.html"; //必须自己前网页地址
JsSDK sdk = new JsSDK();
sdk.Corpid = Corpid;
sdk.Agentid = AgentId;
sdk.Timestamp = GetTimestam();
sdk.NonceStr = GetNoncestr(16);
sdk.Signature = GetSignature(Jsapi_ticket, sdk.NonceStr, sdk.Timestamp, Url);
json = new
{
code = 1,
data = sdk,
msg = "请求成功"
};
}
else
{
json = new
{
code = 0,
data = "",
msg = "请求失败,Jsapi_ticket获取失败"
};
}
}
catch (Exception ex)
{
json = new
{
code = 0,
data = "",
msg = "请求失败,出现异常"
};
}
return json;
}
验证域名归属的方法在企业微信的管理后台“我的应用”里,进入应用,设置应用可信域名。
$.ajax({
url: "/api/EnterpriseWeChatJsSDK/GetResultJsSDK",
type:"post",
success: function (result)
{
if (result.code == 1)
{
alert("Corpid:" + result.data.Corpid + ",Timestamp:" + result.data.Timestamp + ",NonceStr:" + result.data.NonceStr + ",Signature:" + result.data.Signature + "");
sdk(result.data.Corpid, result.data.Timestamp, result.data.NonceStr, result.data.Signature);
}
}
});
function sdk(Corpid, timestamp, nonceStr, signature) {
//1.注入权限验证配置
wx.config({
beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: Corpid, // 必填,企业微信的corpID
timestamp: timestamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
jsApiList: ['getLocation'] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
});
//2.调用地理位置接口
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
alert("latitude:" + res.latitude);
alert("longitude:" + res.longitude);
alert("speed:" + res.speed);
alert("accuracy:" + res.accuracy);
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
var speed = res.speed; // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
}
});
}