7 changed files with 237 additions and 54 deletions
@ -1,47 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace app\controller\api; |
|||
|
|||
|
|||
use app\BaseController; |
|||
use think\facade\Log; |
|||
use think\Request; |
|||
class Wx extends BaseController |
|||
{ |
|||
|
|||
protected $token = 'invoiceApi'; |
|||
|
|||
public function verify(Request $request) |
|||
{ |
|||
|
|||
$param = $request->param(); |
|||
|
|||
$signature = $param['signature'] ?? '';// 签名 |
|||
$timestamp = $param['timestamp'] ?? '';// 时间戳 |
|||
$nonce = $param['nonce'] ?? '';// 随机数 |
|||
$echostr = $param['echostr'] ?? '0'; // 随机字符串 |
|||
|
|||
$checkRes = $this->checkSignature($signature,$timestamp,$nonce); |
|||
|
|||
$param['res_error'] = $checkRes; |
|||
|
|||
Log::write($param); |
|||
|
|||
echo $echostr;die; |
|||
} |
|||
|
|||
private function checkSignature($signature,$timestamp,$nonce):bool |
|||
{ |
|||
$token = $this->token; |
|||
$tmpArr = array($token, $timestamp, $nonce); |
|||
sort($tmpArr, SORT_STRING); |
|||
$tmpStr = implode( $tmpArr ); |
|||
$tmpStr = sha1( $tmpStr ); |
|||
|
|||
if( $tmpStr == $signature ){ |
|||
return true; |
|||
}else{ |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
<?php |
|||
|
|||
namespace app\middleware; |
|||
|
|||
use app\util\ReturnCode; |
|||
use Firebase\JWT\JWT; |
|||
use Firebase\JWT\Key; |
|||
use think\Response; |
|||
|
|||
class WechatAuth |
|||
{ |
|||
/** |
|||
* 后台登录验证 |
|||
* @param $request |
|||
* @param \Closure $next |
|||
* @return Response |
|||
*/ |
|||
public function handle($request, \Closure $next): Response { |
|||
$header = config('apiadmin.CROSS_DOMAIN'); |
|||
$token = $request->header('token', ''); |
|||
if ($token) { |
|||
$userInfo = $this->checkToken($token); |
|||
if ($userInfo['code'] == 2) { |
|||
return json([ |
|||
'code' => ReturnCode::AUTH_ERROR, |
|||
'msg' => $userInfo['msg'], |
|||
'data' => [] |
|||
])->header($header); |
|||
} else { |
|||
$request->wechat_user = $userInfo['data']; |
|||
$request->wechat_user_id = $userInfo['data']['id']; |
|||
} |
|||
|
|||
return $next($request); |
|||
} else { |
|||
return json([ |
|||
'code' => ReturnCode::AUTH_ERROR, |
|||
'msg' => '缺少token', |
|||
'data' => [] |
|||
])->header($header); |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
/** |
|||
* 验证token |
|||
* @param $token |
|||
* @return array|int[] |
|||
*/ |
|||
private function checkToken($token): array |
|||
{ |
|||
$key = config('jwt.key'); |
|||
$status = array("code" => 2); |
|||
try { |
|||
JWT::$leeway = 60; //当前时间减去60,把时间留点余地 |
|||
$decoded = JWT::decode($token, new Key($key, 'HS384')); //同上的方式,这里要和签发的时候对应 |
|||
$arr = (array)$decoded; |
|||
$res['code'] = 200; |
|||
$res['data'] = $arr['data']; |
|||
$res['data'] = json_decode(json_encode($res['data']), true);//将stdObj类型转换为array |
|||
return $res; |
|||
|
|||
} catch (\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 |
|||
$status['msg'] = "签名不正确"; |
|||
return $status; |
|||
} catch (\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 |
|||
$status['msg'] = "token失效"; |
|||
return $status; |
|||
} catch (\Firebase\JWT\ExpiredException $e) { // token过期 |
|||
$status['msg'] = "token失效"; |
|||
return $status; |
|||
} catch (\Exception $e) { //其他错误 |
|||
$status['msg'] = "未知错误"; |
|||
return $status; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
<?php |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
|
|||
class WechatPucode extends Model |
|||
{ |
|||
|
|||
/** |
|||
* 添加pucode |
|||
* @param $user_id |
|||
* @param $pucode |
|||
* @return WechatPucode|array|mixed|Model |
|||
* @throws \think\db\exception\DataNotFoundException |
|||
* @throws \think\db\exception\DbException |
|||
* @throws \think\db\exception\ModelNotFoundException\ |
|||
*/ |
|||
public static function createPuCode($user_id,$pucode) |
|||
{ |
|||
$data = [ |
|||
'wechat_user_id' => $user_id, |
|||
'pucode' => $pucode |
|||
]; |
|||
|
|||
$query = self::where($data)->find(); |
|||
if ($query) { |
|||
return $query; |
|||
} |
|||
|
|||
$data['create_time'] = time(); |
|||
|
|||
return self::create($data); |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
<?php |
|||
|
|||
namespace app\validate; |
|||
|
|||
use think\Validate; |
|||
|
|||
class WechatUser extends Validate |
|||
{ |
|||
/** |
|||
* 验证规则. |
|||
*/ |
|||
protected $rule = [ |
|||
'mobile|手机号' => 'require|mobile', |
|||
'pucode|用户编号' => 'require|number', |
|||
]; |
|||
|
|||
/** |
|||
* 提示消息. |
|||
*/ |
|||
protected $message = [ |
|||
]; |
|||
|
|||
/** |
|||
* 字段描述. |
|||
*/ |
|||
protected $field = [ |
|||
]; |
|||
|
|||
/** |
|||
* 验证场景. |
|||
*/ |
|||
protected $scene = [ |
|||
'savePuCode' => ['mobile', 'pucode'] |
|||
]; |
|||
|
|||
} |
|||
Loading…
Reference in new issue