Browse Source

验证xml接口请求、微信测试账号认证3

master
wanghongjun 1 year ago
parent
commit
71d6b6128b
  1. 80
      app/controller/wechat/Base.php
  2. 10
      app/controller/wechat/Login.php
  3. 16
      config/jwt.php

80
app/controller/wechat/Base.php

@ -9,13 +9,9 @@ declare (strict_types=1);
namespace app\controller\wechat;
use app\BaseController;
use app\service\user\LoginService;
use app\util\ReturnCode;
use fast\FuncException;
use think\App;
use Firebase\JWT\JWT;
use think\facade\Env;
use think\facade\Request;
use think\facade\Session;
use think\Response;
class Base extends BaseController {
@ -25,60 +21,6 @@ class Base extends BaseController {
protected $user = [];
public $user_id = '';
// public function __construct()
// {
// $app = new App();
// parent::__construct($app);
//
// try {
// if(!$this->user){
// $bool = (new LoginService())->userAutologin();
// if($bool){
// $this->user = session('user');
// }
// }
// if ($this->user){
// $this->user_id = $this->user['id'];
// }
//// //需要登录接口进行校验
// if (!$this->match($this->noNeedLogin)){
// $this->checklogin();
// }
// } catch (\Exception $e) {
// return $this->buildFailed($e->getCode() ?: 400,$e->getMessage());
// }
// }
/**
* 关联检测是否包含该请求是否包含该方法
* @param $arr
* @return bool
*/
public function match($arr = []) {
$request = Request::instance();
$arr = is_array($arr) ? $arr : explode(',', $arr);
if (! $arr) {
return false;
}
$arr = array_map('strtolower', $arr);
// 是否存在
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
return true;
}
// 没找到匹配
return false;
}
/**
* 用户登录检测
*/
public function checkLogin() {
$login = new LoginService();
if (!$login->isLogin()){
throw new FuncException('用户未登录',302);
}
}
public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ReturnCode::SUCCESS): Response {
$return = [
'code' => $code,
@ -110,4 +52,24 @@ class Base extends BaseController {
$this->debug[] = $data;
}
}
/**
* 生成验签
* @param $data
* @return string
*/
protected function signToken($data): string
{
$key = config('jwt.key'); //这里是自定义的一个随机字串,应该写在config文件中的,解密时也会用,相当于加密中常用的 盐-salt
$token = array(
"iss" => $key, //签发者 可以为空
"aud" => '', //面象的用户,可以为空
"iat" => time(), //签发时间
"nbf" => time() + 3, //在什么时候jwt开始生效 (这里表示生成100秒后才生效)
"exp" => time() + 7200, //token 过期时间
"data" => $data //记录的userid的信息,这里是自已添加上去的,如果有其它信息,可以再添加数组的键值对
);
return JWT::encode($token, $key, "HS384"); //根据参数生成了token,可选:HS256、HS384、HS512、RS256、ES256等
}
}

10
app/controller/wechat/Login.php

@ -3,7 +3,6 @@
namespace app\controller\wechat;
use app\service\user\LoginService;
use think\App;
use think\facade\Request;
class Login extends Base
@ -14,13 +13,6 @@ class Login extends Base
public $auth;
protected $valid = \app\validate\Login::class;
public function __construct()
{
$app = new App();
parent::__construct($app);
}
/**
* @title 登录凭证校验
* @return \think\Response|void
@ -28,6 +20,7 @@ class Login extends Base
public function code2session(){
try {
validate($this->valid)->scene('code2session')->check(Request::post());
$code = $this->request->param('code');
$iv = $this->request->param('iv');
$encryptedData = $this->request->param('encryptedData');
@ -51,6 +44,7 @@ class Login extends Base
$unionid = $this->request->post('unionid');
$loginService = new LoginService();
$user = $loginService->userLogin($phone, $openid, $unionid);
$user['token'] = $this->signToken($user);
return $this->buildSuccess($user);
} catch (\Exception $e) { //错误消息 $e->getMessage()
return $this->buildFailed($e->getCode() ?: 400,$e->getMessage());

16
config/jwt.php

@ -0,0 +1,16 @@
<?php
// +----------------------------------------------------------------------
// | jwt设置
// +----------------------------------------------------------------------
return[
"key" => "invoice_jwt@key",
// 签发时间
'lat' => time(),
// 生效时间
'nbf' => time(),
// 过期时间
// 'exp' => time() + (3600 * 5),
];
Loading…
Cancel
Save