dada.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/3/25 21:38
  5. */
  6. namespace common\components\dada;
  7. use common\components\dada\api\cityCodeApi;
  8. use common\components\dada\api\orderApi;
  9. use common\components\dada\api\reasonsApi;
  10. use common\components\dada\config\config;
  11. use common\components\dada\protocol\requestClient;
  12. use common\components\util;
  13. use Yii;
  14. //达达快递
  15. class dada
  16. {
  17. //cityCode
  18. public static function getCityCode($sourceId)
  19. {
  20. $config = self::getConfig($sourceId);
  21. $api = new cityCodeApi('');
  22. $client = new requestClient($config, $api);
  23. $resp = $client->makeRequest();
  24. return self::handleResp($resp);
  25. }
  26. //
  27. /*
  28. [{
  29. "reason": "没有配送员接单",
  30. "id": 1
  31. }, {
  32. "reason": "配送员没来取货",
  33. "id": 2
  34. }, {
  35. "reason": "配送员态度太差",
  36. "id": 3
  37. }, {
  38. "reason": "顾客取消订单",
  39. "id": 4
  40. }, {
  41. "reason": "订单填写错误",
  42. "id": 5
  43. }, {
  44. "reason": "配送员让我取消此单",
  45. "id": 34
  46. }, {
  47. "reason": "配送员不愿上门取货",
  48. "id": 35
  49. }, {
  50. "reason": "我不需要配送了",
  51. "id": 36
  52. }, {
  53. "reason": "配送员以各种理由表示无法完成订单",
  54. "id": 37
  55. }, {
  56. "reason": "其他",
  57. "id": 10000
  58. }]
  59. */
  60. public static function getCancelReason($sourceId)
  61. {
  62. $config = self::getConfig($sourceId);
  63. $api = new reasonsApi('');
  64. $client = new requestClient($config, $api);
  65. $resp = $client->makeRequest();
  66. return self::handleResp($resp);
  67. }
  68. //新增订单
  69. /*
  70. *
  71. shop_no String 是 门店编号,门店创建后可在门店列表和单页查看
  72. origin_id String 是 第三方订单ID
  73. city_code String 是 订单所在城市的code(查看各城市对应的code值)
  74. cargo_price Double 是 订单金额(单位:元)
  75. is_prepay Integer 是 是否需要垫付 1:是 0:否 (垫付订单金额,非运费)
  76. receiver_name String 是 收货人姓名
  77. receiver_address String 是 收货人地址
  78. receiver_lat Double 是 收货人地址纬度(高德坐标系,若是其他地图经纬度需要转化成高德地图经纬度,高德地图坐标拾取器)
  79. receiver_lng Double 是 收货人地址经度(高德坐标系,若是其他地图经纬度需要转化成高德地图经纬度,高德地图坐标拾取器)
  80. callback String 是 回调URL(查看回调说明)
  81. cargo_weight Double 是 订单重量(单位:Kg)
  82. receiver_phone String 否 收货人手机号(手机号和座机号必填一项)
  83. receiver_tel String 否 收货人座机号(手机号和座机号必填一项)
  84. tips Double 否 小费(单位:元,精确小数点后一位)
  85. info String 否 订单备注
  86. cargo_type Integer 否 订单商品类型:食品小吃-1,饮料-2,鲜花-3,文印票务-8,便利店-9,水果生鲜-13,同城电商-19, 医药-20,蛋糕-21,酒品-24,小商品市场-25,服装-26,汽修零配-27,数码-28,小龙虾-29,火锅-51,其他-5
  87. cargo_num Integer 否 订单商品数量
  88. invoice_title String 否 发票抬头
  89. origin_mark String 否 订单来源标示(只支持字母,最大长度为10)
  90. origin_mark_no String 否 订单来源编号,最大长度为30,该字段可以显示在骑士APP订单详情页面,示例:origin_mark_no:"#京东到家#1"达达骑士APP看到的是:#京东到家#1
  91. is_use_insurance Integer 否是否使用保价费(0:不使用保价,1:使用保价; 同时,请确保填写了订单金额(cargo_price))商品保价费(当商品出现损坏,可获取一定金额的赔付)保费=配送物品实际价值*费率(5‰),配送物品价值及最高赔付不超过10000元, 最高保费为50元(物品价格最小单位为100元,不足100元部分按100元认定,保价费向上取整数, 如:物品声明价值为201元,保价费为300元*5‰=1.5元,取整数为2元。)若您选择不保价,若物品出现丢失或损毁,最高可获得平台30元优惠券。 (优惠券直接存入用户账户中)。
  92. is_finish_code_needed Integer 否 收货码(0:不需要;1:需要。收货码的作用是:骑手必须输入收货码才能完成订单妥投)
  93. delay_publish_time Integer 否 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约,可以支持未来3天内的订单发预约单。)
  94. is_direct_delivery Integer 否 是否选择直拿直送(0:不需要;1:需要。选择直拿直送后,同一时间骑士只能配送此订单至完成,同时,也会相应的增加配送费用)
  95. product_list Object 否 订单商品明细
  96. pick_up_pos String 否 货架信息,该字段可在骑士APP订单备注中展示
  97. product_list:参数
  98. sku_name String 是 商品名称,限制长度128
  99. src_product_no String 是 商品编码,限制长度64
  100. count Double 是 商品数量,精确到小数点后两位
  101. unit String 否 商品单位,默认:件
  102. */
  103. /*
  104. {
  105. "status": "success",
  106. "result": {
  107. "distance": 53459.98,
  108. "fee": 51.0
  109. "deliverFee": 51.0
  110. },
  111. "code": 0,
  112. "msg": "成功"
  113. }
  114. */
  115. public static function addOrder($sourceId, $params)
  116. {
  117. $config = self::getConfig($sourceId);
  118. $api = new orderApi();
  119. //body内容
  120. $api->addOrder($params);
  121. $client = new requestClient($config, $api);
  122. $resp = $client->makeRequest();
  123. return self::handleResp($resp);
  124. }
  125. //运费查询(参数跟新增订单一致)
  126. public static function queryDeliverFee($sourceId, $params)
  127. {
  128. $config = self::getConfig($sourceId);
  129. $api = new orderApi();
  130. $api->queryDeliverFee($params);
  131. $client = new requestClient($config, $api);
  132. $resp = $client->makeRequest();
  133. return self::handleResp($resp);
  134. }
  135. //订单重发(参数跟新增订单一致)
  136. public static function reAddOrder($sourceId, $params)
  137. {
  138. $config = self::getConfig($sourceId);
  139. $api = new orderApi();
  140. $api->reAddOrder($params);
  141. $client = new requestClient($config, $api);
  142. $resp = $client->makeRequest();
  143. return self::handleResp($resp);
  144. }
  145. //订单取消 (可以用自定义的理由)
  146. /*
  147. order_id String 是 第三方订单编号
  148. cancel_reason_id Integer 是 取消原因ID(取消原因列表)
  149. cancel_reason String 否 取消原因(当取消原因ID为其他时,此字段必填)
  150. */
  151. public static function formalCancel($sourceId, $orderSn, $reasonId, $reason = '')
  152. {
  153. $config = self::getConfig($sourceId);
  154. $api = new orderApi();
  155. $params = [
  156. 'order_id' => $orderSn,
  157. 'cancel_reason_id' => $reasonId,
  158. ];
  159. if ($reasonId == 10000) {
  160. $params['cancel_reason'] = $reason;
  161. }
  162. $api->formalCancel($params);
  163. $client = new requestClient($config, $api);
  164. $resp = $client->makeRequest();
  165. return self::handleResp($resp);
  166. }
  167. //订单状态查询
  168. public static function statusQuery($sourceId, $orderSn)
  169. {
  170. $config = self::getConfig($sourceId);
  171. $api = new orderApi();
  172. $params = [
  173. 'order_id' => $orderSn,
  174. ];
  175. $api->queryOrderStatus($params);
  176. $client = new requestClient($config, $api);
  177. $resp = $client->makeRequest();
  178. return self::handleResp($resp);
  179. }
  180. public static function getConfig($sourceId)
  181. {
  182. if (empty($sourceId)) {
  183. util::fail("请先开通达达配送");
  184. }
  185. $config = new config();
  186. //$online = Yii::$app->params['dadaOnline'];
  187. $appKey = Yii::$app->params['dadaAppKey'];
  188. $appSecret = Yii::$app->params['dadaAppSecret'];
  189. if (getenv('YII_ENV', 'local') == 'production') {
  190. $config->setSourceId($sourceId);
  191. $config->setHost('https://newopen.imdada.cn');
  192. } else {
  193. //测试 :商户id: 73753 门店id: 11047059
  194. $config->setSourceId(73753);
  195. $config->setHost("http://newopen.qa.imdada.cn");
  196. }
  197. $config->setAppKey($appKey);
  198. $config->setAppSecret($appSecret);
  199. return $config;
  200. }
  201. public static function handleResp($resp)
  202. {
  203. if ($resp->getStatus() != 'success') {
  204. util::fail($resp->getMsg());
  205. }
  206. return $resp->getResult();
  207. }
  208. }