微信文檔 ,https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html,php
在用微信自定義分享,遇到了幾個坑,記錄一下。html
注意點2:access_token是公衆號的全局惟一接口調用憑據,有效時間2小時,注意緩存,不能每次都獲取新的,次數一天2000次,能夠緩存,或者存入數據表,用時判斷當前時間和存入的時間差。實在超過,可在微信後臺 接口權限中清空次數,一個月只能清3次。android
其中的jsapi_ticket也是,緩存或者存表。web
注意點3: 簽名的注意事項ajax
注意點4:簽名時的url, 在pc的微信中,url沒有拼接什麼,而在手機的微信中,會在你當前頁面上自動拼接上朋友圈 from=timeline,微信羣 from=groupmessage,好友分享 from=singlemessage,簽名時要注意,不要漏掉,能夠在前臺ajax請求時,或者當前頁面的url,傳入後臺簽名。json
注意點5:windows
在手機微信中這些接口確實已經廢棄,用了,就會提示不支持,而在pc微信中,仍是用這些,不能用新的api
因爲pc和手機使用的方法不一樣,在php中定義了兩套,用手機打開時訪問一套,pc打開時訪問一套,或者弄訪問地址徹底不同的頁面,pc一個地址,手機一個地址數組
下面是php中的定義,手機和pc雖然是同一個網址,可是真正訪問的仍是兩套緩存
function is_mobile(){ $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte"); $is_mobile = false; foreach ($mobile_agents as $device) {//這裏把值遍歷一遍,用於查找是否有上述字符串出現過 if (stristr($user_agent, $device)) { //stristr 查找訪客端信息是否在上述數組中,不存在即爲PC端。 $is_mobile = true; break; } } return $is_mobile; } if(is_mobile()){ //跳轉至wap分組 define('BIND_MODULE','Mobile'); }else{ define('BIND_MODULE','Home'); }
判斷access_token 過時,我這只是2000秒,遠遠不到7200秒(2小時),
time字段設置爲了默認值當前時間
$access_time = M("dc_weixin")->order("id desc")->getField("time"); $time = time() - strtotime($access_time); if($time>2000){ $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->app_id."&secret=".$this->app_secret; $content = file_get_contents($url); $result = json_decode($content); $data['access_token'] = $result->access_token; $weixin = M("dc_weixin")->data($data)->add(); }
//jsapi_ticket
public function getJk(){ $this->getToken(); $access_token = M("dc_weixin")->order("id desc")->getField("access_token"); $jk_time = M("dc_jk")->order("id desc")->getField("time"); $time = time() - strtotime($jk_time); if($time>2000){ $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi"; $content = file_get_contents($url); $result = json_decode($content); $data['jk'] = $result->ticket; $weixin = M("dc_jk")->data($data)->add(); } }
/* * 隨機字符串 */ function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen; return str_replace("-","",$uuid); }
//簽名 頁面裏ajax訪問,簽名的方法
public function dcsign_mob(){
$html = $_SERVER['HTTP_REFERER']; $time = strtotime(date('Y-m-d H:i:s',time())); $nostr = $this->create_guid(); $jk= M("dc_jk")->order("id desc")->getField("jk"); $url = "jsapi_ticket=".$jk."&noncestr=".$nostr."×tamp=".$time."&url=".$html; $signature = sha1($url); $data['html'] = $html; $data['time'] = $time; $data['nostr'] = $nostr; $data['signature'] = $signature; $this->ajaxReturn($data); //返回到html也中 }
$.ajax({
url: "{:U('**/**')}",
type: "get", //data:{} //這裏沒有將頁面的url傳入簽名方法中,
timeout: 20000, //超時時間設置,單位毫秒
success: function (data) {
console.log(data);
wx.config({
debug: true, // 開啓調試模式,調用的全部api的返回值會在客戶端alert出來,若要查看傳入的參數,能夠在pc端打開,參數信息會經過log打出,僅在pc端時纔會打印。
appId: '*****', // 必填,公衆號的惟一標識
timestamp: data.time, // 必填,生成簽名的時間戳
nonceStr: data.nostr, // 必填,生成簽名的隨機串
signature: data.signature,// 必填,簽名
jsApiList: [ 'checkJsApi',
'onMenuShareTimeline','onMenuShareAppMessage','updateAppMessageShareData','updateTimelineShareData'] // 必填,須要使用的JS接口列表
});
wx.ready(function() {
wx.updateAppMessageShareData({
title: shareTitle, // 分享標題
desc: descContent, // 分享描述
link: lineLink, // 分享連接,該連接域名或路徑必須與當前頁面對應的公衆號JS安全域名一致
imgUrl: imgUrl, // 分享圖標
success: function () {
// 用戶確認分享後執行的回調函數
},
cancel: function () {
// 用戶取消分享後執行的回調函數
}
});
wx.updateTimelineShareData({
title: shareTitle, // 分享標題
desc: descContent, // 分享描述
link: lineLink, // 分享連接,該連接域名或路徑必須與當前頁面對應的公衆號JS安全域名一致
imgUrl: imgUrl, // 分享圖標
success: function () {
// 用戶確認分享後執行的回調函數
},
cancel: function () {
// 用戶取消分享後執行的回調函數
}
});
});
},
error: function (xhr, textStatus, errorThrown) {
/*錯誤信息處理*/
alert("進入error---");
alert("狀態碼:"+xhr.status);
alert("狀態:"+xhr.readyState);//當前狀態,0-未初始化,1-正在載入,2-已經載入,3-數據進行交互,4-完成。
alert("錯誤信息:"+xhr.statusText );
alert("返回響應信息:"+xhr.responseText );//這裏是詳細的信息
alert("請求狀態:"+textStatus);
alert(errorThrown);
alert("請求失敗");
},
complete: function (XMLHttpRequest, status) {
if (status == 'timeout') {
layer.msg("請求超時,請稍後再試!");
layer.close(index);
}
}
});