Share call jssdk instance, call jssdk instance
The web page is opened in, shared, detailed process
1. Problem Description
After half a day, we finally got it done. Let's talk about the basic requirements. It's very simple. The webpage is open and shared.
The webpage is in html format. The link is always displayed during sharing. After reading the code, jssdk is not called and a third-party platform sharing interface is called.
2. Problem Analysis
According to the official demo, we can see that the core is to get four parameters and configure wx. config.
The four parameters are appId, timestamp, nonceStr, and signature.
Not to mention appId, the last three are obtained based on the interface. If you are interested, you can refer to the official website, but you can leave it alone.
3. parsing ideas
Next, let's talk about my ideas. In js, call the interface deployed in jssdk through ajax and return the above parameters.
I tested PHP and wrote a myapi. php interface, which is very simple:
<? Phprequire_once "jssdk. php "; $ jssdk = new JSSDK (" Your appId "," Your appSecret "); $ signPackage = $ jssdk-> GetSignPackage (); $ data = array ('appid '=> $ signPackage ["appId"], 'timestamp' => $ signPackage ["timestamp"], 'noncestr' => $ signPackage ["nonceStr"], 'signature' => $ signPackage ["signature"]); echo json_encode ($ data );
Front-end page
<Script src = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script> <script> $ (function () {$. ajax ({type: "GET", url :". /myapi. php ", data:" ", success: function (data) {var result = JSON. parse (data); if (result! = Null) {wx. config ({debug: false, appId: result. appId, timestamp: result. timestamp, nonceStr: result. nonceStr, signature: result. signature, jsApiList: ['onmenusharetimeline', 'onmenushareappmessage']}) ;}, error: function () {}}); window. required data = {title: 'title', desc: 'description', link: window. location. href, imgUrl: 'https: // gqrcode.alicdn.com/img? Type = cs & shop_id = 295323332 & seller_id = 1094878600 & w = 140 & h = 140 & el = q & v = 1 ', success: function () {alert ('success') ;}, cancel: function () {alert ('cancel') ;}}; wx. ready (function () {wx. onMenuShareAppMessage (response data); wx. onMenuShareTimeline (structured data) ;}); </script>
4. knowledge points
1. Share the use of jssdk
2. html calls ajax
3. Solve the connection issue during sharing.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.