实例
/医院
* Created by PhpStorm.
* User: Administrator
* Date: 2018/6/1 0001
* Time: 上午 9:52
*/
namespace app\index\model;
use think\facade\Request;
use think\facade\Config;
use think\Model;
use think\Facade\Cache;
class WeixinModel extends Model
{
public function checkToken(){
$signature = Request::param('signature');
$timestamp = Request::param('timestamp');
$nonce = Request::param('nonce');
$echostr = Request::param('echostr');
$Token = Config::get('app.Token');
$tmpArr = array($timestamp, $nonce, $Token);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr != $signature) {
return false;
}
return $echostr;
}
public function getAccessToken($isCache = true)
{
if (!$isCache) {
Cache::rm("access_token");
}
$access_token = Cache::get("access_token");
if ($access_token && $isCache) {
return $access_token;
}
$APPID = Config::get('app.AppID');
$AppSecret = Config::get('app.AppSecret');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $APPID . "&secret=" . $AppSecret;
$res = http_Get($url);
$res = json_decode($res,true);
// $access_token = Request::param('access_token');
// $expires_in = Request::param('expires_in');
Cache::set("access_token", $res['access_token'], $res['expires_in'] - 600);
return $res['access_token'];
}
}
运行实例 »
点击 "运行实例" 按钮查看在线实例
本文介绍了如何使用PhpStorm开发环境下实现微信公众号接口的Token验证与accessToken的缓存管理,包括checkToken函数的实现和getAccessToken方法的调用过程。
1414

被折叠的 条评论
为什么被折叠?



