About this development kit
- This composer package mainly integrates some common development on WeChat, such as: secondary development of WeChat public account, WeChat payment, WeChat mobile web sharing, etc. Due to the inclusion of many functions, only the sharing of WeChat mobile web is uploaded. We will continue to follow up and update in the future.
- When you use this package, you have already configured and successfully configured the relevant official account information and will use composer
- install command composer require doing/wechat version number
- This package can only be integrated in ThinkPHP5: the reason is that its caching mechanism and exception handling mechanism are used. If you want to use it in other frameworks, you only need to replace the caching mechanism and exception mechanism (mainly programming Thought)
WeChat mobile web sharing (send to friends, circle of friends, etc.)
configure public account
- If you only need jssdk-related requirements, you only need appid and appsecret. As for how to get to the public account, you can use Baidu a lot.
- Configure a secure domain name
Service-Terminal
- The task is to obtain parameters such as appid and signature required for sharing
- Approximate process: through appid and appsecret->get AccessToken->get the ticket of jsapi->resign and return
- Please configure appid and appsecret in doing/wechat/config/WechatConfig.php before development
written before the interface
- Copy and paste the functions getRandChar() and postCurl() from doing/wechat/functions.php into application/common.php. After the copy is complete functions.php has completed its mission, you can choose to delete this file
Write an interface to get parameters (return in json)
public function info() { //The parameter fullurl passed by the client must be escaped by the encodeURIComponent of js $fullurl = urldecode(input('fullurl')); #Get permission information (you can directly call the method in the package): //The top of the class must ensure use wechat\auth\WxAuth; try { return json(WxAuth::instance()->getInfo($fullurl)); } catch (\Exception $e) { $exp['msg'] = $e->getMessage(); $exp['code'] = $e->getCode(); #TP5's return method exceptions are handled in the client's error return function return json($exp, $e->getCode()); }//-try }//pf
The data format of the returned success is as follows (to meet the data structure required by the client when calling WeChat jssdk)
{ appId: 'you appId',//The unique identifier of the official account timestamp:'12345678',// Generate the timestamp of the signature nonceStr: '12345678',// Generate a random string of signature signature: '12345678'// signature }
The data format of the returned exception is as follows (http status code 600 is reflected in the header)
{ "msg": "WeChat error code [40164] invalid ip 118.112.58.58, not in whitelist hint: [wgirhA04751512]", "code": 600 }
Client (mobile web)
1. Make sure jquery and wechat jsdk are loaded
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
2. Define the corresponding url
//BASE_URL_WX is your server address, such as http://www.test.com to prevent future changes: this domain name should be consistent with the JS security domain name configured by the WeChat public
account //shareUrl is the url of the page you want to share if there is a later The parameter is ?id=xx splicing
var shareUrl = BASE_URL_WX + "/listen/web/share/app";
//fullurl is the url to sign, that is, the client wants to use the interface
//fullurl must be written like this, And many times shareUrl and fullurl are equal, but some special cases are not equal to report an error: this problem was debugged for a day and came to the conclusion
var fullurl = encodeURIComponent(location.href.split('#')[0]);
through ajax request The logic of obtaining parameters such as signature and writing WeChat at the same time
Description: The following imgUrl must be directly accessible and the full path on the server
$.ajax({ type: 'GET', url: SIGN_URL, data:{fullurl:fullurl}, dataType: 'json', success: function (res) { wx.config({ debug: false, // Turn on the debug mode, the return values of all the apis called will be alerted on the client side, if you want to view the incoming parameters, you can open it on the pc side, and the parameter information will be printed out through the log, only on the pc side will print. appId: res.appId, // Required, the unique ID of the official account timestamp: res.timestamp, // required, the timestamp of the generated signature nonceStr: res.noncestr, // required, generate a random string for signature signature: res.signature,// required, signature jsApiList: [ 'onMenuShareAppMessage',//Share with friends 'onMenuShareTimeline'//Circle of friends ] // Required, a list of JS interfaces to be used }); wx.ready(function () { //Send to friend wx.onMenuShareAppMessage({ title: title, // share title desc: desc, // share description link: shareUrl, // share link imgUrl:shareImg, // share icon type: 'link', // share type, music, video or link, if not filled, the default is link dataUrl: '', // If the type is music or video, a data link should be provided, the default is empty success: function () { }, cancel: function () {} }), //Share to the circle of friends wx.onMenuShareTimeline({ title: title, // share title link: shareUrl, // share link imgUrl:shareImg, // share icon }) }); }, error: function (res) { alert("WeChat server exception"); } });
Code download: WeChat development, a composer package that integrates public account, WeChat payment and other functions