DeliveryController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\express\classes\DeliveryAuthTokenClass;
  6. use Yii;
  7. use biz\shop\classes\ShopClass;
  8. use common\components\delivery\services\DispatchService;
  9. use common\components\util;
  10. use yii\helpers\ArrayHelper;
  11. class DeliveryController extends BaseController
  12. {
  13. public function actionList()
  14. {
  15. $platformList = [];
  16. $platformLogos = [
  17. 'data' => ['name'=>'达达', 'logo'=>'📦'],
  18. 'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
  19. 'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
  20. 'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
  21. 'huolala' => ['name'=>'货拉拉跑腿', 'logo'=>'🚛'],
  22. 'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
  23. 'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
  24. 'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
  25. ];
  26. $mainId = $this->mainId;
  27. $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
  28. foreach($allDeliveries as $d){
  29. $item = [
  30. 'id'=>$d['platform'],
  31. 'logo'=>$platformLogos[$d['platform']]['logo'],
  32. 'name'=>$platformLogos[$d['platform']]['name'],
  33. 'is_authorized' => true,
  34. 'balance' => $d['balance']/100.0,
  35. 'access_type' => 'oauth'
  36. ];
  37. $platformList[] = $item;
  38. }
  39. util::success(['platformList'=>$platformList]);
  40. }
  41. // 获取多个平台报价
  42. public function actionAllDeliveryQuotes()
  43. {
  44. $post = Yii::$app->request->post();
  45. $orderTime = date('Y-m-d H:i:s');
  46. // --------- $ghsShopId ----------
  47. $ghsId = $post['ghsId'] ?? 0;
  48. $ghs = GhsClass::getById($ghsId, true);
  49. if (empty($ghs)) {
  50. util::fail('获取批发商失败');
  51. }
  52. $ghsShopId = $ghs->shopId;
  53. // --------- $customId
  54. $customId = $ghs->customId ?? 0;
  55. $custom = CustomClass::getById($customId, true);
  56. if (empty($custom)) {
  57. util::fail('获取custom数据失败');
  58. }
  59. $weight = $post['weight'] ?? 1;
  60. // 生成随机订单号
  61. //$snData = ['shopId' => $ghsShopId, 'mainId' => $ghs->mainId, 'ghsId' => $ghsId, 'customId' => $customId];
  62. //$orderSn = orderSn::getGhsOrderSn($snData);
  63. $prefix = 'XSD_CS-';
  64. $orderSn = $prefix . round(microtime(true) * 1000);
  65. //构建出 Order 数据
  66. $order = [
  67. 'orderSn' => $orderSn,
  68. 'customName' => $custom['name'],
  69. 'customMobile' => $custom['mobile'],
  70. 'fullAddress' => $custom['fullAddress'],
  71. 'floor' => $custom['floor'],
  72. 'dist' => $custom['dist'],
  73. 'lat' => $custom['lat'],
  74. 'long' => $custom['long'],
  75. 'address' => $custom['address'], //'toAddress' => $order['address'],
  76. 'city' => $custom['city'],
  77. 'weight' => $weight,
  78. 'remark' => $post['remark'] ?? '',
  79. 'prePrice' => 10, // TODO 要从前端获取
  80. 'actPrice' => 10, // TODO
  81. ];
  82. $ghsShop = ShopClass::getById($ghsShopId);
  83. $ds = new DispatchService($ghs->mainId);
  84. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  85. if(isset($platformQuotes['error'])){
  86. util::fail($platformQuotes['error']);
  87. }
  88. $deliveryList = [];
  89. // 格式化各平台报价为前端展示格式
  90. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  91. // 按 price 从低到高排序
  92. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  93. $ret['deliveryList'] = $deliveryList;
  94. util::success($ret, "success");
  95. }
  96. public function actionCancelAuth()
  97. {
  98. // 有平台有对应的取消授权接口,有的没有。
  99. // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
  100. $platform = Yii::$app->request->get('platform');
  101. $delivery = DeliveryAuthTokenClass::getByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  102. if($delivery) {
  103. if($platform == 'shansong') {
  104. $auth = new \common\components\delivery\platform\shansong\Auth();
  105. $result = $auth->cancelAuthorization($delivery['accessToken']);
  106. if($result['success']) {
  107. // 授权已取消
  108. }else{
  109. if($result['message'] == '商户未授权,请授权后再次尝试'){
  110. Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
  111. }else{
  112. util::fail('取消授权失败:'.$result['message']);
  113. }
  114. }
  115. }
  116. }else{
  117. util::fail('取消授权失败,数据未找到');
  118. }
  119. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  120. if($ret > 0){
  121. util::success(['message'=>'取消授权成功']);
  122. } else {
  123. util::fail('取消授权失败');
  124. }
  125. }
  126. public function actionGetAuthUrl()
  127. {
  128. $platform = Yii::$app->request->get('platformId');
  129. $Auth = new \common\components\delivery\services\AuthService();
  130. $mainId = $this->mainId;
  131. switch($platform){
  132. case 'shansong':
  133. $Auth->shansongAuth($mainId);
  134. break;
  135. case 'huolala':
  136. $Auth->huolalaAuth($mainId);
  137. break;
  138. case 'fengniao':
  139. $Auth->fengniaoAuth($mainId);
  140. break;
  141. case 'dada':
  142. $Auth->dadaAuth($mainId);
  143. break;
  144. case 'shunfeng':
  145. $Auth->shunfengAuth($mainId);
  146. break;
  147. case 'didi':
  148. break;
  149. default:
  150. util::error(-1, $platform . '不存在');
  151. break;
  152. }
  153. }
  154. }