'/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); } }