javascript获取wx.config内部字段解决分享
背景
phpjson格式化输出
在分享开发的时候我们通常的流程是
<?php
require_once "jssdk.php";
$jssdk = new JSSDK("yourAppID", "yourAppSecret");
$signPackage = $jssdk->GetSignPackage();
>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分享</title>
</head>
<body>
</body>
<script src="res.wx.qq/open/js/jweixin-1.0.0.js"></script>
<script>
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: ['onMenuShareTimeline'
'onMenuShareAppMessage'
]
});
title: '', // 分享标题
link: '', // 分享链接
imgUrl: '', // 分享图标
success: function() {
/
/ ⽤户确认分享后执⾏的回调函数
},
cancel: function() {
// ⽤户取消分享后执⾏的回调函数
}
});
title: '', // 分享标题
desc: '', // 分享描述
link: '', // 分享链接
imgUrl: '', // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function() {
// ⽤户确认分享后执⾏的回调函数
},
cancel: function() {
// ⽤户取消分享后执⾏的回调函数
}
});
});
</script>
</html>
解决⽅案
将获取配置参数的PHP写作为接⼝,在js⾥使⽤ajax调⽤,获取参数并转换为对象,再通过回调函数将ajax获取的参数塞到wx.config中
代码结构及功能
index.html 页⾯⼊⼝
weixin.php 服务器端获取配置参数
configdata.php将配置转为借⼝输出
getconfig.js ⽤ajax获取configdata.php的数据
share.js 分享回调函
index.js 打包后最终html调⽤js⽂件
index.html html静态⽂件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>静态页⾯分享测试</title>
</head>
<body>
<script src="res.wx.qq/open/js/jweixin-1.0.0.js"></script>
<script src="statics/js/index.js"></script>
</body>
</html>
configdata.php 后台获取配置的参数注意url要写上⾃⼰被分享的页⾯url不然会报invalid signature错误 <?php
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
$url = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这⾥参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->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() {
// jsapi_ticket 应该全局存储与更新,以下代码以写⼊到⽂件中做⽰例
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
$url = "api.weixin.qq/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写⼊到⽂件中做⽰例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$url = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
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_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
weixin.php 将配置参数格式化输出
<?php
require_once "weixin.php";
$jssdk = new JSSDK(appId, appSecretecret);
$signPackage = $jssdk->GetSignPackage();
class Config{
var $appId;
var $timestamp;
var $nonceStr;
var $signature;
var $url;
}
$config = new Config();
$config -> appId = $signPackage["appId"];
$config -> timestamp = $signPackage["timestamp"];
$config -> nonceStr = $signPackage["nonceStr"];
$config -> signature = $signPackage["signature"];
$config -> url = $signPackage["url"];
echo json_encode($config);
>
getconfig.js 使⽤ajax获取接⼝数据(配置参数)
var getConfig = function(callback) {
$.ajax({
url: "ueche/api/configdata.php",
type: "get",
success: function(data) {
callback(data);
}
})
}
share.js 分享函数
var getWeixincofig = require("./getconfig.js");
getWeixincofig(shareweixin);
function shareweixin(data) {
var data = JSON.parse(data);
console.log(data);
fig({
debug:true,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: Str,
signature: data.signature,
jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage'] });
wxShare();
}
function wxShare() {
//检测api是否⽣效
wx.checkJsApi({
jsApiList: [
'getNetworkType',
'previewImage'
],
success: function(res) {
console.log(JSON.stringify(res));
}
});
//分享给好友
title: '趣学车-有温度的互联⽹驾校',
desc: '想去学车,就趣学车!',
link: 'ueche',
imgUrl: 'ueche/....png'
});
//分享到朋友圈
title: '趣学车-有温度的互联⽹驾校',
desc: '想去学车,就趣学车!',
link: 'ueche',
imgUrl: 'ueche/....png'
});
});
}
var webpack = require('webpack');
entry: {
index: './share.js',
},
output: {
path: './',
filename: '[name].js'
}
};
本⽂已被整理到了《》,欢迎⼤家学习阅读。
为⼤家推荐现在关注度⽐较⾼的⼩程序教程⼀篇:⼩编为⼤家精⼼整理的,希望喜欢。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。