DeliveryController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 bizHd\order\classes\OrderClass;
  7. use bizHd\express\classes\HdDeliveryOrderClass;
  8. use hd\models\delivery\CreateOrderForm;
  9. use Yii;
  10. use biz\shop\classes\ShopClass;
  11. use common\components\delivery\services\DispatchService;
  12. use common\components\util;
  13. use yii\helpers\ArrayHelper;
  14. class DeliveryController extends BaseController
  15. {
  16. public function actionList()
  17. {
  18. $platformList = [];
  19. $platformLogos = [
  20. 'dada' => ['name'=>'达达', 'logo'=>'📦'],
  21. 'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
  22. 'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
  23. 'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
  24. 'huolala' => ['name'=>'货拉拉跑腿', 'logo'=>'🚛'],
  25. 'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
  26. 'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
  27. 'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
  28. ];
  29. $mainId = $this->mainId;
  30. $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
  31. foreach($allDeliveries as $d){
  32. $item = [
  33. 'id'=>$d['platform'],
  34. 'logo'=>$platformLogos[$d['platform']]['logo'],
  35. 'name'=>$platformLogos[$d['platform']]['name'],
  36. 'is_authorized' => true,
  37. 'balance' => $d['balance']/100.0,
  38. 'access_type' => 'oauth'
  39. ];
  40. $platformList[] = $item;
  41. }
  42. util::success(['platformList'=>$platformList]);
  43. }
  44. // 花店发货 -- 获取多个平台报价
  45. public function actionHdAllDeliveryQuotes()
  46. {
  47. $post = Yii::$app->request->post();
  48. $orderTime = $post['pickupTime'];
  49. $weight = $post['weight'];
  50. $remark = $post['remark'] ?? '';
  51. $adminId = $this->adminId;
  52. util::checkRepeatCommit($adminId, 3);
  53. $orderId = intval($post['orderId']);
  54. $order = OrderClass::getById($orderId);
  55. $shop = ShopClass::getById($this->shopId);
  56. $order['weight'] = $weight;
  57. $order['remark'] = $remark;
  58. $ds = new DispatchService($this->mainId);
  59. $platformQuotes = $ds->getAllPlatformPrice($order, $shop, $orderTime);
  60. if(isset($platformQuotes['error'])){
  61. util::fail($platformQuotes['error']);
  62. }
  63. // 格式化各平台报价为前端展示格式
  64. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  65. // 按 price 从低到高排序
  66. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  67. $ret['deliveryList'] = $deliveryList;
  68. util::success($ret, "success");
  69. }
  70. // 花店向批发买花 -- 获取多个平台报价
  71. public function actionAllDeliveryQuotes()
  72. {
  73. $post = Yii::$app->request->post();
  74. $orderTime = date('Y-m-d H:i:s');
  75. // --------- $ghsShopId ----------
  76. $ghsId = $post['ghsId'] ?? 0;
  77. $ghs = GhsClass::getById($ghsId, true);
  78. if (empty($ghs)) {
  79. util::fail('获取批发商失败');
  80. }
  81. $ghsShopId = $ghs->shopId;
  82. // --------- $customId
  83. $customId = $ghs->customId ?? 0;
  84. $custom = CustomClass::getById($customId, true);
  85. if (empty($custom)) {
  86. util::fail('获取custom数据失败');
  87. }
  88. $weight = $post['weight'] ?? 1;
  89. // 生成随机订单号
  90. $prefix = 'XSD_CS-' . $ghs->mainId . '-';
  91. $orderSn = $prefix . round(microtime(true) * 1000);
  92. //构建出 Order 数据
  93. $order = [
  94. 'orderSn' => $orderSn,
  95. 'customName' => $custom['name'],
  96. 'customMobile' => $custom['mobile'],
  97. 'fullAddress' => $custom['fullAddress'],
  98. 'floor' => $custom['floor'],
  99. 'dist' => $custom['dist'],
  100. 'lat' => $custom['lat'],
  101. 'long' => $custom['long'],
  102. 'address' => $custom['address'], //'toAddress' => $order['address'],
  103. 'city' => $custom['city'],
  104. 'weight' => $weight,
  105. 'remark' => $post['remark'] ?? '',
  106. 'prePrice' => 10, // TODO 要从前端获取
  107. 'actPrice' => 10, // TODO
  108. ];
  109. $ghsShop = ShopClass::getById($ghsShopId);
  110. $ds = new DispatchService($ghs->mainId);
  111. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  112. if(isset($platformQuotes['error'])){
  113. util::fail($platformQuotes['error']);
  114. }
  115. $deliveryList = [];
  116. // 格式化各平台报价为前端展示格式
  117. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  118. // 按 price 从低到高排序
  119. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  120. $ret['deliveryList'] = $deliveryList;
  121. util::success($ret, "success");
  122. }
  123. // 创建订单(真实下单)
  124. public function actionCreateOrder()
  125. {
  126. $form = new CreateOrderForm();
  127. $form->loadAndValidate();
  128. $post = $form->getAttributes();
  129. $orderId = $post['orderId']; // xhOrder.id
  130. $params = $post['allParams'];
  131. $platform = $params['en_name'];// $post['platform'] 是中文
  132. $pickupTime = $post['pickupTime'];
  133. $params['mainId'] = $this->mainId;
  134. $params['ip'] = Yii::$app->request->userIP;
  135. $params['remark'] = $post['remark'];
  136. $params['total_price_fen'] = $post['price'];
  137. $params['pickupTime'] = $pickupTime['value'];
  138. $shopId = $this->shopId;
  139. $Order = OrderClass::getById($orderId, true);
  140. if(!in_array($Order['sendStatus'], [-4, -1])) {
  141. Yii::error('订单已经发过跑腿');
  142. util::fail('订单已经发过跑腿');
  143. }
  144. $ds = new DispatchService($this->mainId, $platform);
  145. $ret = $ds->createOrder($Order, $shopId, $params);
  146. if (isset($ret['code']) && $ret['code'] == 0) {
  147. $gdo = HdDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'orderId'=>$ret['data']['order_id']]);
  148. if (empty($gdo)) {
  149. $data = [
  150. 'mainId' => $this->mainId,
  151. 'shopId' => $this->shopId,
  152. 'deliveryId' => $ret['platform'],
  153. 'hdOrderId' => $orderId,
  154. 'orderId' => $ret['data']['order_id'],
  155. 'distance' => $ret['data']['distance'],
  156. 'fee' => $ret['data']['fee'],
  157. 'orderStatus' => 0
  158. ];
  159. HdDeliveryOrderClass::add($data);
  160. } else {
  161. Yii::info('重复创建订单');
  162. }
  163. //更新订单
  164. $Order->sendDistance = $ret['data']['distance'];
  165. $Order->sendStatus = 0;
  166. $Order->deliveryId = $ret['platform'];
  167. $Order->sendType = 2;// 2指跑腿
  168. $Order->status = 3;// 3配送中
  169. $save = $Order->save();
  170. if($save){
  171. util::success($ret, "success");
  172. }else{
  173. util::fail('创建跑腿失败(更新订单失败)');
  174. }
  175. } else {
  176. if(isset($ret['msg'])){
  177. util::fail($ret['msg']);
  178. }
  179. util::fail('创建跑腿失败');
  180. }
  181. }
  182. public function actionAddress()
  183. {
  184. $post = Yii::$app->request->post();
  185. $adminId = $this->adminId;
  186. util::checkRepeatCommit($adminId, 4);
  187. $orderId = intval($post['orderId']);
  188. $order = OrderClass::getById($orderId, false, 'fullAddress');
  189. $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
  190. $ret = [
  191. "send_address" => $shop['fullAddress'],
  192. "receive_address" => $order['fullAddress']
  193. ];
  194. util::success($ret, "success");
  195. }
  196. public function actionCancelAuth()
  197. {
  198. // 有平台有对应的取消授权接口,有的没有。
  199. // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
  200. $platform = Yii::$app->request->get('platform');
  201. $delivery = DeliveryAuthTokenClass::getByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  202. if($delivery) {
  203. if($platform == 'shansong') {
  204. $auth = new \common\components\delivery\platform\shansong\Auth();
  205. $result = $auth->cancelAuthorization($delivery['accessToken']);
  206. if($result['success']) {
  207. // 授权已取消
  208. }else{
  209. if($result['message'] == '商户未授权,请授权后再次尝试'){
  210. Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
  211. }else{
  212. util::fail('取消授权失败:'.$result['message']);
  213. }
  214. }
  215. }
  216. }else{
  217. util::fail('取消授权失败,数据未找到');
  218. }
  219. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  220. if($ret > 0){
  221. util::success(['message'=>'取消授权成功']);
  222. } else {
  223. util::fail('取消授权失败');
  224. }
  225. }
  226. public function actionGetAuthUrl()
  227. {
  228. $platform = Yii::$app->request->get('platformId');
  229. $Auth = new \common\components\delivery\services\AuthService();
  230. $mainId = $this->mainId;
  231. switch($platform){
  232. case 'shansong':
  233. $Auth->shansongAuth($mainId);
  234. break;
  235. case 'huolala':
  236. $Auth->huolalaAuth($mainId);
  237. break;
  238. case 'fengniao':
  239. $Auth->fengniaoAuth($mainId);
  240. break;
  241. case 'dada':
  242. $Auth->dadaAuth($mainId);
  243. break;
  244. case 'shunfeng':
  245. $Auth->shunfengAuth($mainId);
  246. break;
  247. case 'didi':
  248. break;
  249. default:
  250. util::error(-1, $platform . '不存在');
  251. break;
  252. }
  253. }
  254. }