微信JSSDK使用

2021-10-01 07:14:23 字數 2676 閱讀 8377

1.步驟一:繫結網域名稱

2.步驟二:引入js檔案

3.步驟三:通過config介面注入許可權驗證配置

wx.config(>', // 必填,生成簽名的時間戳

noncestr: '<>', // 必填,生成簽名的隨機串

signature: '<>',// 必填,簽名

jsapilist: ['getlocation','openlocation'] // 必填,需要使用的js介面列表

});

建立一張表tm_token,用以儲存access_tokenjsapi_ticket預設初始將odate設為0,就可以第一次獲取了

要獲取access_token,需要在開發》基本配置配置ip白名單

//判斷獲取的access_token是否過期

$sql="select * from ".$prefix."_token where type='access_token' ";

$token=$db->getrow($sql);

$access_getdate=$token['odate'];

$nowdate=time();

if(($nowdate-$access_getdate)>7000 ||$access_getdate==0)else

}else

}使用上述的access_token 獲得jsapi_ticket

//用上一步拿到的access_token 採用http get方式請求獲得 jsapi_ticket

$prefix=$this->prefix;

$db=$this->db;

//var_dump($access_token);

$sql="select * from ".$prefix."_token where type='jsapi_ticket' ";

$token=$db->getrow($sql);

$access_getdate=$token['odate'];

$nowdate=time();

if(($nowdate-$access_getdate)>=7000||$access_getdate==0)else

}else

}

使用上述的jsapi_ticket,生成js-sdk許可權驗證的簽名

//開始生成簽名

//第一步,準備參與簽名的字段

$noncestr = 'sxll_***x'; //隨機字串

$timestamp = time(); //timestamp(時間戳)

//第二步,對所有待簽名引數按照欄位名的ascii 碼從小到大排序(字典序)後,

//使用url鍵值對的格式(即key1=value1&key2=value2…)拼接成字串string1:

$string1 = "jsapi_ticket=$jsapi_ticket&noncestr=$noncestr×tamp=$timestamp&url=$url";

//第三步進行sha1加密上述字串string1

$signature = sha1($string1);

$output['noncestr'] = $noncestr;

$output['timestamp'] = $timestamp;

$output['signature'] = $signature;

如果前端出現invalid signauture ,可以使用如下工具校驗一下

4使用jssdk,獲取定位資訊,並且載入地圖,顯示當前位置

function createmap() );

var marker = new amap.marker();

var geocoder = new amap.geocoder();

marker.on('dragend', function (data) else

});})

map.add(marker);

geocoder.getaddress(new amap.lnglat(longitude, latitude), function (status, result) else

});}

});}

微信jssdk使用

使用jssdk之前,我們需要在伺服器端準備什麼資料 jssdk使用的config wx.config 這裡我們需要自己去準備的資料,主要有 timestamp 這個一般可用伺服器當前時間設值,php now noncestr 隨機的字串,這個字串可以任意長度,自己設定隨機串 signature 簽名...

微信JS SDK如何使用

雖然開發者平台上說的清清楚楚,但是使用起來就是not ok 找到功能設定,填入js介面安全網域名稱 wx.config wx物件提供了很多方法呼叫,而這些方法的返回值可以在config裡面設定,方便除錯的時候觀察報錯資訊。簽名步驟比較小複雜 生成簽名 the jsapi ticket.the non...

微信js sdk使用說明

3獲取token 4獲取ticket,將access token提換為3步中獲取的token即可。結果 5生成簽名 填寫規則 jsapi ticket 步驟四獲取的ticket。noncestr wx中填寫的內容 timestamp wx中填寫的內容 url 呼叫js的url 比如最後生成的簽名是 ...