qingjian/app/middleware/Token.php

46 lines
1.5 KiB
PHP

<?php
namespace app\middleware;
use app\model\User;
use app\model\WeixinAccountMini;
use Closure;
class Token
{
public function handle($request, Closure $next)
{
$result = [
'code' => -1,
'msg' => "未登录或登录过期",
'time' => time(),
];
$allHeader = request()->header();
$token = isset($allHeader["token"]) ? $allHeader["token"] : "";
$userId = isset($allHeader["userid"]) ? (int)$allHeader["userid"] : 0;
$wechatToken = isset($allHeader["wechattoken"]) ? $allHeader["wechattoken"] : "";
$openid = isset($allHeader["openid"]) ? $allHeader["openid"] : "";
//先验证是否有小程序登录信息
if ($wechatToken != "" && $openid != "") {
$wechatUserId = WeixinAccountMini::userTokenVerification($wechatToken, $openid);
//微信小程序登录过期或未登录
if ($wechatUserId <= 0) {
$result["msg"] = "小程序登录已过期或未登录";
return json($result);
}
}
//验证平台账号是否登录
if ($token != "" || $userId > 0) {
$IndexUserId = User::userTokenVerification($token, $userId);
if ($IndexUserId <= 0) {
$result["msg"] = "原账号登录已过期或未登录" /*. $token . "_" . $userId*/
;
return json($result);
}
return $next($request);
}
return json($result);
}
}