Przeglądaj źródła

达达快递相关接口(待续)

林琦海 5 lat temu
rodzic
commit
458679a580

+ 7 - 0
app-ghs/controllers/TestController.php

@@ -9,6 +9,7 @@ use bizHd\order\services\OrderService;
 use bizHd\wx\classes\WxOpenClass;
 use bizGhs\express\services\MiniExpressService;
 use bizGhs\product\classes\ProductClass;
+use common\components\dada\dada;
 use common\components\miniUtil;
 use common\components\wxUtil;
 use Yii;
@@ -20,6 +21,12 @@ class TestController extends BaseController
 	public $guestAccessAction = ['inform', 'notice','order-query','add-order'];
 
 
+	//dada 城市code
+	public function actionDadaCityCode()
+    {
+        $res = dada::getCityCode(0);
+        util::success($res);
+    }
 
     //获取已支持的配送公司列表接口
     public function actionGetAllDelivery()

+ 54 - 0
common/components/dada/api/baseApi.php

@@ -0,0 +1,54 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 22:32
+ */
+namespace common\components\dada\api;
+
+class baseApi
+{
+
+    private $url;
+
+    private $businessParams;
+
+    /**
+     * BaseApi constructor.
+     * @param $url
+     * @param $params
+     * 构造
+     */
+    public function __construct($url, $params) {
+        $this->url = $url;
+        $this->businessParams = $params;
+    }
+
+    public function getUrl(){
+        return $this->url;
+    }
+
+    /**
+     * @return mixed
+     * 获取 businessParams
+     */
+    public function getBusinessParams(){
+        return $this->businessParams;
+    }
+
+    /**
+     * @param mixed $url
+     */
+    public function setUrl($url)
+    {
+        $this->url = $url;
+    }
+
+    /**
+     * @param mixed $businessParams
+     */
+    public function setBusinessParams($businessParams)
+    {
+        $this->businessParams = $businessParams;
+    }
+
+}

+ 17 - 0
common/components/dada/api/cityCodeApi.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 22:34
+ */
+
+namespace common\components\dada\api;
+
+
+use common\components\dada\config\urlConfig;
+//code: 0592 (厦门)
+class cityCodeApi extends baseApi
+{
+    public function __construct($params) {
+        parent::__construct(urlConfig::CITY_ORDER_URL, $params);
+    }
+}

+ 187 - 0
common/components/dada/api/orderApi.php

@@ -0,0 +1,187 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 22:34
+ */
+
+namespace common\components\dada\api;
+
+
+use common\components\dada\config\urlConfig;
+
+class orderApi extends baseApi
+{
+
+    public function __construct()
+    {
+
+    }
+
+    /**
+     * @param $params
+     * 新增订单 新增配送单接口
+     */
+    public function addOrder($params)
+    {
+        $this->setUrl(urlConfig::ORDER_ADD_URL);
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 重新发布订单 在调用新增订单后,订单被取消、过期或者投递异常的情况下,调用此接口,可以在达达平台重新发布订单 接口调用URL地址:/api/order/reAddOrder。
+     */
+    public function reAddOrder($params)
+    {
+        $this->setUrl(urlConfig::ORDER_RE_ADD_URL);
+        $this->setBusinessParams($params);
+    }
+
+
+    /**
+     * @param $params
+     * 订单预发布
+     * 1、查询订单运费接口,接口URL地址:/api/order/queryDeliverFee。
+     */
+    public function queryDeliverFee($params)
+    {
+        $this->setUrl(urlConfig::ORDER_DELIVER_FEE);
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 订单预发布
+     * 2、查询运费后发单接口,接口URL地址:/api/order/addAfterQuery。
+     */
+    public function addAfterQuery($params)
+    {
+        $this->setUrl(urlConfig::ORDER_AFTER_QUERY);
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 增加小费
+     * 可以对待接单状态的订单增加小费。需要注意:订单的小费,以最新一次加小费动作的金额为准,故下一次增加小费额必须大于上一次小费额。
+     * 接口调用URL地址:/api/order/addTip。
+     */
+    public function addTip($params)
+    {
+        $this->setUrl(urlConfig::ORDER_ADD_TIP);
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 订单详情查询
+     * 查询订单的相关信息以及骑手的相关信息,具体信息参见参数说明。
+     * 接口调用请求说明,URL地址:/api/order/status/query。
+     */
+    public function queryOrderStatus($params)
+    {
+        $this->setUrl('/api/order/status/query');
+        $this->setBusinessParams($params);
+    }
+
+
+    /**
+     * @param $params
+     * 在订单待接单或待取货情况下,调用此接口可取消订单。注意:接单后1-15分钟内取消订单,运费退回。同时扣除2元作为给配送员的违约金
+     * 接口调用请求说明,URL地址:/api/order/formalCancel
+     */
+    public function formalCancel($params)
+    {
+        $this->setUrl('/api/order/formalCancel');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 获取取消原因
+     * 调用取消订单接口时,需要传递取消原因ID,通过此接口获取订单取消理由列表
+     * 接口调用请求说明,URL地址:/api/order/cancel/reasons。
+     */
+    public function reasonsCancel($params)
+    {
+        $this->setUrl('/api/order/cancel/reasons');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 追加订单
+     * 商户调用该接口将已发布的订单追加给指定的配送员,接口URL地址:/api/order/appoint/exist
+     * 1. 追加的订单必须是该门店发出的处于待接单状态的订单
+     * 2. 只能从符合条件的配送员列表选取配送员进行追加,参考查询追加配送员
+     */
+    public function existAppoint($params)
+    {
+        $this->setUrl('/api/order/appoint/exist');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 取消追加订单
+     * 商户调用该接口取消已发布的追加订单,接口URL地址:/api/order/appoint/cancel
+     * 被取消的追加订单,状态变为待接单,其他配送员可见
+     */
+    public function cancelAppoint($params)
+    {
+        $this->setUrl('/api/order/appoint/cancel');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 查询追加配送员
+     * 商户调用该接口查询可追加订单的配送员列表,接口URL地址:/api/order/appoint/list/transporter
+     * 可追加的配送员需符合以下条件:
+    1. 配送员在1小时内接过此商户的订单,且订单未完成
+    2. 配送员在当前商户接单数小于系统限定的指定商户接单总数
+    3. 配送员在达达平台的接单数量未达上限
+     */
+    public function transporterAppointList($params)
+    {
+        $this->setUrl('/api/order/appoint/list/transporter');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 商家投诉达达
+     * 达达配送员接单后,商家如果对达达服务不满意,均可以使用该接口对达达进行投诉。
+     * 接口调用请求说明,URL地址:/api/complaint/dada
+     */
+    public function dadaComplaint($params)
+    {
+        $this->setUrl('/api/complaint/dada');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 获取商家投诉达达原因
+     * 商家投诉达达,需要传递投诉原因ID,通过此接口获取投诉原因列表
+     * 接口调用请求说明,URL地址:/api/complaint/reasons。
+     */
+    public function reasonsComplaint($params)
+    {
+        $this->setUrl('/api/complaint/reasons');
+        $this->setBusinessParams($params);
+    }
+
+    /**
+     * @param $params
+     * 妥投异常之物品返回完成
+     * 订单妥投异常后,订单状态变为9,骑士将物品进行返还,如果商家确认收到物品后,可以使用该 接口进行确认,订单状态变成10,同时订单终结。
+     * 接口调用请求说明,URL地址:/api/order/confirm/goods。
+     */
+    public function goodsConfirm($params)
+    {
+        $this->setUrl('/api/order/confirm/goods');
+        $this->setBusinessParams($params);
+    }
+
+}

+ 119 - 0
common/components/dada/config/config.php

@@ -0,0 +1,119 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 22:36
+ */
+
+namespace common\components\dada\config;
+
+
+class config
+{
+
+    /**
+     * 达达开发者app_key
+     */
+    private $appKey = '';
+
+    /**
+     * 达达开发者app_secret
+     */
+    private $appSecret = '';
+
+    /**
+     * api版本
+     */
+    private $v = "1.0";
+
+    /**
+     * 数据格式
+     */
+    private $format = "json";
+
+    /**
+     * 商户ID
+     */
+    private $sourceId;
+
+    /**
+     * host
+     */
+    public $host;
+
+    public function __construct(){
+
+    }
+
+    public function getAppKey(){
+        return $this->appKey;
+    }
+
+    public function getAppSecret(){
+        return $this->appSecret;
+    }
+
+    public function getV(){
+        return $this->v;
+    }
+
+    public function getFormat(){
+        return $this->format;
+    }
+
+    public function getSourceId(){
+        return $this->sourceId;
+    }
+
+    public function getHost(){
+        return $this->host;
+    }
+
+    /**
+     * @param mixed $appKey
+     */
+    public function setAppKey($appKey)
+    {
+        $this->appKey = $appKey;
+    }
+
+    /**
+     * @param mixed $appSecret
+     */
+    public function setAppSecret($appSecret)
+    {
+        $this->appSecret = $appSecret;
+    }
+
+    /**
+     * @param mixed $v
+     */
+    public function setV($v)
+    {
+        $this->v = $v;
+    }
+
+    /**
+     * @param mixed $format
+     */
+    public function setFormat($format)
+    {
+        $this->format = $format;
+    }
+
+    /**
+     * @param mixed $sourceId
+     */
+    public function setSourceId($sourceId)
+    {
+        $this->sourceId = $sourceId;
+    }
+
+    /**
+     * @param mixed $host
+     */
+    public function setHost($host)
+    {
+        $this->host = $host;
+    }
+
+}

+ 20 - 0
common/components/dada/config/constant.php

@@ -0,0 +1,20 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 23:09
+ */
+
+namespace common\components\dada\config;
+
+
+class constant
+{
+
+    const FAIL = "fail";
+
+    const SUCCESS = "success";
+
+    const FAIL_MSG = "接口请求超时或失败";
+
+    const FAIL_CODE = -2;
+}

+ 21 - 0
common/components/dada/config/urlConfig.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 22:40
+ */
+
+namespace common\components\dada\config;
+
+
+class urlConfig
+{
+    const ORDER_ADD_URL = "/api/order/addOrder";//新增订单
+    const ORDER_RE_ADD_URL = "/api/order/reAddOrder";//重发订单
+    const ORDER_DELIVER_FEE = '/api/order/queryDeliverFee'; // 预发布: 查询运费后发单接口
+    const ORDER_AFTER_QUERY = '/api/order/addAfterQuery'; // 预发布后 查询订单运费接口
+    const ORDER_ADD_TIP = '/api/order/addTip'; // 增加小费
+
+    const SHOP_ADD_URL = "/api/shop/add";
+    const CITY_ORDER_URL = "/api/cityCode/list"; //城市code
+
+}

+ 72 - 0
common/components/dada/dada.php

@@ -0,0 +1,72 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 21:38
+ */
+
+namespace common\components\dada;
+
+use common\components\dada\api\cityCodeApi;
+use common\components\dada\api\orderApi;
+use common\components\dada\config\config;
+use common\components\dada\protocol\requestClient;
+use common\components\util;
+use Yii;
+//达达快递
+class dada
+{
+
+    //cityCode
+    public static function getCityCode($sourceId)
+    {
+        $config = self::getConfig($sourceId);
+        $api = new cityCodeApi('');
+        $client = new requestClient($config,$api);
+        $resp = $client->makeRequest();
+        return self::handleResp($resp);
+    }
+
+    //新增订单
+    public static function addOrder($sourceId,$shopNo,$orderSn,$receiver)
+    {
+        $config = self::getConfig($sourceId);
+        $api = new orderApi();
+        //body内容
+        $params = [
+            'shop_no'=>$shopNo,
+            'origin_id'=>$orderSn,
+            'city_code'=> '0592',
+        ];
+        $api->addOrder($params);
+        $client = new requestClient($config,$api);
+        $resp = $client->makeRequest();
+        return self::handleResp($resp);
+    }
+
+
+    public static function getConfig($sourceId)
+    {
+        $config = new config();
+        $online = Yii::$app->params['dadaOnline'];
+        $appKey = Yii::$app->params['dadaAppKey'];
+        $appSecret = Yii::$app->params['dadaAppSecret'];
+        if($online){
+            $config->setSourceId($sourceId);
+            $config->setHost('https://newopen.imdada.cn');
+        }else{
+            $config->setSourceId(73753);
+            $config->setHost("http://newopen.qa.imdada.cn");
+        }
+        $config->setAppKey($appKey);
+        $config->setAppSecret($appSecret);
+        return $config;
+    }
+
+    public static function handleResp($resp)
+    {
+        if($resp->getStatus()!='success'){
+            util::fail($resp->getMsg());
+        }
+        return $resp->getResult();
+    }
+}

+ 146 - 0
common/components/dada/model/orderModel.php

@@ -0,0 +1,146 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 23:03
+ */
+
+namespace common\components\dada\model;
+
+
+class orderModel
+{
+
+    public $shop_no;
+
+    public $origin_id;
+
+    public $city_code;
+
+    public $cargo_price;
+
+    public $is_prepay;
+
+    public $receiver_name;
+
+    public $receiver_address;
+
+    public $receiver_lat;
+
+    public $receiver_lng;
+
+    public $receiver_phone;
+
+    public $callback;
+
+
+    public function setShopNo($shopNo)
+    {
+        !empty($shopNo) ? $this->shop_no = $shopNo : trigger_error('shop_no不能为空', E_USER_ERROR);
+    }
+
+    public function getShopNo()
+    {
+        return $this->shop_no;
+    }
+
+    public function setOriginId($originId)
+    {
+        !empty($originId) ? $this->origin_id = $originId : trigger_error('origin_id不能为空', E_USER_ERROR);
+    }
+
+    public function getOriginId()
+    {
+        return $this->origin_id;
+    }
+
+    public function setCityCode($cityCode)
+    {
+        !empty($cityCode) ? $this->city_code = $cityCode : trigger_error('city_code不能为空', E_USER_ERROR);
+    }
+
+    public function getCityCode()
+    {
+        return $this->city_code;
+    }
+
+    public function setCargoPrice($cargoPrice)
+    {
+        isset($cargoPrice) ? $this->cargo_price = $cargoPrice : trigger_error('cargo_price不能为空', E_USER_ERROR);
+    }
+
+    public function getCargoPrice()
+    {
+        return $this->cargo_price;
+    }
+
+    public function setIsPrepay($isPrepay)
+    {
+        isset($isPrepay) ? $this->is_prepay = $isPrepay : trigger_error('is_prepay不能为空', E_USER_ERROR);
+    }
+
+    public function getIsPrepay()
+    {
+        return $this->is_prepay;
+    }
+
+    public function setReceiverName($receiverName)
+    {
+        !empty($receiverName) ? $this->receiver_name = $receiverName : trigger_error('receiver_name不能为空', E_USER_ERROR);
+    }
+
+    public function getReceiverName()
+    {
+        return $this->receiver_name;
+    }
+
+    public function setReceiverAddress($receiverAddress)
+    {
+        !empty($receiverAddress) ? $this->receiver_address = $receiverAddress : trigger_error('receiver_address不能为空', E_USER_ERROR);
+    }
+
+    public function getReceiverAddress()
+    {
+        return $this->receiver_address;
+    }
+
+    public function setReceiverLat($receiverLat)
+    {
+        isset($receiverLat) ? $this->receiver_lat = $receiverLat : trigger_error('receiver_lat不能为空', E_USER_ERROR);
+    }
+
+    public function getReceiverLat()
+    {
+        return $this->receiver_lat;
+    }
+
+    public function setReceiverLng($receiverLng)
+    {
+        isset($receiverLng) ? $this->receiver_lng = $receiverLng : trigger_error('receiver_lng不能为空', E_USER_ERROR);
+    }
+
+    public function getReceiverLng()
+    {
+        return $this->receiver_lng;
+    }
+
+    public function setReceiverPhone($receiverPhone)
+    {
+        !empty($receiverPhone) ? $this->receiver_phone = $receiverPhone : trigger_error('receiver_phone不能为空', E_USER_ERROR);
+    }
+
+    public function getReceiverPhone()
+    {
+        return $this->receiver_phone;
+    }
+
+    public function setCallback($callback)
+    {
+        !empty($callback) ? $this->callback = $callback : trigger_error('callback不能为空', E_USER_ERROR);
+    }
+
+    public function getCallback()
+    {
+        return $this->callback;
+    }
+
+}

+ 152 - 0
common/components/dada/protocol/requestClient.php

@@ -0,0 +1,152 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 23:07
+ */
+
+namespace common\components\dada\protocol;
+
+
+use common\components\dada\config\constant;
+
+class requestClient
+{
+
+    /**
+     * http request timeout;
+     */
+    private $httpTimeout = 5;
+
+    /**
+     * 配置项
+     */
+    private $config;
+
+    /**
+     * 接口类
+     */
+    private $api;
+
+    /**
+     * 构造函数
+     */
+    public function __construct($config, $api){
+        $this->config   = $config;
+        $this->api      = $api;
+    }
+
+    /**
+     * 请求调用api
+     * @return bool
+     */
+    public function makeRequest(){
+        $reqParams = $this->buildRequestParams();
+        $resp = $this->getHttpRequestWithPost(json_encode($reqParams));
+        return $this->parseResponseData($resp);
+    }
+
+    /**
+     * 构造请求数据
+     * data:业务参数,json字符串
+     */
+    public function buildRequestParams(){
+        $config = $this->getConfig();
+        $api    = $this->getApi();
+
+        $requestParams = array();
+        $requestParams['app_key']   = $config->getAppKey();
+        $requestParams['body']      = $api->getBusinessParams();
+        $requestParams['format']    = $config->getFormat();
+        $requestParams['v']         = $config->getV();
+        $requestParams['source_id'] = $config->getSourceId();
+        $requestParams['timestamp'] = time();
+        $requestParams['signature'] = $this->_sign($requestParams);
+        return $requestParams;
+    }
+
+    /**
+     * 签名生成signature
+     */
+    public function _sign($data){
+
+        $config = $this->getConfig();
+
+        //1.升序排序
+        ksort($data);
+
+        //2.字符串拼接
+        $args = "";
+        foreach ($data as $key => $value) {
+            $args.=$key.$value;
+        }
+        $args = $config->getAppSecret() . $args . $config->getAppSecret();
+        //3.MD5签名,转为大写
+        $sign = strtoupper(md5($args));
+
+        return $sign;
+    }
+
+
+    /**
+     * 发送请求,POST
+     * @param $url 指定URL完整路径地址
+     * @param $data 请求的数据
+     */
+    public function getHttpRequestWithPost($data){
+
+        $config = $this->config;
+        $api = $this->api;
+        $url = $config->getHost() . $api->getUrl();
+
+        // json
+        $headers = array(
+            'Content-Type: application/json',
+        );
+        $curl = curl_init($url);
+        curl_setopt($curl, CURLOPT_URL, $url);
+        curl_setopt($curl, CURLOPT_HEADER, false);
+        curl_setopt($curl, CURLOPT_POST, true);
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
+        curl_setopt($curl, CURLOPT_TIMEOUT, 3);
+        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
+        $resp = curl_exec($curl);
+        //var_dump( curl_error($curl) );//如果在执行curl的过程中出现异常,可以打开此开关查看异常内容。
+        $info = curl_getinfo($curl);
+        curl_close($curl);
+
+        if (isset($info['http_code']) && $info['http_code'] == 200) {
+            return $resp;
+        }
+        return '';
+    }
+
+    /**
+     * 解析响应数据
+     * @param $arr返回的数据
+     * 响应数据格式:{"status":"success","result":{},"code":0,"msg":"成功"}
+     */
+    public function parseResponseData($arr){
+        $resp = new response();
+        if (empty($arr)) {
+            $resp->setStatus(constant::FAIL);
+            $resp->setMsg(constant::FAIL_MSG);
+            $resp->setCode(constant::FAIL_CODE);
+        }else{
+            $data = json_decode($arr, true);
+            $resp->setStatus($data['status']);
+            $resp->setMsg($data['msg']);
+            $resp->setCode($data['code']);
+            $resp->setResult(isset($data['result'])?$data['result']:null);
+        }
+        return $resp;
+    }
+
+    public function getConfig(){
+        return $this->config;
+    }
+
+    public function getApi(){
+        return $this->api;
+    }
+}

+ 77 - 0
common/components/dada/protocol/response.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/25 23:07
+ */
+
+namespace common\components\dada\protocol;
+
+
+class response
+{
+
+    /**
+     * 请求响应返回的数据状态
+     */
+    public $status;
+
+    /**
+     * 请求响应返回的code
+     */
+    public $code;
+
+    /**
+     * 请求响应返回的信息
+     */
+    public $msg;
+
+    /**
+     * 请求响应返回的结果
+     */
+    public $result;
+
+
+    /**
+     * 获取返回code
+     */
+    public function getCode(){
+        return $this->code;
+    }
+
+    public function setCode($code){
+        $this->code = $code;
+    }
+
+    /**
+     * 获取返回status
+     */
+    public function getStatus(){
+        return $this->status;
+    }
+
+    public function setStatus($status){
+        $this->status = $status;
+    }
+
+    /**
+     * 获取返回msg
+     */
+    public function getMsg(){
+        return $this->msg;
+    }
+
+    public function setMsg($msg){
+        $this->msg = $msg;
+    }
+
+    /**
+     * 获取返回result
+     */
+    public function getResult(){
+        return $this->result;
+    }
+
+    public function setResult($result){
+        $this->result = $result;
+    }
+}

+ 4 - 0
common/config/params.php

@@ -45,6 +45,10 @@ $config = [
     'accessKeySecret'=>'m5q7aU3wGcoid9wr0yFJ4JqzHMKfks',
     'endpoint'=>'oss-cn-hangzhou.aliyuncs.com',
 
+    //达达相关配置
+    'dadaOnline'=>false, //正式环境改为true
+    'dadaAppKey'=>'dadae6c9d6064e69cf4',
+    'dadaAppSecret'=>'8ed3b159f64021003aa73f7233118fe6',
 ];
 
 $prefixHttp = 'http://';