初始化提交
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
// 权限工具类
|
||||
class Auth
|
||||
{
|
||||
// 白名单接口
|
||||
public array $whiteApi;
|
||||
|
||||
// 特殊接口 存在需要
|
||||
public array $specialApi;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->whiteApi = [
|
||||
"/" => "*",
|
||||
"/patient/index" => "get",
|
||||
"/login/wechat_mobile_login" => "post",
|
||||
"/login/mobile_login" => "post",
|
||||
"/code/phone" => "post",
|
||||
"/disease/expertise" => "get",
|
||||
"/area/province" => "get",
|
||||
"/area/city" => "get",
|
||||
"/area/county" => "get",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测接口白名单
|
||||
* @param string $path_info 请求地址 /v1/user/info
|
||||
* @param string $method 请求方式 POST
|
||||
* @return bool true:在白名单 false:不在白名单
|
||||
*/
|
||||
public function checkApiWhiteList(string $path_info,string $method): bool
|
||||
{
|
||||
// 版本白名单-app使用
|
||||
/*$version_white_list = config('jwt.version_white_list', []);
|
||||
if (!empty($version_white_list)) {
|
||||
foreach ($version_white_list as $value) {
|
||||
$req = substr_compare($path_info,"/" . $value,0,strlen($value));
|
||||
if ($req === 0){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
if(!empty($this->whiteApi)){
|
||||
if (array_key_exists($path_info, $this->whiteApi)) {
|
||||
if ($this->whiteApi[$path_info] == '*') {
|
||||
return true;
|
||||
}
|
||||
if (stristr($this->whiteApi[$path_info], $method)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测token的快过期时间.
|
||||
* @param array $token token
|
||||
*/
|
||||
public function checkTokenExpTime(array $token): bool
|
||||
{
|
||||
$time_difference = $token['exp'] - time();
|
||||
|
||||
// 设定24小时过期时间
|
||||
if ($time_difference < (3600 * 24)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Model\Area;
|
||||
use App\Model\Hospital;
|
||||
use App\Model\TbHospitalMy;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Snowflake\IdGeneratorInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class Data
|
||||
{
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
// 迁移医院数据
|
||||
public function hospital()
|
||||
{
|
||||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||||
|
||||
$params = array();
|
||||
$params['prov_name'] = "贵州省";
|
||||
$hospital = TbHospitalMy::getList($params);
|
||||
foreach ($hospital as $key => $value) {
|
||||
$params = array();
|
||||
$params['hospital_name'] = $value['name'];
|
||||
$params['province'] = $value['prov_name'];
|
||||
$params['city'] = $value['city_name'];
|
||||
$res = Hospital::getOne($params);
|
||||
if(!empty($res)){
|
||||
dump("重复-跳过-".$value['name']);
|
||||
$params = array();
|
||||
$params['id'] = $value['id'];
|
||||
$res = Db::table('tb_hospital_my')->where($params)->delete();
|
||||
if (!$res) {
|
||||
return "删除错误";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$data['hospital_id'] = $generator->generate();;
|
||||
$data['hospital_name'] = $value['name'];
|
||||
$data['hospital_status'] = 1;
|
||||
|
||||
if ($value['level'] == 0){
|
||||
$data['hospital_level_name'] = "未知";
|
||||
}elseif ($value['level'] == 1){
|
||||
$data['hospital_level_name'] = "三甲";
|
||||
}elseif ($value['level'] == 2){
|
||||
$data['hospital_level_name'] = "三级";
|
||||
}elseif ($value['level'] == 3){
|
||||
$data['hospital_level_name'] = "二级";
|
||||
}elseif ($value['level'] == 4){
|
||||
$data['hospital_level_name'] = "其他";
|
||||
}else{
|
||||
continue;
|
||||
}
|
||||
|
||||
$data['post_code'] = $value['postcode'];
|
||||
$data['tele_phone'] = $value['office_phone'];
|
||||
$data['lat'] = $value['lat'];
|
||||
$data['lng'] = $value['lng'];
|
||||
$data['desc'] = $value['info'];
|
||||
$data['created_at'] = date('Y-m-d H:i:s',time());
|
||||
$data['updated_at'] = date('Y-m-d H:i:s',time());
|
||||
|
||||
// 省
|
||||
$params = array();
|
||||
$params['area_name'] = $value['prov_name'];
|
||||
$province = Area::getOne($params);
|
||||
if(empty($province)){
|
||||
dump("省份-未知-".$value['prov_name']);
|
||||
continue;
|
||||
}else{
|
||||
$data['province_id'] = $province['area_id'];
|
||||
$data['province'] = $province['area_name'];
|
||||
}
|
||||
|
||||
// 市
|
||||
$params = array();
|
||||
$params['area_name'] = $value['city_name'];
|
||||
$params['parent_id'] = $province['area_id'];
|
||||
$city = Area::getOne($params);
|
||||
if(empty($city)){
|
||||
dump("市区-未知-".$value['city_name']);
|
||||
continue;
|
||||
}else{
|
||||
$data['city_id'] = $city['area_id'];
|
||||
$data['city'] = $city['area_name'];
|
||||
}
|
||||
|
||||
$params = array();
|
||||
$params['area_name'] = $value['county_name'];
|
||||
$params['parent_id'] = $city['area_id'];
|
||||
$county = Area::getOne($params);
|
||||
if(empty($county)){
|
||||
dump("区县-未知-".$value['county_name']);
|
||||
continue;
|
||||
}else{
|
||||
$data['county_id'] = $county['area_id'];
|
||||
$data['county'] = $county['area_name'];
|
||||
}
|
||||
|
||||
$data['address'] = $value['address'];
|
||||
|
||||
$res = Hospital::addHospital($data);
|
||||
if (empty($res)) {
|
||||
dump("添加-失败-跳过");
|
||||
continue;
|
||||
}
|
||||
|
||||
$params = array();
|
||||
$params['id'] = $value['id'];
|
||||
$res = Db::table('tb_hospital_my')->where($params)->delete();
|
||||
if (!$res) {
|
||||
return "删除错误";
|
||||
}
|
||||
}
|
||||
|
||||
return success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
|
||||
class Http
|
||||
{
|
||||
#[Inject]
|
||||
protected RequestInterface $request;
|
||||
|
||||
/**
|
||||
* 获取客户端ip地址
|
||||
* @return string
|
||||
*/
|
||||
public function getIp():string
|
||||
{
|
||||
$res = $this->request->getServerParams();
|
||||
if (isset($res['http_client_ip'])) {
|
||||
return $res['http_client_ip'];
|
||||
} elseif (isset($res['http_x_real_ip'])) {
|
||||
return $res['http_x_real_ip'];
|
||||
} elseif (isset($res['http_x_forwarded_for'])) {
|
||||
//部分CDN会获取多层代理IP,所以转成数组取第一个值
|
||||
$arr = explode(',', $res['http_x_forwarded_for']);
|
||||
return $arr[0];
|
||||
} else {
|
||||
return $res['remote_addr'];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
/**
|
||||
* guzzle请求封装
|
||||
*/
|
||||
class HttpRequest
|
||||
{
|
||||
#[Inject]
|
||||
protected Client $client;
|
||||
|
||||
/**
|
||||
* 请求封装
|
||||
* @param string $path
|
||||
* @param array $option
|
||||
* @return array
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getRequest(string $path,array $option = []): array
|
||||
{
|
||||
$response = $this->client->post($path, $option);
|
||||
|
||||
if ($response->getStatusCode() != '200'){
|
||||
// 请求失败
|
||||
throw new BusinessException(HttpEnumCode::SERVER_ERROR,$response->getBody()->getContents());
|
||||
}
|
||||
|
||||
return json_decode($response->getBody(),true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use Firebase\JWT\ExpiredException;
|
||||
use Firebase\JWT\Key;
|
||||
use Firebase\JWT\SignatureInvalidException;
|
||||
|
||||
class Jwt
|
||||
{
|
||||
/**
|
||||
* 生成jwttoken
|
||||
* @param array $data 自定义数组
|
||||
*/
|
||||
public function encode(array $data): string
|
||||
{
|
||||
$time = time();
|
||||
$secret = config('jwt.secret');
|
||||
$expire = config('jwt.ttl');
|
||||
$algo = config('jwt.algo');
|
||||
$payload = [
|
||||
'iss' => 'gdxz',
|
||||
'iat' => $time,
|
||||
'nbf' => $time,
|
||||
'exp' => $time + $expire,
|
||||
'userInfo' => $data,
|
||||
];
|
||||
// token_type:bearer
|
||||
return \Firebase\JWT\JWT::encode($payload, $secret, $algo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码jwttoken
|
||||
* @param string $token token数据,不卸载bearer
|
||||
* @return array
|
||||
*/
|
||||
public function decode(string $token): array
|
||||
{
|
||||
$secret = config('jwt.secret');
|
||||
try {
|
||||
$jwt = json_decode(json_encode(\Firebase\JWT\JWT::decode($token, new Key($secret, 'HS256'))), true); // 解密jwt
|
||||
|
||||
} catch (SignatureInvalidException $e) {
|
||||
// 签名不正确
|
||||
throw new BusinessException( $e->getMessage(),HttpEnumCode::TOKEN_ERROR);
|
||||
|
||||
} catch (ExpiredException|\UnexpectedValueException|\InvalidArgumentException $e) {
|
||||
// token过期
|
||||
throw new BusinessException( $e->getMessage(),HttpEnumCode::TOKEN_EXPTIRED);
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// 其他错误:
|
||||
throw new BusinessException( $e->getMessage());
|
||||
}
|
||||
|
||||
return $jwt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace App\Utils;
|
||||
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* 重写log类
|
||||
*/
|
||||
class Log
|
||||
{
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
self::getInstance()->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
public static function getInstance(string $name = 'app'): LoggerInterface
|
||||
{
|
||||
return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
|
||||
class Mask
|
||||
{
|
||||
/*
|
||||
* 用户名用*号处理
|
||||
* 用户名:英文、中文、中英文混合的、中英文字符混合
|
||||
* 首字母和末尾保留,中间用*号代替
|
||||
* */
|
||||
public static function maskNameStr($str = '')
|
||||
{
|
||||
if (empty($str) ){
|
||||
return $str;
|
||||
}
|
||||
|
||||
$str = mb_convert_encoding( $str , 'UTF-8', 'auto' );
|
||||
//判断是否包含中文字符
|
||||
if(preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str)) {
|
||||
//按照中文字符计算长度
|
||||
$len = mb_strlen($str, 'UTF-8');
|
||||
//echo '中文';
|
||||
if($len >= 3){
|
||||
//三个字符或三个字符以上掐头取尾,中间用*代替
|
||||
$str = mb_substr($str, 0, 1, 'UTF-8') . '*' . mb_substr($str, -1, 1, 'UTF-8');
|
||||
} elseif($len == 2) {
|
||||
//两个字符
|
||||
$str = mb_substr($str, 0, 1, 'UTF-8') . '*';
|
||||
}
|
||||
} elseif(preg_match("/[A-Za-z]/", $str)) {
|
||||
//按照英文字串计算长度
|
||||
$len = mb_strlen($str);
|
||||
//echo 'English';
|
||||
if($len >= 3) {
|
||||
//三个字符或三个字符以上掐头取尾,中间用*代替
|
||||
$str = mb_substr($str, 0, 1) . '*' . mb_substr($str, -1);
|
||||
} elseif($len == 2) {
|
||||
//两个字符
|
||||
$str = mb_substr($str, 0, 1) . '*';
|
||||
}
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
/*
|
||||
* 手机号、固话加密
|
||||
* 示例:
|
||||
* 固话:0510-89754815 0510-8****815
|
||||
* 手机号:18221234158 18*******58
|
||||
* */
|
||||
public static function maskPhoneStr($phone)
|
||||
{
|
||||
if (empty($phone)){
|
||||
return $phone;
|
||||
}
|
||||
|
||||
$IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话
|
||||
if($IsWhat == 1){
|
||||
return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);
|
||||
}else{
|
||||
return preg_replace('/(1[0-9]{1})[0-9]{7}([0-9]{2})/i','$1*******$2',$phone);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 地址中夹带数字加密
|
||||
* 示例:
|
||||
* 北京市124Ff:北京市***Ff
|
||||
* */
|
||||
public static function maskAddressStr($address)
|
||||
{
|
||||
if (empty($address)){
|
||||
return $address;
|
||||
}
|
||||
|
||||
$address = mb_convert_encoding( $address , 'UTF-8', 'auto' );
|
||||
$pattern = '/[0-9]/';
|
||||
if(preg_match_all($pattern, $address, $match)){
|
||||
return str_replace($match[0],'*',$address);
|
||||
}else{
|
||||
return $address;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 身份证掩码
|
||||
* 示例:
|
||||
* 372929199610075411:372929****5411
|
||||
* @param string $card_num
|
||||
* @return string
|
||||
*/
|
||||
public static function maskIdCard(string $card_num): string
|
||||
{
|
||||
if (empty($card_num)){
|
||||
return $card_num;
|
||||
}
|
||||
|
||||
$result = preg_replace('/(\\w{6})(\\w+)(\\w{4})/','$1****$3',$card_num);
|
||||
if (empty($result)){
|
||||
return $card_num;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
|
||||
/**
|
||||
* 正则
|
||||
*/
|
||||
class PcreMatch
|
||||
{
|
||||
private static string $PregIdCard = '/^(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dxX]$/';
|
||||
|
||||
/**
|
||||
* 匹配身份证
|
||||
* @param string $id_number
|
||||
* @return string
|
||||
*/
|
||||
public static function pregIdCard(string $id_number): string
|
||||
{
|
||||
if(empty($id_number))
|
||||
{
|
||||
throw new BusinessException("身份证号为空",HttpEnumCode::HTTP_ERROR);
|
||||
}
|
||||
|
||||
preg_match(self::$PregIdCard, $id_number,$match);
|
||||
|
||||
if(empty($match))
|
||||
{
|
||||
throw new BusinessException("身份证号错误", HttpEnumCode::HTTP_ERROR);
|
||||
}
|
||||
return $match[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配身份证最后一位,修改为大写x
|
||||
* @param string $id_number
|
||||
* @return string
|
||||
*/
|
||||
public static function pregIdCardX(string $id_number): string
|
||||
{
|
||||
if(empty($id_number))
|
||||
{
|
||||
throw new BusinessException("身份证号为空",HttpEnumCode::HTTP_ERROR);
|
||||
}
|
||||
|
||||
return str_replace("x","X",$id_number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配去除oss网址
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function pregRemoveOssWebsite(string $path): string
|
||||
{
|
||||
if (empty($path)){
|
||||
return $path;
|
||||
}
|
||||
|
||||
return str_replace('https://' . config('alibaba.oss.endpoint'),"",$path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Hyperf\Guzzle\CoroutineHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use Hyperf\Redis\Redis;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
/**
|
||||
* 处方平台对接类
|
||||
*/
|
||||
class Prescription
|
||||
{
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
#[Inject]
|
||||
protected Client $client;
|
||||
|
||||
#[Inject]
|
||||
protected Redis $redis;
|
||||
|
||||
protected string $api_url;
|
||||
protected string $client_id;
|
||||
protected string $client_secret;
|
||||
protected string $access_token;
|
||||
protected array $header;
|
||||
public string $version = "v1";
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// $container = ApplicationContext::getContainer();
|
||||
// $this->redis = $container->get(Redis::class);
|
||||
|
||||
// 请求地址
|
||||
$this->api_url = "http://49.233.3.200:6304/api/thridapi/";
|
||||
$this->client_id = "ZD-004";
|
||||
$this->client_secret = "0baa5927164710b9f800bf33546b6da3";
|
||||
|
||||
// 启动时redis已注入,此处不会出现问题。
|
||||
$this->access_token = $this->redis->get("prescription_token");
|
||||
if (empty($access_token)){
|
||||
$this->access_token = $this->getToken();
|
||||
}
|
||||
|
||||
$this->header = [
|
||||
"Authorization" => "Bearer " . $this->access_token
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token接口
|
||||
* @return string token数据
|
||||
*/
|
||||
protected function getToken(): string
|
||||
{
|
||||
$option = [
|
||||
"json" => array(
|
||||
"clientId" => $this->client_id,
|
||||
"clientSecret" => $this->client_secret,
|
||||
)
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->httpRequest($this->api_url . $this->version . '/user_thrid/token', $option);
|
||||
|
||||
if (empty($response['result'])){
|
||||
// 返回值为空
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
|
||||
if (empty($response['result']['token'])){
|
||||
// 返回值为空
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
|
||||
} catch (GuzzleException $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
|
||||
return $response['result']['token'];
|
||||
}
|
||||
|
||||
// 获取药品
|
||||
public function getProd(){
|
||||
$option = [
|
||||
"json" => array(
|
||||
"page" => 1,
|
||||
"pageSize" => 1,
|
||||
),
|
||||
"headers" => $this->header
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->httpRequest($this->api_url . $this->version . '/drug/syncDrugCatalogue', $option);
|
||||
if (empty($response['result'])){
|
||||
// 返回值为空
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
|
||||
if (empty($response['result']['rows'])){
|
||||
return true;
|
||||
}
|
||||
dump($response);
|
||||
// 获取总数
|
||||
// $count
|
||||
} catch (GuzzleException $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
|
||||
// return $response['token'];
|
||||
}
|
||||
|
||||
// 查询商品库存
|
||||
public function getProdStock(){
|
||||
$option = [
|
||||
"json" => array(
|
||||
"pharmacyCode" => "JG-10009",
|
||||
"drugCode" => "105860",
|
||||
),
|
||||
"headers" => $this->header
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->httpRequest($this->api_url . $this->version . '/pharmacy/pharmacyInventory', $option);
|
||||
dump($response);
|
||||
|
||||
} catch (GuzzleException $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 获取运费
|
||||
public function getExpressPrice(){
|
||||
$option = [
|
||||
"json" => array(
|
||||
"pharmacyCode" => "JG-10009",
|
||||
),
|
||||
"headers" => $this->header
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->httpRequest($this->api_url . $this->version . '/pharmacy/transportationExpenses', $option);
|
||||
dump($response);
|
||||
|
||||
} catch (GuzzleException $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求封装
|
||||
* @param string $path
|
||||
* @param array $option
|
||||
* @return array
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
protected function httpRequest(string $path,array $option = []): array
|
||||
{
|
||||
$response = $this->client->post($path, $option);
|
||||
|
||||
if ($response->getStatusCode() != '200'){
|
||||
// 请求失败
|
||||
throw new BusinessException($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
$body = json_decode($response->getBody(),true);
|
||||
if (empty($body)){
|
||||
// 返回值为空
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
dump($body);
|
||||
|
||||
if (empty($body['data'])){
|
||||
// 返回值为空
|
||||
if (!empty($body['message'])){
|
||||
throw new BusinessException($body['message']);
|
||||
}
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
|
||||
return $body['data'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use EasyWeChat\Kernel\Exceptions\BadResponseException;
|
||||
use EasyWeChat\Kernel\Exceptions\DecryptException;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use EasyWeChat\Kernel\HttpClient\AccessTokenAwareClient;
|
||||
use EasyWeChat\MiniApp\Application;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
class WeChat
|
||||
{
|
||||
protected Application $app;
|
||||
|
||||
protected array $config;
|
||||
|
||||
protected AccessTokenAwareClient $client;
|
||||
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = config("easy_we_chat");
|
||||
|
||||
try {
|
||||
// 获取WeChat客户端
|
||||
$this->app = new Application($this->config);
|
||||
|
||||
// 替换缓存
|
||||
$this->app->setCache(ApplicationContext::getContainer()->get(CacheInterface::class));
|
||||
|
||||
$this->client = $this->app->getClient();
|
||||
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new BusinessException('实例化EasyWeChat类失败:' . $e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 jsCode 获取用户 session 信息
|
||||
* @param string $code code
|
||||
* @return array
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function codeToSession(string $code): array
|
||||
{
|
||||
try {
|
||||
$utils = $this->app->getUtils();
|
||||
return $utils->codeToSession($code);
|
||||
} catch (\Exception $e) {
|
||||
throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密微信会话信息
|
||||
* @param string $sessionKey 会话密钥
|
||||
* @param string $iv 加密算法的初始向量
|
||||
* @param string $encryptedData 用户信息的加密数据
|
||||
* @return array
|
||||
*/
|
||||
public function decryptSession(string $sessionKey, string $iv, string $encryptedData): array
|
||||
{
|
||||
try {
|
||||
$utils = $this->app->getUtils();
|
||||
return $utils->decryptSession($sessionKey, $iv, $encryptedData);
|
||||
} catch (\Exception $e) {
|
||||
throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 access_token
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
try {
|
||||
$accessToken = $this->app->getAccessToken();
|
||||
return $accessToken->getToken();
|
||||
} catch (\Exception $e) {
|
||||
throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机号
|
||||
* @param string $code
|
||||
* @return array
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface|BadResponseException
|
||||
*/
|
||||
public function getPhone(string $code): array
|
||||
{
|
||||
$options = [
|
||||
"code" => $code,
|
||||
];
|
||||
$response = $this->client->postJson('wxa/business/getuserphonenumber', $options);
|
||||
if ($response->isFailed()) {
|
||||
// 出错了,处理异常
|
||||
$result = $response->toArray();
|
||||
if(empty($result)){
|
||||
throw new BusinessException( $response->toJson(false),HttpEnumCode::GET_WX_ERROR);
|
||||
}
|
||||
if (isset($result['errcode'])){
|
||||
if ($result['errcode'] == "40029"){
|
||||
// code过期
|
||||
throw new BusinessException( HttpEnumCode::getMessage(HttpEnumCode::GET_WX_ERROR),HttpEnumCode::WX_CODE_ERROR);
|
||||
}
|
||||
}
|
||||
throw new BusinessException( $response->toJson(false),HttpEnumCode::GET_WX_ERROR);
|
||||
}
|
||||
return $response->toArray(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user