香洲二院小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mini_xzey/app/Services/MedicalAuth/Client.php

182 lines
4.6 KiB

<?php
declare(strict_types=1);
namespace App\Services\MedicalAuth;
use App\Utils\Traits\HttpRequest;
use App\Utils\Traits\Logger;
use Exception;
// use JetBrains\PhpStorm\ArrayShape;
/**
* 医保移动支付免密授权 - 微信端
*
* @link https://yb.qq.com/yibao-payment/doc?nodeId=83679977515675648
*/
class Client
{
use Logger;
use HttpRequest;
/**
* 配置数组
* @var string[]
*/
/*#[ArrayShape([
'app_id' => "string",
'partner_id' => "string",
'partner_secret' => "string",
'channel' => "string",
'org_chnl_crtf_codg' => "string",
'org_codg' => "string",
'org_app_id' => "string",
'auth_return_url' => "string",
])]*/
private array $config;
/**
* 城市编码 441721 香洲区
* @var string
*/
private string $city_code = '441721';
/**
* 授权查询host地址
* @var string
*/
private string $host = 'https://test-receiver.wecity.qq.com';
// private string $host = 'https://mip-receiver.tengmed.com';
/**
* 免密授权host地址
* @var string
*/
private string $auth_host = 'https://mitest.wecity.qq.com';
// private string $auth_host = 'https://card.wecity.qq.com';
/**
* Authorization Class Construct.
*/
public function __construct()
{
$this->config = config('wechat.medical.auth');
}
/**
* 获取签名
* @param string $timestamp
* @return string
*/
private function getSign(string $timestamp): string
{
return hash_hmac("sha256", $this->config['partner_id'] . $timestamp, $this->config['partner_secret']);
}
/**
* 获取RequestId
* @return string
*/
private function getRequestId(): string
{
$char_id = strtoupper(md5(uniqid((string)mt_rand(), true)));
return substr($char_id, 0, 8)
. substr($char_id, 8, 4)
. substr($char_id, 12, 4)
. substr($char_id, 16, 4)
. substr($char_id, 20, 12);
}
/**
* 获取毫秒
* @return int
*/
private function getMillisecond(): int
{
[$t1, $t2] = explode(' ', microtime());
return (int)sprintf('%.0f', ((float)$t1 + (float)$t2) * 1000);
}
/**
* @param string $endpoint
* @param array $params
* @param string $method
* @param array $options
* @return array|mixed
*/
protected function requestHandle(string $endpoint, array $params = [], string $method = 'post', array $options = []): mixed
{
$timestamp = $this->getMillisecond();
$params = array_merge([
'base_uri' => $this->host,
'headers' => [
'god-portal-timestamp' => $timestamp,
'god-portal-request-id' => $this->getRequestId(),
'god-portal-signature' => $this->getSign((string)$timestamp),
],
'json' => $params
], $options);
try {
$response = $this->request($method, $endpoint, $params);
$this->writerLog($endpoint, $params, $response);
return $response;
} catch (Exception $e) {
$err_msg = "{$e->getMessage()} ON {$e->getFile()}:{$e->getLine()}";
$this->info($endpoint, $params, $err_msg);
return [
'code' => -1,
//'message' => $err_msg
'message' => $e->getMessage()
];
}
}
/**
* 获取小程序授权跳转地址
* @return string
*/
public function getMiniProgramAuthRedirectUrl(): string
{
$url = 'auth/pages/bindcard/auth/index';
$data = [
'openType' => 'getAuthCode',
'cityCode' => $this->config['city_code'],
'channel' => $this->config['channel'],
'orgChnlCrtfCodg' => $this->config['org_chnl_crtf_codg'],
'orgCodg' => $this->config['org_codg'],
'bizType' => '04107',
'orgAppId' => $this->config['org_app_id'],
];
$redirect_url = $url . '?' . http_build_query($data);
$this->writerLog($url, $data, '');
return $redirect_url;
}
/**
* 查询用户授权
* @param string $auth_code
* @param string $open_id
* @return mixed
*/
public function userQuery(string $auth_code, string $open_id): mixed
{
$uri = '/api/mipuserquery/userQuery/' . $this->config['partner_id'];
$param = [
'qrcode' => $auth_code,
'openid' => $open_id,
];
return $this->requestHandle($uri, $param);
}
}