Commit 94d2b42d authored by zhaopeng343's avatar zhaopeng343

Merge branch 'test' of git.huaperfect.com:hwq/micro into develop

# Conflicts:
#	src/Tools/HttpRequestJson.php
parents 62a7bcc0 2d3435c1
......@@ -14,6 +14,7 @@
"ext-redis": "*",
"ext-gd": "*",
"ext-curl": "*",
"ext-mbstring": "*",
"hyperf/cache": "~1.1.0",
"hyperf/framework": "~1.1.0",
"hyperf/db-connection": "~1.1.0",
......
......@@ -162,4 +162,30 @@ interface BaseInfoServiceInterface
*/
public function getReviewUsers($teamId, $authId);
/**
* 通过id数组获取部门数组
* @param array $idList 默认去重
* @param array $relations 关联关系,默认空 ['users', 'leader']
* @param array $columns 要显示的字段,默认全部 ['id', 'name', 'pid', 'remark']
* @return array 默认keyBY('id')
*/
public function getDepartmentListByIdList(array $idList, array $relations = [], array $columns = ['*']): array;
/**
* 获取单个部门数据
* @param int $id
* @param array $relations 关联关系,默认空 ['users', 'leader']
* @param array $columns 要显示的字段,默认全部 ['id', 'name', 'pid', 'remark']
* @return array|null
*/
public function getDepartmentById($id, array $relations = [], array $columns = ['*']);
/**
* 获取所有部门数据
* @param array $relations 关联关系,默认空 ['users', 'leader']
* @param array $columns 要显示的字段,默认全部 ['id', 'name', 'pid', 'remark']
* @return array 默认已keyBy('id')
*/
public function departments(array $relations = [], array $columns = ['*']): array;
}
......@@ -168,6 +168,19 @@ abstract class AbstractShopify
return $this->processResponse($response, $dataKey);
}
/**
* 分页
* @param null $url
* @return ['data' => [数据], 'next_link' => '下一页链接']
* @throws Exception
*/
public function page($url = null)
{
if (!$url) $url = $this->generateUrl();
$response = HttpRequestJson::get($url, $this->httpHeaders);
return $this->processPageResponse($response, $this->pluralizeKey);
}
/**
* 根据id获取一条数据
* @param $id
......@@ -284,4 +297,53 @@ abstract class AbstractShopify
}
}
/**
* 处理响应
* @param array $response
* @param null $dataKey
* @return mixed
* @throws Exception
*/
public function processPageResponse($response, $dataKey = null)
{
[$code, $header, $body] = $response;
$content = json_decode($body, true);
$link = $this->getLink($header);
if (isset($content['errors'])) {
throw new Exception($this->castString($content['errors']), $code);
}
if ($dataKey && isset($content[$dataKey])) {
$data = $content[$dataKey];
} else {
$data = $content;
}
return ['data' => $data, 'next_link' => $link];
}
public function getLink($header, $type = 'next')
{
if (array_key_exists('x-shopify-api-version', $header) && $header['x-shopify-api-version'] < '2019-07') {
return null;
}
if (!empty($header['link'])) {
if (stristr($header['link'], '; rel="' . $type . '"') > -1) {
$headerLinks = explode(',', $header['link']);
foreach ($headerLinks as $headerLink) {
if (stristr($headerLink, '; rel="' . $type . '"') === -1) {
continue;
}
$pattern = '#<(.*?)>; rel="' . $type . '"#m';
preg_match($pattern, $headerLink, $linkResponseHeaders);
if ($linkResponseHeaders) {
return $linkResponseHeaders[1];
}
}
}
}
return null;
}
}
......@@ -8,8 +8,8 @@
namespace Meibuyu\Micro\Tools;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
//use GuzzleHttp\Client;
//use Psr\Http\Message\ResponseInterface;
/**
* json格式请求(非协程)
......@@ -19,54 +19,112 @@ use Psr\Http\Message\ResponseInterface;
class HttpRequestJson
{
protected static function prepareRequest($headers, $data = [])
{
$data = json_encode($data);
if (!empty($data)) {
$headers['Content-type'] = 'application/json';
$headers['Content-Length'] = strlen($data);
}
return [$headers, $data];
}
// /**
// * get请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function get($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->get($url, ['headers' => $httpHeaders]);
// }
//
// /**
// * post请求
// * @param $url
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function post($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * put请求
// * @param $url
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function put($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * delete请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function delete($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->delete($url, ['headers' => $httpHeaders]);
// }
/**
* get请求
* @param $url
* @param array $httpHeaders
* @return ResponseInterface
* @param array $headers
* @return array
*/
public static function get($url, $httpHeaders = [])
public static function get($url, $headers = [])
{
$client = new Client();
return $client->get($url, ['headers' => $httpHeaders]);
return CurlRequest::get($url, $headers);
}
/**
* post请求
* @param $url
* @param $dataArray
* @param array $httpHeaders
* @return ResponseInterface
* @param $data
* @param array $headers
* @return array
*/
public static function post($url, $dataArray, $httpHeaders = [])
public static function post($url, $data, $headers = [])
{
$client = new Client();
return $client->post($url, ['headers' => $httpHeaders, 'json' => $dataArray]);
[$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::post($url, $data, $headers);
}
/**
* put请求
* @param $url
* @param $dataArray
* @param array $httpHeaders
* @return ResponseInterface
* @param $data
* @param array $headers
* @return array
*/
public static function put($url, $dataArray, $httpHeaders = [])
public static function put($url, $data, $headers = [])
{
$client = new Client();
return $client->put($url, ['headers' => $httpHeaders, 'json' => $dataArray]);
[$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::put($url, $data, $headers);
}
/**
* delete请求
* @param $url
* @param array $httpHeaders
* @return ResponseInterface
* @param array $headers
* @return array
*/
public static function delete($url, $httpHeaders = [])
public static function delete($url, $headers = [])
{
$client = new Client();
return $client->delete($url, ['headers' => $httpHeaders]);
return CurlRequest::delete($url, $headers);
}
}
......@@ -754,7 +754,36 @@ if (!function_exists('to_camel_case')) {
}
}
if (!function_exists('mb_trim')) {
/**
* 去除两边带全角空格的字符串
* @param $str
* @return string
* @author zero
*/
function mb_trim($str)
{
mb_regex_encoding('utf-8');
$str = mb_ereg_replace(' ', '', $str);
return trim($str);
}
}
if (!function_exists('str_replace_once')) {
/**
* 用替换字符串替换第一个出现的搜索字符串
* @param mixed $search 搜索字符串
* @param mixed $replace 替换字符串
* @param mixed $subject 被替换字符串
* @return string
* @author zero
*/
function str_replace_first($search, $replace, $subject)
{
if (($position = strpos($subject, $search)) !== false) {
$replaceLen = strlen($search);
$subject = substr_replace($subject, $replace, $position, $replaceLen);
}
return $subject;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment