JS-SDK micro-channel custom note share access point

Micro-channel document, https: //developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html,

Share with the custom micro letter, met a few pit, record it.

Note Point 1: Go to "Settings Public No." "functional settings" fill in "JS interface security domain." No authentication is required public attention, otherwise you may be prompted to use interface has no authority;

Note point 2: access_token is a globally unique interface call number of public credentials, the effective time of 2 hours, pay attention to the cache, can not always get a new, 2,000 times a day can be cached, or stored in a data table, determine the current time with the time and into the time difference. It is more than can be emptied in the number of micro letter backstage interfaces permissions, can only clean three times a month .

Wherein the jsapi_ticket is, the cache memory or table.

Note point 3: The signature of Notes

 

 

 

 Note point 4: url when the signature, in micro-channel pc's, url no stitching nothing, and in the micro-channel phone's automatically stitching on your current page circle of friends from = timeline, micro-channel group from = groupmessage, friends to share = singlemessage from , should pay attention to when signing, do not miss, can be in the foreground ajax request, or url of the current page, passing in the background signature.

Notes 5:

 

 

In the mobile micro-letter, these interfaces, it has been abandoned, used, it will prompt does not support, and in the pc micro letter, or use them, not with the new

Due to the different methods pc and mobile phone use, as defined in php in two sets, a set of open access using a mobile phone, access to a pc when open, or get access to address a completely different page, pc an address, a phone address

Here is the definition of php, although the phone and pc is the same URL, but the real visit or two

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 = to false ;
     the foreach ( $ mobile_agents  AS  $ Device ) { // Here again traversing the value, the character string to find whether there occurred 
        IF ( stristr ( $ user_agent , $ Device )) { // stristr lookup visitor information is in the above-end array, that is, the PC does not exist. 
            IS_MOBILE $ = to true ;
             BREAK ; 
        } 
    } 
    return  $ IS_MOBILE ; 
} 

IF (IS_MOBILE ()) { // Go to wap packet 
    DEFINE ( 'BIND_MODULE', 'Mobile' ); 
}else{
    define('BIND_MODULE','Home');
}

 

Access_token expires judge, my only 2000 seconds, far less than 7200 seconds (two hours),

time field is set to a default value for the current 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."&timestamp=".$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); } } });

 

 

 

Guess you like

Origin www.cnblogs.com/andzhang/p/11982826.html
Recommended