在制作手机网站应用时,用户在可以通过微信浏览器访问。相比于普通浏览器的访问需要对接利用其自身的一些网页接口。如支付接口就是其中的一种,本文介绍一下自定义微信浏览器的分享内容如何处理。
更多网页应用可以查看:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
1、在公众号管理平台,“设置”->“公众号设置”->“功能设置”下的“JS 接口安全域名”和“网页授权域名”中填写自已的域名。
2、在公众号管理平台,“开发”->“基本配置”下设置“IP 白名单”。
3、应用,也可以到上面的地址中去下载 demo。下面是 TP 的应用。
1)在项目模块下的 Service 文件夹中新建 WxjssdkService.class.php 用于 js 接口调用。其完整代码为:
需要注意的是这里获取的 accessToken 因为有请求次数的限制,所以一定要在服务器缓存起来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| <?php namespace Home\Service;
class WxjssdkService {
function __construct() { $jsConfig = array( 'APPID' => D('Config')->get('wxpay_appid'), 'APPSECRET' => '******', ); $this->jsConfig = $jsConfig; }
public function getSignPackage($url = '') { $jsConfig = $this->jsConfig; $jsapiTicket = $this->getJsApiTicket(); $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $url ? $url : "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $jsConfig['APPID'], "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; }
private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; }
private function getJsApiTicket() { $data = S('wx_jsapi_ticket'); if (!$data) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { S('wx_jsapi_ticket', $ticket, 7000); } } else { $ticket = $data; } return $ticket; }
private function getAccessToken() { $jsConfig = $this->jsConfig; $data = S('wx_access_token'); if (!$data) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $jsConfig['APPID'] . "&secret=" . $jsConfig['APPSECRET']; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { S('wx_access_token', $access_token, 7000); } } else { $access_token = $data; } return $access_token; }
private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
|
2)在控制器中即可使用:
1 2 3 4
| $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $sharelink = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $signPackage = D('Wxjssdk', 'Service')->GetSignPackage($sharelink); $this->signPackage = $signPackage;
|
3)在模板中可以输出,仅以分享给朋友和微信朋友圈为例,此为公共文件,当需要改变要分享的内容时可在当前页中输出下面的 js 变量覆盖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <script> var wx_share_title = ("undefined" != typeof wx_share_title) ? wx_share_title : '{$config.index_title}'; var wx_share_link = ("undefined" != typeof wx_share_link) ? wx_share_link : 'http://{$_SERVER[HTTP_HOST]}{$_SERVER[REQUEST_URI]}'; var wx_share_imgUrl = ("undefined" != typeof wx_share_imgUrl) ? wx_share_imgUrl : "http://{:$_SERVER['SERVER_NAME']}__ROOT__/Uploads/mlogo/thumb/default.png"; var wx_share_desc = ("undefined" != typeof wx_share_desc) ? wx_share_desc : '{$config.index_keywords}'; </script> <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script> wx.config({ debug: false, / / 开启调试模式 appId: '{$signPackage.appId}', timestamp: '{$signPackage.timestamp}', nonceStr: '{$signPackage.nonceStr}', signature: '{$signPackage.signature}', jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] });
wx.ready(function(res) { wx.onMenuShareTimeline({ title: wx_share_title , link: wx_share_link , imgUrl: wx_share_imgUrl , success: function() { }, cancel: function() { } }); wx.onMenuShareAppMessage({ title: wx_share_title , desc: wx_share_desc , link: wx_share_link , imgUrl: wx_share_imgUrl , type: '', dataUrl: '', success: function() { }, cancel: function() { } }); }); < /script>
|