| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?php
- namespace addons\QiJuhe\common\api;
- use Yii;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class Api extends BaseObject
- {
- const API_RUL = "https://zhongtai.qijianshigw.com/supplyapi/";
- public $app_host;
- public $app_key;
- public $app_secret;
- public $mall_id;
- public $supply_id;
- public $token;
- public $error;
- public $api_list = [
- 'getToken' => '/app/application/getToken',
- 'getSupplySource' => '/app/application/getSupplySource',
- 'getCategory' => '/app/category/getCategory',
- 'getAllCategory' => '/app/category/tree',
- 'storageList' => '/app/product/storage/list',
- 'deleteStorage' => '/app/product/storage/deleteStorage',
- 'addStorage' => '/app/product/storage/addStorage',
- 'getGoodsInfo' => '/app/product/storage/detailList',
- 'goodsInfo' => '/app/product/storage/detailList',
- 'availableCheck' => '/app/order/availableCheck',
- 'createOreder' => '/app/order',
- 'getOrderInfo' => '/app/product/storage/detailList',
- 'orderLogistic' => '/app/order/Logistic',
- 'orderBeforeCheck' => '/app/order/beforeCheck',
- 'orderReceive' => '/app/order/receive',
- 'getAfterSales' => '/app/afterSales/get', // 通过售后id获取售后详情
- 'orderAfterSalesCreate' => '/app/afterSales/orderAfterSalesCreate', // 通过订单id/采购端订单号 发起整个订单的售后
- 'afterSalesReason' => '/app/afterSales/reason/list', // 获取售后原因
- 'findShopAddressByOrderId' => '/app/supplier/findShopAddressByOrderId', // 通过售后id获取默认商家售后退货地址
- 'afterSalesSend' => '/app/afterSales/send', //
- 'afterSalesList' => '/app/afterSales/list', // 获取售后列表
- 'afterSalesClose' => '/app/afterSales/close', // 关闭售后
- 'getSuppliers' => '/app/supplier/getSuppliers', // 获取供应商
- 'afterSalesCreate' => '/app/afterSales/create',
- 'orderDetail' => '/app/order/orderDetail', // 通过订单id/订单编号订单详情
- 'getAfterSalesTypeNameMap' => '/app/afterSales/getAfterSalesTypeNameMap', // 获取订单支持的售后方式
- 'refundUserReceive' => '/app/afterSales/userReceive', // 售后用户收货
- 'getAfterSalesByOrderItemId'=> '/app/afterSales/getAfterSalesByOrderItemId', // 通过子订单id获取售后详情
- 'orderDetailByThirdOrderSn' => '/app/order/orderDetailByThirdOrderSn', // 通过子订单id获取售后详情
- 'getGatherSupplyAndSource' => '/app/application/getGatherSupplyAndSource',
- ];
- /**
- * 初始
- *
- * @param array $config
- */
- public function __construct($config = [])
- {
- if (empty($config['app_key']) && empty($this->app_key)) {
- if (empty($config['mall_id'])) {
- throw new \Exception('请至少配置mall—id');
- }
- }
- parent::__construct($config);
- }
- public function getGatherSupplyAndSource()
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action);
- }
-
- /**
- * 获取token
- * @param $param
- * @param $method
- *
- * @return array|bool
- */
- public function getToken($param = [], $method = 'post')
- {
- $action = __FUNCTION__; //请求资源名
- $param['app_key'] = $this->app_key;
- $param['app_secret'] = $this->app_secret;
- return $this->request($action, $param, $method);
- }
- /**
- * 可用中台
- *
- * @return boolean|array
- */
- public function getSupplySource()
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action);
- }
- /**
- * 获取售后原因
- *
- * @param integer $after_sale_type
- * @param integer $is_received
- * @return boolean|array
- */
- public function afterSalesReason($after_sale_type, $is_received)
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, compact('after_sale_type', 'is_received'));
- }
- /**
- * 获取供应商列表
- *
- * @return boolean|array
- */
- public function getSuppliers()
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action);
- }
- /**
- * 取消售后
- *
- * @param integer $id
- * @return boolean|array
- */
- public function afterSalesClose($id)
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, compact('id'), 'post');
- }
- /**
- * 获取售后列表
- *
- * @param string $third_order_sn
- * @return array
- */
- public function afterSalesList($third_order_sn)
- {
- $params['pageSize'] = 100;
- $params['page'] = 1;
- // $params['status'] = 1;
- $params['third_order_sn'] = $third_order_sn;
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, $params);
- }
- /**
- * 获取全部分类
- *
- * @return boolean|array
- */
- public function getAllCategory()
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action);
- }
- /**
- * 获取分类
- *
- * @param integer $parent_id
- * @return boolean|array
- */
- public function getCategory($parent_id = 0)
- {
- $action = __FUNCTION__; //请求资源名
- $param = $parent_id > 0 ? ['parent_id' => $parent_id] : [];
- return $this->request($action, $param);
- }
- /**
- * 通过订单id/采购端订单号 发起整个订单的售后
- *
- * @param array $params
- * [
- * order_id => 售后订单id 订单id与采购端订单号两个必传一个
- * third_order_sn => 采购端订单号 订单id与采购端订单号两个必传一个
- * reason_type => 售后原因 售后原因(售后原因API返回)
- * reason => 售后原因是 其他的的时候填写 否
- * description => 描述 否
- * is_received => 是否收到货 1是0否 是
- * detail_images => 图片凭证 否
- * refund_type => 退款还是退货退款 0退款 1退货退款
- * ]
- * @see https://www.yunzmall.com/plugins/shop_server/knowledge/knowledge_detail/919?base_id=14&i=10
- * @return boolean|array
- */
- public function orderAfterSalesCreate($params, $method = 'post')
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, $params, $method);
- }
- /**
- * 子订单申请售后
- *
- * @param array $params
- * @param string $method
- * @return boolean|array
- * @see https://www.yunzmall.com/plugins/shop_server/knowledge/knowledge_detail/910?base_id=14&i=10
- */
- public function afterSalesCreate(array $params, string $method = 'post')
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, $params, $method);
- }
- /**
- * 通过售后id获取售后详情
- *
- * @param integer $id 售后id
- * @return boolean|array
- */
- public function getAfterSales($id)
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, compact('id'));
- }
- /**
- * 通过订单id/采购端订单号获取默认售后收货地址
- *
- * @param array $params
- * @param string $method
- * @return array
- * @see https://www.yunzmall.com/plugins/shop_server/knowledge/knowledge_detail/916?base_id=14&i=10
- */
- public function findShopAddressByOrderId($after_sales_id)
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, compact('after_sales_id'));
- }
- /**
- * 售后发货
- *
- * @param array $params
- * @param string $method
- * @return array
- */
- public function afterSalesSend($params, $method = 'post')
- {
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, $params, $method);
- }
- /**
- * 平台列表[淘宝、jd、]
- *
- * @param [type] $param
- * @return array
- */
- public function storageList($param)
- {
- $action = __FUNCTION__; //请求资源名
- $data = $this->request($action, $param, 'post');
- if (empty($data['list'])) {
- $data['list'] = [];
- $data['page_count'] = 0;
- $data['currentPage'] = 0;
- return $data;
- } else {
- $total = $data['total'];
- $data['page_count'] = ceil($total / $param['pageSize']);
- $data['currentPage'] = $param['page'] + 1;
- return $data;
- }
- }
- /**
- * 获取商品详情
- *
- * @param array $goods_id
- * @return array
- */
- public function goodsInfo(array $goods_id)
- {
- $action = __FUNCTION__; //请求资源名
- $ids = [];
- foreach ($goods_id as $id) {
- $ids[] = intval($id);
- }
- $data = $this->request($action, ['ids' => $ids], 'post');
- return $data['list'] ?? [];
- }
- /**
- * 获取商品详情属性
- *
- * @param integer $goods_id
- * @return array
- */
- public function getGoodsInfo($ids)
- {
- $ids = is_array($ids) ? $ids : [$ids];
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, compact('ids'), 'post');
- }
- /**
- * 添加选品库
- *
- * @param array $ids
- * @return boolean|array
- */
- public function addStorage($ids)
- {
- $ids = is_array($ids) ? $ids : [$ids];
- $data = [];
- foreach ($ids as $id) {
- $data[] = intval($id);
- }
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, ['ids' => $data], 'post');
- }
- /**
- * 删除选品库
- *
- * @param array $ids
- * @return boolean|array
- */
- public function deleteStorage($ids)
- {
- $ids = is_array($ids) ? $ids : [$ids];
- $data = [];
- foreach ($ids as $id) {
- $data[] = intval($id);
- }
- $action = __FUNCTION__; //请求资源名
- return $this->request($action, ['ids' => $data], 'post');
- }
- /**
- * 可售检测
- *
- * @param array $data
- * @return boolean|array
- */
- public function availableCheck($data)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, $data, 'post');
- }
- /**
- * 订单售前检测
- *
- * @param array $data
- * @return boolean|array
- */
- public function orderBeforeCheck($data)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, $data, 'post');
- }
- /**
- * 下单
- *
- * @param array $data
- * @return boolean|array
- */
- public function createOreder($data)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, $data, 'post');
- }
- /**
- * 订单物流查询
- *
- * @param string $order_sn
- * @return boolean|array
- */
- public function orderLogistic($order_sn)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, ['order_sn' => $order_sn], 'post');
- }
- public function orderReceive($id)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, ['order_id' => $id], 'post');
- }
- /**
- * 获取订单详情
- *
- * @param array $ids
- * @return void
- */
- public function getOrderInfo($ids)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, compact($ids), 'post');
- }
- /**
- * 获取订单详情
- *
- * @param int $id
- * @return array
- */
- public function orderDetail(int $id)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, compact('id'));
- }
- public function orderDetailByThirdOrderSn($order_no)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, ['third_order_sn' => $order_no]);
- }
- public function getAfterSalesTypeNameMap(int $order_id = 0, int $order_item_id = 0)
- {
- $action = __FUNCTION__; // 请求资源名
- $id = $order_id;
- $key = 'order_id';
- if (empty($id)) {
- $id = $order_item_id;
- $key = 'order_item_id';
- }
- if (empty($id)) throw new \Exception("中台订单id或中台子订单id必须选择一个");
- return $this->request($action, compact($key));
- }
- /**
- * @param int $id 中台售后id
- * @return array|bool
- */
- public function refundUserReceive(int $id)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, compact('id'), 'post');
- }
- /**
- * @param int $order_item_id
- * @return array|bool
- */
- public function getAfterSalesByOrderItemId(int $order_item_id)
- {
- $action = __FUNCTION__; // 请求资源名
- return $this->request($action, compact('order_item_id'));
- }
- /**
- * 请求
- *
- * @param string $method
- * @param string $action
- * @param array $param
- * @param boolean $is_data
- * @return boolean|array
- */
- private function request($action, $param = [], $method = 'get', $is_data = true)
- {
- $action = $this->api_list[$action] ?? '';
- if (empty($action)) {
- $this->error = "$action 对应API接口不存在";
- return false;
- }
- try {
- $host = $this->app_host ?: self::API_RUL;
- $result = $this->curl_request($host . $action, $param, $method);
- if (!isset($result['code']) || $result['code'] != '0') {
- $msg = $result['msg'] ?? '未知错误' . __LINE__;
- if (isset($result['data']['messages'][0])) {
- $msg = $result['data']['messages'][0];
- }
- $this->error = $msg;
- throw new \Exception($msg);
- }
- if ($is_data && !isset($result['data'])) {
- $this->error = "请求{$action}接口失败:未返回data数据";
- return false;
- }
- return $is_data ? $result['data'] : $result;
- } catch (\Exception $e) {
- \Yii::error($e->getMessage());
- $this->error = $e->getMessage();
- }
- return false;
- }
- /**
- * 发送curl请求
- *
- * @param string $url
- * @param array $data
- * @param string $method
- * @return array
- */
- private function curl_request($url, $data = [], $method = 'get')
- {
- if ($method == 'get' && !empty($data)) {
- $str = http_build_query($data);
- $url = strpos($url, '?') === false ? $url . '?' . $str : $url . '&' . $str;
- }
- $headers = [
- "Content-type: application/json",
- "charset=utf-8",
- "Accept: application/json",
- ];
- if (!empty($this->token)) {
- $headers[] = "x-token: " . $this->token;
- }
-
- $curl = curl_init();
- $timeout = 5;
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 添加超时无响应
- if ($method == 'post') {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, Json::encode($data));
- }
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- $curl_data = curl_exec($curl);
- if (curl_error($curl)) {
- $error = '错误:' . $url . '=>' . curl_error($curl);
- throw new \Exception($error);
- }
- \Yii::$app->custom->logs(sprintf('请求地址: %s', $url));
- \Yii::$app->custom->logs(sprintf('请求信息: %s', json_encode($data)));
- \Yii::$app->custom->logs(sprintf('响应信息: %s', json_encode($curl_data)));
- return Json::decode($curl_data);
- }
- }
|