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 @@ ...@@ -14,6 +14,7 @@
"ext-redis": "*", "ext-redis": "*",
"ext-gd": "*", "ext-gd": "*",
"ext-curl": "*", "ext-curl": "*",
"ext-mbstring": "*",
"hyperf/cache": "~1.1.0", "hyperf/cache": "~1.1.0",
"hyperf/framework": "~1.1.0", "hyperf/framework": "~1.1.0",
"hyperf/db-connection": "~1.1.0", "hyperf/db-connection": "~1.1.0",
......
...@@ -162,4 +162,30 @@ interface BaseInfoServiceInterface ...@@ -162,4 +162,30 @@ interface BaseInfoServiceInterface
*/ */
public function getReviewUsers($teamId, $authId); 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 ...@@ -168,6 +168,19 @@ abstract class AbstractShopify
return $this->processResponse($response, $dataKey); 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获取一条数据 * 根据id获取一条数据
* @param $id * @param $id
...@@ -284,4 +297,53 @@ abstract class AbstractShopify ...@@ -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 @@ ...@@ -8,8 +8,8 @@
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Tools;
use GuzzleHttp\Client; //use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface; //use Psr\Http\Message\ResponseInterface;
/** /**
* json格式请求(非协程) * json格式请求(非协程)
...@@ -19,54 +19,112 @@ use Psr\Http\Message\ResponseInterface; ...@@ -19,54 +19,112 @@ use Psr\Http\Message\ResponseInterface;
class HttpRequestJson 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请求 * get请求
* @param $url * @param $url
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function get($url, $httpHeaders = []) public static function get($url, $headers = [])
{ {
$client = new Client(); return CurlRequest::get($url, $headers);
return $client->get($url, ['headers' => $httpHeaders]);
} }
/** /**
* post请求 * post请求
* @param $url * @param $url
* @param $dataArray * @param $data
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function post($url, $dataArray, $httpHeaders = []) public static function post($url, $data, $headers = [])
{ {
$client = new Client(); [$headers, $data] = self::prepareRequest($headers, $data);
return $client->post($url, ['headers' => $httpHeaders, 'json' => $dataArray]); return CurlRequest::post($url, $data, $headers);
} }
/** /**
* put请求 * put请求
* @param $url * @param $url
* @param $dataArray * @param $data
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function put($url, $dataArray, $httpHeaders = []) public static function put($url, $data, $headers = [])
{ {
$client = new Client(); [$headers, $data] = self::prepareRequest($headers, $data);
return $client->put($url, ['headers' => $httpHeaders, 'json' => $dataArray]); return CurlRequest::put($url, $data, $headers);
} }
/** /**
* delete请求 * delete请求
* @param $url * @param $url
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function delete($url, $httpHeaders = []) public static function delete($url, $headers = [])
{ {
$client = new Client(); return CurlRequest::delete($url, $headers);
return $client->delete($url, ['headers' => $httpHeaders]);
} }
} }
...@@ -754,7 +754,36 @@ if (!function_exists('to_camel_case')) { ...@@ -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