批改状态:未批改
老师批语:

<?php
/医院
* Created by PhpStorm.
* User: Administrator
* Date: 2018/6/1 0001
* Time: 上午 9:52
*/
namespace app\index\controller;
use think\Controller;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Request;
//注意驼峰规则命名的控制器在url中访问中间是要加 _ 下划线的 略坑 不知道为什么这么设计
class Weixin extends Controller
{
public function __construct()
{
parent::__construct();
$this->model = model('WeixinModel');//这块控制器名字写错了 难怪出不来。。。。
}
//验证签名
public function check()
{
$valid = $this->model->checkToken();
if (!$valid) {
exit('signature error');
}
exit(input('get.echostr'));
}
//获取access_token
public function getAccessToken(){
return $this->model->getAccessToken(true);
}
}点击 "运行实例" 按钮查看在线实例
<?php
/医院
* 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'];
}
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号