提交 6338e136 编写于 作者: 李光春's avatar 李光春

- 优化服务返回的结果

上级 4fa69e69
......@@ -33,6 +33,7 @@ use think\facade;
* @method helper exitContain(string $str, $nee = 3, $del = ',') bool 判断字符串是否包含某个字符
* @method helper len(string $str) int 统计字符串长度
* @method helper trimAll(string $str) string 删除空格
* @method helper replace(string $search, string $replace, string $subject) string 替换字符串
*/
class Strings extends Facade
{
......
......@@ -160,10 +160,22 @@ class Strings
* @param $str
* @return string|string[]
*/
private function trimAll($str): string
public function trimAll($str): string
{
$oldchar = array(" ", " ", "\t", "\n", "\r");
$newchar = array("", "", "", "", "");
return str_replace($oldchar, $newchar, $str);
}
/**
* 替换字符串
* @param string $search
* @param string $replace
* @param string $subject
* @return string|string[]
*/
public function replace(string $search, string $replace, string $subject)
{
return str_replace($search, $replace, $subject);
}
}
......@@ -18,6 +18,7 @@ namespace DtApp\ThinkLibrary\service\Jd;
use DtApp\ThinkLibrary\exception\CurlException;
use DtApp\ThinkLibrary\exception\JdException;
use DtApp\ThinkLibrary\facade\Strings;
use DtApp\ThinkLibrary\Service;
/**
......@@ -336,9 +337,16 @@ class UnionService extends Service
$this->params['sign_method'] = $this->sign_method;
$this->params['param_json'] = json_encode($this->param);
$this->http();
if (is_array($this->output)) return $this->output;
if (is_object($this->output)) $this->output = json_encode($this->output);
return json_decode($this->output, true);
$response = Strings::replace('.', '_', $this->method) . "_response";
if (isset($this->output["$response"]['result'])) {
if (is_array($this->output["$response"]['result'])) return $this->output["$response"]['result'];
if (is_object($this->output["$response"]['result'])) $this->output = json_encode($this->output["$response"]['result']);
return json_decode($this->output["$response"]['result'], true);
} else {
if (is_array($this->output)) return $this->output;
if (is_object($this->output)) $this->output = json_encode($this->output);
return json_decode($this->output, true);
}
}
/**
......
......@@ -18,6 +18,7 @@ namespace DtApp\ThinkLibrary\service\PinDuoDuo;
use DtApp\ThinkLibrary\exception\CurlException;
use DtApp\ThinkLibrary\exception\PinDouDouException;
use DtApp\ThinkLibrary\facade\Strings;
use DtApp\ThinkLibrary\Service;
/**
......@@ -156,10 +157,9 @@ class JinBaoService extends Service
/**
* 获取商品信息 - 多多进宝商品查询
* https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.search
* @param array $data
* @return array|mixed
*/
public function goodsSearch(array $data = [])
public function goodsSearch()
{
$this->type = 'pdd.ddk.goods.search';
return $this;
......@@ -423,9 +423,27 @@ class JinBaoService extends Service
$this->param['data_type'] = $this->data_type;
$this->param['version'] = $this->version;
$this->http();
if (is_array($this->output)) return $this->output;
if (is_object($this->output)) return $this->object2array($this->output);
return json_decode($this->output, true);
if (isset($this->output['error_response'])) {
// 错误
if (is_array($this->output)) return $this->output;
if (is_object($this->output)) return $this->object2array($this->output);
return json_decode($this->output, true);
} else {
// 正常
$response = substr(Strings::replace('.', '_', $this->type), 8) . "_response";
if (is_array($this->output)) {
if (isset($this->output["$response"])) return $this->output["$response"];
return $this->output;
}
if (is_object($this->output)) {
$this->output = $this->object2array($this->output);
if (isset($this->output["$response"])) return $this->output["$response"];
return $this->output;
}
$this->output = json_decode($this->output, true);
if (isset($this->output["$response"])) return $this->output["$response"];
return $this->output;
}
}
/**
......
......@@ -18,6 +18,7 @@ namespace DtApp\ThinkLibrary\service\TaoBao;
use DtApp\ThinkLibrary\exception\CurlException;
use DtApp\ThinkLibrary\exception\TaoBaoException;
use DtApp\ThinkLibrary\facade\Strings;
use DtApp\ThinkLibrary\Service;
/**
......@@ -479,9 +480,23 @@ class TbkService extends Service
$this->param['sign_method'] = $this->sign_method;
$this->param['timestamp'] = date('Y-m-d H:i:s');
$this->http();
if (is_array($this->output)) return $this->output;
if (is_object($this->output)) $this->output = json_encode($this->output);
return json_decode($this->output, true);
if (isset($this->output['error_response'])) {
// 错误
if (is_array($this->output)) return $this->output;
if (is_object($this->output)) $this->output = json_encode($this->output);
return json_decode($this->output, true);
} else {
// 正常
$response = substr(Strings::replace('.', '_', $this->method), 7) . "_response";
if (is_array($this->output)) {
if (isset($this->output["$response"])) return $this->output["$response"];
return $this->output;
};
if (is_object($this->output)) $this->output = json_encode($this->output);
$this->output = json_decode($this->output, true);
if (isset($this->output["$response"])) return $this->output["$response"];
else return $this->output;
}
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册