Introduction to WeChat development (jssdk development)

零下一度
Release: 2017-05-27 13:56:28
Original
2188 people have browsed it

A project needs to obtain the current position in WeChat, so I started my WeChat development journey...

WeChat JSSDK documentation
mp. weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html

JSSDK usage steps
Step 1: Bind domain name
The binding domain name requires a first-level domain name. But When using it, web pages under the second-level domain name can still use this interface.

Step 2: Introduce JS files
http://res.wx.qq.com/open /js/jweixin-1.0.0.js
If your page enables https, be sure to introduce res.wx.qq.com/open/js/jweixin-1.0.0.js

Step 3: Inject the permission verification configuration through the config interface
Configuring this will be more troublesome, let’s talk about it at the end.

wx.config({
   debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
   appId: '', // 必填,公众号的唯一标识
   timestamp: , // 必填,生成签名的时间戳
   nonceStr: '', // 必填,生成签名的随机串
   signature: '',// 必填,签名,见附录1
   jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
Copy after login

Step 4: Process successful verification through the ready interface

wx.ready(function(){
  // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
Copy after login

Step 5: Handle failed verification through the error interface

wx.error(function(res){
  // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
Copy after login

For example: Get the geographical location interface

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; // 位置精度     }  });
});
Copy after login

For details, see The official document is clearer.

Config instructions for step three:

var wxConfigJson = {   debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。   appId: 'wx821bb7356e99a3a7', // 必填,公众号的唯一标识   timestamp: 1446184841, // 必填,生成签名的时间戳   nonceStr: '80262bec-7cd2-4b03-b366-97fab6e91cdd', // 必填,生成签名的随机串   signature: 'fd017d631d63cef32d5f25ea276911bcb0a29782', // 必填,签名,见附录1   jsApiList: ['getLocation'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2};@Html.Raw(new WeChatJS_SDK_Config().GetConfig()) 生成上面的wxConfigJson
Copy after login

The following is the WeChatJS_SDK_Config class I wrote, it’s funny.

 1 public class WeChatJS_SDK_Config
 2 {
 3     private string _appId;
 4     private int _timestamp ;// 必填,生成签名的时间戳
 5     private string _nonceStr ;// 必填,生成签名的随机串
 6     private string _url;
 7     private string _sign;
 8 
 9     public WeChatJS_SDK_Config()
10     {
11         _appId = AccountConfig._appID;
12         _timestamp = Core.GetTimestamp();
13         _nonceStr = Guid.NewGuid().ToString();
14         _url = HttpContext.Current.Request.Url.ToString();
15         _sign =
16             String.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", Jsapi_Ticket.GetJsapi_Ticket(),
17                 _nonceStr, _timestamp, _url).Md5Entry("sha1").ToLower();
18     }
19 
20     /* Url方法的区别
21      *@Html.Raw("var originalString='"+HttpContext.Current.Request.Url.OriginalString+"';")
22     @Html.Raw("var rawUrl='"+HttpContext.Current.Request.RawUrl+"';")
23     @Html.Raw("var url='"+HttpContext.Current.Request.Url+"';")
24    
25     var originalString = 'http://xb.hnjdkj.cn:80/index/home';
26     var rawUrl = '/index/home';
27     var url = 'http://xb.hnjdkj.cn/index/home';*@
28      */
29     public string GetConfig()
30     {
31         var res = string.Format(
32             "var wxConfigJson = {{debug: {0}, appId: '{1}', timestamp: {2},nonceStr: '{3}',signature: '{4}',jsApiList: ['getLocation'] }};", Kits.AppSettings("WeChatDebug"), _appId, _timestamp, _nonceStr, _sign);
33         return res;
34     }
35 
36 }
Copy after login

【 Related recommendations】

1. WeChat public account platform source code download

2. An example tutorial of the life cycle function of WeChat development

3. Introduction to several methods of passing and obtaining values ​​in WeChat development

4. Detailed explanation of the summary of errors in the development of WeChat applet payment function

The above is the detailed content of Introduction to WeChat development (jssdk development). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template