Files
huangjingfen/pro_v3.5.1_副本/crmeb/services/HttpService.php

214 lines
6.7 KiB
PHP
Raw Normal View History

feat(fsgx): 完成全部24项开发任务 Phase1-7 Phase1 后端核心: - 新增 fsgx_v1.sql 迁移脚本(is_queue_goods/frozen_points/available_points/no_assess) - SystemConfigServices 返佣设置扩展(周期人数/分档比例/范围/时机) - StoreOrderCreateServices 周期循环佣金计算 - StoreOrderTakeServices 佣金发放后同步冻结积分 - StoreProductServices/StoreProduct 保存 is_queue_goods Phase2 后端接口: - GET /api/hjf/brokerage/progress 佣金周期进度 - GET /api/hjf/assets/overview 资产总览 - HjfPointsServices 每日 frozen_points 0.4‰ 释放定时任务 - PUT /adminapi/hjf/member/{uid}/no_assess 不考核接口 - GET /adminapi/hjf/points/release_log 积分日志接口 Phase3 前端清理: - hjfCustom.js 路由精简(仅保留 points/log) - hjfQueue.js/hjfMember.js API 清理/重定向至 CRMEB 原生接口 - pages.json 公排→推荐佣金/佣金记录/佣金规则 Phase4-5 前端改造: - queue/status.vue 推荐佣金进度页整体重写 - 商品详情/订单确认/支付结果页文案与逻辑改造 - 个人中心/资产页/引导页/规则页文案改造 - HjfQueueProgress/HjfRefundNotice/HjfAssetCard 组件改造 - 推广中心嵌入佣金进度摘要 - hjfMockData.js 全量更新(公排字段→佣金字段) Phase6 Admin 增强: - 用户列表新增 frozen_points/available_points 列及不考核操作按钮 - hjfPoints.js USE_MOCK=false 对接真实积分日志接口 Phase7 配置文档: - docs/fsgx-phase7-config-checklist.md 后台配置与全链路验收清单 Made-with: Cursor
2026-03-23 22:32:19 +08:00
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services;
/**
* Class HttpService
* @package crmeb\services
*/
class HttpService
{
/**
* 错误信息
* @var string|null
*/
private static string|null $curlError;
/**
* header头信息
* @var string|null
*/
private static string|null $headerStr;
/**
* 请求状态
* @var int
*/
private static $status;
/**
* @return string
*/
public static function getCurlError()
{
return self::$curlError;
}
/**
* @return mixed
*/
public static function getStatus()
{
return self::$status;
}
/**
* 模拟GET发起请求
* @param $url
* @param array $data
* @param bool $header
* @param int $timeout
* @return bool|string
*/
public static function getRequest($url, $data = array(), $header = false, $timeout = 10)
{
if (!empty($data)) {
$url .= (stripos($url, '?') === false ? '?' : '&');
$url .= (is_array($data) ? http_build_query($data) : $data);
}
return self::request($url, 'get', array(), $header, $timeout);
}
/**
* Swoole 下对 HTTPS 使用 PHP 流请求,避免未编译 OpenSSL Swoole curl 报错
* @param string $url
* @param string $method
* @param array $data
* @param array|false $header
* @param int $timeout
* @return bool|string
*/
private static function requestByStream(string $url, string $method, array $data, $header, int $timeout): bool|string
{
$method = strtoupper($method);
if ($method === 'GET' && !empty($data)) {
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($data);
}
$opts = [
'http' => [
'method' => $method,
'timeout' => (float) $timeout,
'ignore_errors' => true,
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
];
if ($header !== false && is_array($header)) {
$opts['http']['header'] = implode("\r\n", $header);
}
if ($method === 'POST' && !empty($data)) {
$opts['http']['content'] = is_array($data) ? http_build_query($data) : $data;
}
$ctx = stream_context_create($opts);
$content = @file_get_contents($url, false, $ctx);
if ($content === false) {
self::$status = null;
self::$curlError = 'stream request failed';
return false;
}
$responseHeaders = $http_response_header ?? [];
self::$headerStr = implode("\r\n", $responseHeaders);
$code = 0;
if (!empty($responseHeaders[0]) && preg_match('/\d{3}/', $responseHeaders[0], $m)) {
$code = (int) $m[0];
}
self::$status = ['http_code' => $code, 'header_size' => 0];
return $code === 200 ? $content : false;
}
/**
* curl 请求
* @param $url
* @param string $method
* @param array $data
* @param bool $header
* @param int $timeout
* @return bool|string
*/
public static function request($url, $method = 'get', $data = array(), $header = false, $timeout = 15)
{
self::$status = null;
self::$curlError = null;
self::$headerStr = null;
$method = strtoupper($method);
$isHttps = (strpos($url, 'https://') === 0);
$inSwoole = extension_loaded('swoole') && class_exists(\Swoole\Coroutine::class, false);
// Swoole 未编译 OpenSSL 时curl 无法请求 HTTPS改用 PHP 流
if ($inSwoole && $isHttps) {
return self::requestByStream($url, $method, is_array($data) ? $data : [], $header, $timeout ?: 300);
}
$curl = curl_init($url);
//请求方式
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
//post请求
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
} elseif ($method == 'GET' && count($data)) {
$url .= '?' . http_build_query($data);
curl_setopt($curl, CURLOPT_URL, $url);
}
//超时时间
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
//设置header头
if ($header !== false) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
//返回抓取数据
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//输出header头信息
curl_setopt($curl, CURLOPT_HEADER, true);
//TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键就是允许你查看请求header
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
//https请求
if (1 == strpos("$" . $url, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
self::$curlError = curl_error($curl);
[$content, $status] = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
self::$status = $status;
self::$headerStr = trim(substr($content, 0, $status['header_size']));
$content = trim(substr($content, $status['header_size']));
return (intval($status["http_code"]) === 200) ? $content : false;
}
/**
* 模拟POST发起请求
* @param $url
* @param array $data
* @param bool $header
* @param int $timeout
* @return bool|string
*/
public static function postRequest($url, $data = [], $header = false, $timeout = 10)
{
return self::request($url, 'post', $data, $header, $timeout);
}
/**
* 获取header头字符串类型
* @return mixed
*/
public static function getHeaderStr()
{
return self::$headerStr;
}
/**
* 获取header头数组类型
* @return array
*/
public static function getHeader()
{
$headArr = explode("\r\n", self::$headerStr);
return $headArr;
}
}