diff --git a/app/common/Curl.php b/app/common/Curl.php new file mode 100644 index 00000000..471c7005 --- /dev/null +++ b/app/common/Curl.php @@ -0,0 +1,329 @@ + true, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HEADER => false, + CURLOPT_VERBOSE => true, + CURLOPT_AUTOREFERER => true, + CURLOPT_CONNECTTIMEOUT => 30, + CURLOPT_TIMEOUT => 30, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' + ); + + public static function mergeArray() + { + $args = func_get_args(); + $res = array_shift($args); + while (!empty($args)) { + $next = array_shift($args); + foreach ($next as $k => $v) { + if (is_array($v) && isset($res[$k]) && is_array($res[$k])) { + $res[$k] = self::mergeArray($res[$k], $v); + } elseif (is_numeric($k)) { + isset($res[$k]) ? $res[] = $v : $res[$k] = $v; + } else { + $res[$k] = $v; + } + } + } + return $res; + } + + public function getOptions() + { + $options = self::mergeArray($this->request_options, $this->options, $this->config); + return $options; + } + + public function setOption($key, $value, $default = false) + { + if ($default) { + $this->options[$key] = $value; + } else { + $this->request_options[$key] = $value; + } + + return $this; + } + + /** + * Clears Options + * This will clear only the request specific options. Default options cannot be cleared. + */ + + public function resetOptions() + { + $this->request_options = array(); + return $this; + } + + /** + * Resets the Option to Default option + * @param $key + * @return $this + */ + public function resetOption($key) + { + if (isset($this->request_options[$key])) { + unset($this->request_options[$key]); + } + return $this; + } + + public function setOptions($options, $default = false) + { + if ($default) { + $this->options = $options + $this->request_options; + } else { + $this->request_options = $options + $this->request_options; + } + + return $this; + } + + public function buildUrl($url, $data = array()) + { + $parsed = parse_url($url); + + isset($parsed['query']) ? parse_str($parsed['query'], $parsed['query']) : $parsed['query'] = array(); + + $params = isset($parsed['query']) ? $data + $parsed['query'] : $data; + $parsed['query'] = ($params) ? '?' . http_build_query($params) : ''; + if (!isset($parsed['path'])) { + $parsed['path']='/'; + } + + $parsed['port'] = isset($parsed['port'])?':'.$parsed['port']:''; + return $parsed['scheme'].'://'.$parsed['host'].$parsed['port'].$parsed['path'].$parsed['query']; + } + + public function exec($url, $options, $debug = false) + { + $this->error = null; + $this->header = null; + $this->headerMap = null; + $this->info = null; + $this->status = null; + + $ch = curl_init($url); + curl_setopt_array($ch, $options); + + $output = curl_exec($ch); + + $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (!$output) { + $this->error = curl_error($ch); + $this->info = curl_getinfo($ch); + } elseif ($debug) { + $this->info = curl_getinfo($ch); + } + + if (@$options[CURLOPT_HEADER] == true) { + list($header, $output) = $this->processHeader($output, curl_getinfo($ch, CURLINFO_HEADER_SIZE)); + $this->header = $header; + } + curl_close($ch); + + return $output; + } + + public function processHeader($response, $header_size) + { + return array(substr($response, 0, $header_size), substr($response, $header_size)); + } + + public function get($url, $params = array(), $debug = false) + { + $exec_url = $this->buildUrl($url, $params); + $options = $this->getOptions(); + return $this->exec($exec_url, $options, $debug); + } + + public function post($url, $data, $params = array(), $debug = false, $flag = false) + { + $url = $this->buildUrl($url, $params); +// if ($flag) { +// $this->setOption(CURLOPT_HTTPHEADER, Yii::app()->params['host']); +// } + $options = $this->getOptions(); + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = $data; + + return $this->exec($url, $options, $debug); + } + + public function postJson($url, $data, $params = array(), $debug = false, $flag = false) + { + $url = $this->buildUrl($url, $params); +// if ($flag) { +// $this->setOption(CURLOPT_HTTPHEADER, Yii::app()->params['host']); +// } + $options = $this->getOptions(); + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = $data; + $options[CURLOPT_HEADER] = 0; + $options[CURLOPT_HTTPHEADER] = + array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data)); + + return $this->exec($url, $options, $debug); + } + + public function put($url, $data = null, $params = array(), $debug = false) + { + $url = $this->buildUrl($url, $params); + + $f = fopen('php://temp', 'rw+'); + fwrite($f, $data); + rewind($f); + + $options = $this->getOptions(); + $options[CURLOPT_PUT] = true; + $options[CURLOPT_INFILE] = $f; + $options[CURLOPT_INFILESIZE] = strlen($data); + + return $this->exec($url, $options, $debug); + } + + public function patch($url, $data = array(), $params = array(), $debug = false) + { + $url = $this->buildUrl($url, $params); + + $options = $this->getOptions(); + $options[CURLOPT_CUSTOMREQUEST] = 'PATCH'; + $options[CURLOPT_POSTFIELDS] = $data; + + return $this->exec($url, $options, $debug); + } + + public function delete($url, $params = array(), $debug = false) + { + $url = $this->buildUrl($url, $params); + + $options = $this->getOptions(); + $options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; + + return $this->exec($url, $options, $debug); + } + + /** + * Gets header of the last curl call if header was enabled + */ + public function getHeaders() + { + if (!$this->header) { + return array(); + } + + if (!$this->headerMap) { + $headers = explode("\r\n", trim($this->header)); + $output = array(); + $output['http_status'] = array_shift($headers); + + foreach ($headers as $line) { + $params = explode(':', $line, 2); + + if (!isset($params[1])) { + $output['http_status'] = $params[0]; + } else { + $output[trim($params[0])] = trim($params[1]); + } + } + + $this->headerMap = $output; + } + return $this->headerMap; + } + + public function addHeader($header = array()) + { + $h = isset($this->request_options[CURLOPT_HTTPHEADER]) ? $this->request_options[CURLOPT_HTTPHEADER] : array(); + foreach ($header as $k => $v) { + $h[] = $k . ': ' . $v; + } + + $this->setHeaders($h); + return $this; + } + + public function getHeader($key) + { + $headers = array_change_key_case($this->getHeaders(), CASE_LOWER); + $key = strtolower($key); + + return @$headers[$key]; + } + + public function setHeaders($header = array(), $default = false) + { + if ($this->isAssoc($header)) { + $out = array(); + foreach ($header as $k => $v) { + $out[] = $k .': '.$v; + } + $header = $out; + } + + $this->setOption(CURLOPT_HTTPHEADER, $header, $default); + return $this; + } + + private function isAssoc($arr) + { + return array_keys($arr) !== range(0, count($arr) - 1); + } + + public function getError() + { + return $this->error; + } + + public function getInfo() + { + return $this->info; + } + + public function getStatus() + { + return $this->status; + } + + public function init() + { + return; + } + + public function download($fileUrl, $localFileName) + { + $ch = curl_init($fileUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + curl_close($ch); + $fp = fopen($localFileName, 'a'); + fwrite($fp, $result); + fclose($fp); + + return $localFileName; + } +} diff --git a/app/common/ThinkApi.php b/app/common/ThinkApi.php new file mode 100644 index 00000000..7279fa3d --- /dev/null +++ b/app/common/ThinkApi.php @@ -0,0 +1,25 @@ +appCode; + $data = $curl->$method($this->domain . $route, $param); + $data = json_decode($data, true); + if (empty($data['data'])) { + throw new ValidateException($data['message']); + } + return $data['data']; + } + +} diff --git a/app/controller/admin/ProductLibrary.php b/app/controller/admin/ProductLibrary.php index e04f1ea8..92b9b225 100644 --- a/app/controller/admin/ProductLibrary.php +++ b/app/controller/admin/ProductLibrary.php @@ -2,8 +2,9 @@ namespace app\controller\admin; +use app\common\ThinkApi; use crmeb\basic\BaseController; -use http\Client; +use think\exception\ValidateException; use think\facade\Db; use think\facade\Log; use crmeb\services\UploadService; @@ -100,16 +101,9 @@ class ProductLibrary extends BaseController $codes=$code; } - $appCode = 'b1eb52b9-0379-4c56-b795-47d90a73ca6a'; - $barcode = $codes; - $url = 'https://api.topthink.com/barcode/query?' . http_build_query(['appCode' => $appCode, 'code' => $barcode]); + $thinkApi = new ThinkApi(); + $result = $thinkApi->request('barcode/query', ['code' => $codes]); - $result = $this->requestApi($url); - -// $client = new Client("b1eb52b9-0379-4c56-b795-47d90a73ca6a"); -// $result = $client->barcodeQuery() -// ->withCode($codes) -// ->request(); try { if ($result['code'] == 0) { $upload = UploadService::create(); @@ -218,7 +212,7 @@ class ProductLibrary extends BaseController if (json_last_error() === JSON_ERROR_NONE) { return $responseData; } else { - return app('json')->fail(json_last_error_msg()); + throw new ValidateException(json_last_error_msg()); } } diff --git a/app/controller/api/server/Store.php b/app/controller/api/server/Store.php index 7e92024d..e1032170 100644 --- a/app/controller/api/server/Store.php +++ b/app/controller/api/server/Store.php @@ -105,7 +105,6 @@ class Store extends BaseController $saleTarget = $userCoupon['origin_price']; $purchaseTarget = bcmul($saleTarget, 0.4, 2); $officialPurchaseTarget = bcsub($saleTarget, $purchaseTarget, 2); - $merchant['purchase_amount'] = bcsub($userCoupon['origin_price'], $userCoupon['coupon_price'], 2); $merchant['balance'] = $userCoupon['coupon_price']; $userCouponStatus = $couponDetail['send_status']; $userCouponStatusName = StoreCouponDetail::SEND_STATUS_MAP[$userCouponStatus];