shish 6 年之前
父節點
當前提交
117a080b60

+ 68 - 0
vendor/dada/README.md

@@ -0,0 +1,68 @@
+# dada_openapi_php
+达达开放平台接口php版demo ***[查看接口文档](http://newopen.imdada.cn/#/development/file/index?_k=isjh9o)***
+
+## 使用说明(以新增订单为例)
+### 1.配置相关信息(config文件)
+<br>
+
+```php
+/**
+ * 达达开发者app_key
+ */
+public $app_key = '';
+
+/**
+ * 达达开发者app_secret
+ */
+public $app_secret = '';
+
+```
+### 2.初始化一个配置文件
+
+```php
+
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/addOrderApi.php';
+require_once BASE_DIR . 'client/dadaRequestClient.php';
+require_once BASE_DIR . 'client/dadaResponse.php';
+require_once BASE_DIR . 'config/config.php';
+require_once BASE_DIR . 'model/orderModel.php';
+
+// isOnline 判断是否是测试环境,会有不同的域名等
+$isOnline = false
+$sourceId = 1000000
+// 初始化一个config
+$config = new Config($sourceId, $isOnline);
+```
+
+### 3.domain文件新建一个model,并初始化
+
+```php
+$orderModel = new OrderModel();
+$orderModel->setShopNo('11047059');
+$orderModel->setOriginId('2018091100000002');
+$orderModel->setCityCode('021');
+$orderModel->setCargoPrice(10);
+$orderModel->setIsPrepay(0);
+$orderModel->setReceiverName('测试达达');
+$orderModel->setReceiverAddress('上海市崇明岛');
+$orderModel->setReceiverLat(31.63);
+$orderModel->setReceiverLng(121.41);
+$orderModel->setReceiverPhone('18588888888');
+$orderModel->setCallback('');
+```
+
+### 4.新建一个api,并初始化
+
+```php
+// api主要有2个参数,一个是url, 一个是业务参数
+$addOrderApi = new AddOrderApi(json_encode($orderModel));
+```
+
+### 5.初始化客户端,并调用rpc
+
+```php
+$dada_client = new DadaRequestClient($config, $addOrderApi);
+$resp = $dada_client->makeRequest();
+echo json_encode($resp);
+```

+ 37 - 0
vendor/dada/addOrderExample.php

@@ -0,0 +1,37 @@
+<?php
+header("Content-Type: text/html;charset=utf-8");
+
+//参考文档 http://newopen.imdada.cn/#/development/file/add?_k=ff7mls
+
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/addOrderApi.php';
+require_once BASE_DIR . 'client/dadaRequestClient.php';
+require_once BASE_DIR . 'client/dadaResponse.php';
+require_once BASE_DIR . 'config/config.php';
+require_once BASE_DIR . 'model/orderModel.php';
+
+
+//*********************1.配置项*************************
+$config = new Config(0, false);
+
+//*********************2.实例化一个model*************************
+$orderModel = new OrderModel();
+$orderModel->setShopNo('xxxxxxxxxxxxorigin_shop_no');	// 第三方门店编号
+$orderModel->setOriginId('xxxxxxxxxxxxxxxxxx');			// 第三方订单号
+$orderModel->setCityCode('xxxxx');						// 城市code(可以参照城市code接口)
+$orderModel->setCargoPrice(10);
+$orderModel->setIsPrepay(0);
+$orderModel->setReceiverName('xxxxxxxxxxxxxxxxxx');
+$orderModel->setReceiverAddress('xxxxxxxxxxxxxxx');
+$orderModel->setReceiverLat(0);
+$orderModel->setReceiverLng(0);
+$orderModel->setReceiverPhone('xxxxxxxxxxxxxxxxxx');
+$orderModel->setCallback('');							// 回调url, 每次订单状态变更会通知该url(参照回调接口)
+
+//*********************3.实例化一个api*************************
+$addOrderApi = new AddOrderApi(json_encode($orderModel));
+
+//***********************4.实例化客户端请求************************
+$dada_client = new DadaRequestClient($config, $addOrderApi);
+$resp = $dada_client->makeRequest();
+echo json_encode($resp);

+ 35 - 0
vendor/dada/addShopExample.php

@@ -0,0 +1,35 @@
+<?php
+header("Content-Type: text/html;charset=utf-8");
+//参考文档 http://newopen.imdada.cn/#/development/file/shopAdd?_k=zumioz
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/shopAddApi.php';
+require_once BASE_DIR . 'client/dadaRequestClient.php';
+require_once BASE_DIR . 'client/dadaResponse.php';
+require_once BASE_DIR . 'config/config.php';
+require_once BASE_DIR . 'model/shopAddModel.php';
+
+
+//*********************1.配置项*************************
+$config = new Config(0, false);
+
+//*********************2.实例化一个model*************************
+$shopModel = new ShopAddModel();
+$shopModel->setStationName('xxxxxxxxxxxxxx');		// 门店名称
+$shopModel->setBusiness(1);
+$shopModel->setCityName('xxxxxxxxxx');				// 根据实际情况填写
+$shopModel->setAreaName('xxxxxxxxxx');
+$shopModel->setStationAddress("xxxxxxxxxxxx");
+$shopModel->setLat(0);
+$shopModel->setLng(0);
+$shopModel->setContactName('xxxxxxxxxxxxxx');
+$shopModel->setPhone('xxxxxxxxxxxx');
+$shopModel->setOriginShopId('xxxxxxxxxx');			// 第三方门店编号,发单时候使用
+// 批量接口
+$shopList = array($shopModel);
+//*********************3.实例化一个api*************************
+$shopAddApi = new AddShopApi(json_encode($shopList));
+
+//***********************4.实例化客户端请求************************
+$dada_client = new DadaRequestClient($config, $shopAddApi);
+$resp = $dada_client->makeRequest();
+echo json_encode($resp);

+ 14 - 0
vendor/dada/api/addOrderApi.php

@@ -0,0 +1,14 @@
+<?php
+/**
+ * 发单api
+ */
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/baseApi.php';
+require_once BASE_DIR . 'config/urlConfig.php';
+
+class AddOrderApi extends BaseApi{
+    
+    public function __construct($params) {
+        parent::__construct(UrlConfig::ORDER_ADD_URL, $params);
+    }
+}

+ 23 - 0
vendor/dada/api/baseApi.php

@@ -0,0 +1,23 @@
+<?php
+/**
+ * base api
+ */
+class BaseApi{
+    
+    private $url;
+    
+    private $businessParams;
+
+    public function __construct($url, $params) {
+        $this->url = $url;
+        $this->businessParams = $params;
+    }
+
+    public function getUrl(){
+        return $this->url;
+    }
+
+    public function getBusinessParams(){
+        return $this->businessParams;
+    }
+}

+ 14 - 0
vendor/dada/api/cityCodeApi.php

@@ -0,0 +1,14 @@
+<?php
+/**
+ * 发单api
+ */
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/baseApi.php';
+require_once BASE_DIR . 'config/urlConfig.php';
+
+class CityCodeApi extends BaseApi{
+    
+    public function __construct($params) {
+        parent::__construct(UrlConfig::CITY_ORDER_URL, $params);
+    }
+}

+ 14 - 0
vendor/dada/api/shopAddApi.php

@@ -0,0 +1,14 @@
+<?php
+/**
+ * 添加门店api
+ */
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/baseApi.php';
+require_once BASE_DIR . 'config/urlConfig.php';
+
+class AddShopApi extends BaseApi{
+    
+    public function __construct($params) {
+        parent::__construct(UrlConfig::SHOP_ADD_URL, $params);
+    }
+}

+ 26 - 0
vendor/dada/cityCodeExample.php

@@ -0,0 +1,26 @@
+<?php
+header("Content-Type: text/html;charset=utf-8");
+
+//参考文档 http://newopen.imdada.cn/#/development/file/cityList?_k=qbcp8l
+
+define("BASE_DIR", dirname(__FILE__) . "/");
+require_once BASE_DIR . 'api/cityCodeApi.php';
+require_once BASE_DIR . 'client/dadaRequestClient.php';
+require_once BASE_DIR . 'client/dadaResponse.php';
+require_once BASE_DIR . 'config/config.php';
+
+
+//*********************1.配置项*************************
+$config = new Config(0, false);
+
+//*********************2.实例化一个model*************************
+// city_code 业务参数为""
+$cityCodeModel = "";
+
+//*********************3.实例化一个api*************************
+$cityCodeApi = new CityCodeApi(cityCodeModel);
+
+//***********************4.实例化客户端请求************************
+$dada_client = new DadaRequestClient($config, $cityCodeApi);
+$resp = $dada_client->makeRequest();
+echo json_encode($resp);

+ 148 - 0
vendor/dada/client/dadaRequestClient.php

@@ -0,0 +1,148 @@
+<?php
+/**
+ * 达达接口请求客户端
+ */
+define("BASE_DIR", dirname(__FILE__) . "/");
+
+require_once BASE_DIR . 'client/dadaResponse.php';
+require_once BASE_DIR . 'config/constant.php';
+
+class DadaRequestClient{
+    
+    /**
+     * 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->bulidRequestParams();
+        $resp = $this->getHttpRequestWithPost(json_encode($reqParams));
+        return $this->parseResponseData($resp);
+    }
+
+    /**
+     * 构造请求数据
+     * data:业务参数,json字符串
+     */
+    public function bulidRequestParams(){
+        $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->app_secret . $args . $config->app_secret;
+        //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 DadaResponse();
+        if (empty($arr)) {
+            $resp->setStatus(DadaConstant::FAIL);
+            $resp->setMsg(DadaConstant::FAIL_MSG);
+            $resp->setCode(DadaConstant::FAIL_CODE);
+        }else{
+            $data = json_decode($arr, true);
+            $resp->setStatus($data['status']);
+            $resp->setMsg($data['msg']);
+            $resp->setCode($data['code']);
+            $resp->setResult($data['result']);
+        }
+        return $resp;
+    }
+
+    public function getConfig(){
+        return $this->config;
+    }
+
+    public function getApi(){
+        return $this->api;
+    }
+}

+ 73 - 0
vendor/dada/client/dadaResponse.php

@@ -0,0 +1,73 @@
+<?php
+/**
+ * 达达接口返回。
+ */
+
+class DadaResponse{
+
+    /**
+     * 请求响应返回的数据状态
+     */
+    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;
+    }
+
+}

+ 72 - 0
vendor/dada/config/config.php

@@ -0,0 +1,72 @@
+<?php
+
+class Config{
+    
+    /**
+     * 达达开发者app_key
+     */
+    public $app_key = '';
+
+    /**
+     * 达达开发者app_secret
+     */
+    public $app_secret = '';
+
+    /**
+     * api版本
+     */
+    public $v = "1.0";
+
+    /**
+     * 数据格式
+     */
+    public $format = "json";
+
+    /**
+     * 商户ID
+     */
+    public $source_id;
+
+    /**
+     * host
+     */
+    public $host;
+
+
+    /**
+     * 构造函数
+     */
+    public function __construct($source_id, $online){
+        if ($online) {
+            $this->source_id = $source_id;
+            $this->host = "https://newopen.imdada.cn";
+        } else {
+            $this->source_id = "73753";
+            $this->host = "http://newopen.qa.imdada.cn";
+        }
+    }
+
+    public function getAppKey(){
+        return $this->app_key;
+    }
+
+    public function getAppSecret(){
+        return $this->app_secret;
+    }
+
+    public function getV(){
+        return $this->v;
+    }
+
+    public function getFormat(){
+        return $this->format;
+    }
+
+    public function getSourceId(){
+        return $this->source_id;
+    }
+
+    public function getHost(){
+        return $this->host;
+    }
+}

+ 12 - 0
vendor/dada/config/constant.php

@@ -0,0 +1,12 @@
+<?php
+
+class DadaConstant{
+    
+    const FAIL = "fail";
+
+    const SUCCESS = "success";
+
+    const FAIL_MSG = "接口请求超时或失败";
+
+    const FAIL_CODE = -2;
+}

+ 10 - 0
vendor/dada/config/urlConfig.php

@@ -0,0 +1,10 @@
+<?php
+
+class UrlConfig{
+    
+    const ORDER_ADD_URL = "/api/order/addOrder";
+
+    const SHOP_ADD_URL = "/api/shop/add";
+
+    const CITY_ORDER_URL = "/api/cityCode/list";
+}

+ 141 - 0
vendor/dada/model/orderModel.php

@@ -0,0 +1,141 @@
+<?php
+/**
+ * http://newopen.imdada.cn/#/development/file/add?_k=5f4vjj
+ *
+ */
+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;
+    }
+
+}

+ 140 - 0
vendor/dada/model/shopAddModel.php

@@ -0,0 +1,140 @@
+<?php
+/**
+ * http://newopen.imdada.cn/#/development/file/shopAdd?_k=6bpoo0
+ *
+ */
+class ShopAddModel{
+    
+    public $station_name;
+    
+    public $business;
+
+    public $city_name;
+
+    public $area_name;
+
+    public $station_address;
+
+    public $lng;
+
+    public $lat;
+
+    public $contact_name;
+
+    public $phone;
+
+    public $origin_shop_id;
+
+    public $id_card;
+
+    public function setStationName($stationName)
+    {
+        $this->station_name = $stationName;
+    }
+
+    public function getStationName()
+    {
+        return $this->station_name;
+    }
+
+    public function setBusiness($business)
+    {
+        $this->business = $business;
+    }
+
+    public function getBusiness()
+    {
+        return $this->business;
+    }
+
+    public function setCityName($cityName)
+    {
+        $this->city_name = $cityName;
+    }
+
+    public function getCityName()
+    {
+        return $this->city_name;
+    }
+
+    public function setAreaName($areaName)
+    {
+        $this->area_name = $areaName;
+    }
+
+    public function getAreaName()
+    {
+        return $this->area_name;
+    }
+
+    public function setStationAddress($stationAddress)
+    {
+        $this->station_address = $stationAddress;
+    }
+
+    public function getStationAddress()
+    {
+        return $this->station_address;
+    }
+
+    public function setLng($lng)
+    {
+        $this->lng = $lng;
+    }
+
+    public function getLng()
+    {
+        return $this->lng;
+    }
+
+    public function setLat($lat)
+    {
+        $this->lat = $lat;
+    }
+
+    public function getLat()
+    {
+        return $this->lat;
+    }
+
+    public function setContactName($contactName)
+    {
+        $this->contact_name = $contactName;
+    }
+
+    public function getContactName()
+    {
+        return $this->contact_name;
+    }
+
+    public function setPhone($phone)
+    {
+        $this->phone = $phone;
+    }
+
+    public function getPhone()
+    {
+        return $this->phone;
+    }
+
+    public function setOriginShopId($originShopId)
+    {
+        $this->origin_shop_id = $originShopId;
+    }
+
+    public function getOriginShopId()
+    {
+        return $this->origin_shop_id;
+    }
+
+    public function setIdCard($idCard)
+    {
+        $this->id_card = $idCard;
+    }
+
+    public function getIdCard()
+    {
+        return $this->id_card;
+    }
+
+}