4 changed files with 125 additions and 83 deletions
@ -0,0 +1,110 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service\invoice; |
||||
|
|
||||
|
use Endroid\QrCode\Builder\Builder; |
||||
|
use Endroid\QrCode\Encoding\Encoding; |
||||
|
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; |
||||
|
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter; |
||||
|
use Endroid\QrCode\Label\Font\NotoSans; |
||||
|
use Endroid\QrCode\Writer\PngWriter; |
||||
|
use Firebase\JWT\JWT; |
||||
|
use Firebase\JWT\Key; |
||||
|
use think\facade\Request; |
||||
|
|
||||
|
class InvoiceQrCode |
||||
|
{ |
||||
|
/** |
||||
|
* 组合 url |
||||
|
* @param $id |
||||
|
* @return string |
||||
|
*/ |
||||
|
public static function getDownFileUrl($id): string |
||||
|
{ |
||||
|
$url = Request::domain(true); |
||||
|
$token = self::signToken($id); |
||||
|
return $url . '/api/InvoiceIssuance/downFile?token=' . $token; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取二维码 |
||||
|
* @param $id |
||||
|
* @return string |
||||
|
*/ |
||||
|
public static function getQrCode($id): string |
||||
|
{ |
||||
|
$codeUrl = self::getDownFileUrl($id); |
||||
|
|
||||
|
$result = Builder::create() |
||||
|
->writer(new PngWriter()) |
||||
|
->writerOptions([]) |
||||
|
->data($codeUrl) |
||||
|
->encoding(new Encoding('UTF-8')) |
||||
|
->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||||
|
->size(300) |
||||
|
->margin(10) |
||||
|
->labelText('')// 发票已开具,请扫描二维码获取发票! |
||||
|
->labelFont(new NotoSans(20)) |
||||
|
->labelAlignment(new LabelAlignmentCenter()) |
||||
|
->validateResult(false) |
||||
|
->build(); |
||||
|
|
||||
|
return $result->getDataUri(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成token |
||||
|
* @param $id |
||||
|
* @return string |
||||
|
*/ |
||||
|
protected static function signToken($id): string |
||||
|
{ |
||||
|
$data = [ |
||||
|
'invoice_issuance_id' => $id, |
||||
|
'now_time' => time() |
||||
|
]; |
||||
|
$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等 |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 验证token |
||||
|
* @param $token |
||||
|
* @return array|int[] |
||||
|
*/ |
||||
|
public static function checkToken($token): array |
||||
|
{ |
||||
|
$key = config('jwt.key'); |
||||
|
$status = array("code" => 0); |
||||
|
try { |
||||
|
JWT::$leeway = 60; //当前时间减去60,把时间留点余地 |
||||
|
$decoded = JWT::decode($token, new Key($key, 'HS384')); //同上的方式,这里要和签发的时候对应 |
||||
|
$arr = (array)$decoded; |
||||
|
$res['code'] = 1; |
||||
|
$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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue