指定位置插入字符串
2021-01-11
//微信分享接口
function make_nonceStr()
{
$codeSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i<16; $i++) {
$codes[$i] = $codeSet[mt_rand(0, strlen($codeSet)-1)];
}
$nonceStr = implode($codes);
return $nonceStr;
}
function make_signature($nonceStr,$timestamp,$jsapi_ticket,$url)
{
$tmpArr = array(
'noncestr' => $nonceStr,
'timestamp' => $timestamp,
'jsapi_ticket' => $jsapi_ticket,
'url' => $url
);
ksort($tmpArr, SORT_STRING);
$string1 = http_build_query( $tmpArr );
$string1 = urldecode( $string1 );
//dump($string1);
$signature = sha1( $string1 );
return $signature;
}
function make_ticket($appId,$appsecret)
{
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents(config('wwwrootdir')."/token/access_token_".$appId.".json"));
if ($data->expire_time < time()) {
$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appsecret;
$json = file_get_contents($TOKEN_URL);
$result = json_decode($json,true);
$access_token = $result['access_token'];
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen(config('wwwrootdir')."/token/access_token_".$appId.".json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$access_token = $data->access_token;
}
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents(config('wwwrootdir')."/token/jsapi_ticket_".$appId.".json"));
if ($data->expire_time < time()) {
$ticket_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
$json = file_get_contents($ticket_URL);
$result = json_decode($json,true);
$ticket = $result['ticket'];
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen(config('wwwrootdir')."/token/jsapi_ticket_".$appId.".json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
使用
//分享接口
$appId = config('appid');
$appsecret = config('appsecret');
$timestamp = time();
$jsapi_ticket = self::make_ticket($appId,$appsecret);
$nonceStr = self::make_nonceStr();
$newurl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//dump($newurl);
$signature = self::make_signature($nonceStr,$timestamp,$jsapi_ticket,$newurl);
$this->assign('appId',$appId);
$this->assign('timestamp',$timestamp);
$this->assign('nonceStr',$nonceStr);
$this->assign('signature',$signature);
VIEW
wx.config({
debug: false,
appId: '=$appId?>',
timestamp: =$timestamp?>,
nonceStr: '=$nonceStr?>',
signature: '=$signature?>',
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'translateVoice',
'startRecord',
'stopRecord',
'onVoiceRecordEnd',
'playVoice',
'onVoicePlayEnd',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
]
});
wx.ready(function () {
var shareData = {
title: '恭喜您免费获得!',
desc: '要安心',
link: 'http://.cn/home/index?id={$id}',
imgUrl: 'http://.cn/b.png'
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
});
wx.error(function (res) {
window.location.reload()
});
2021-01-11
2021-01-11
2021-01-11
2021-01-11
2021-01-11
2021-02-25
2021-01-11
2021-05-25
评论