MtController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\goods\classes\CategoryClass;
  6. use bizHd\goods\classes\GoodsClass;
  7. use bizHd\meituan\classes\MtOrderClass;
  8. use bizHd\order\classes\OrderClass;
  9. use bizHd\order\services\OrderService;
  10. use bizHd\shop\classes\ShopClass;
  11. use bizHd\work\classes\WorkClass;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\mtUtil;
  15. use common\components\noticeUtil;
  16. use common\components\oss;
  17. use common\components\util;
  18. use Yii;
  19. use Shishaoqi\MeituanFlashPurchase\Application;
  20. use yii\helpers\Json;
  21. class MtController extends PublicController
  22. {
  23. public $guestAccess = [];
  24. public function actionGoodsAdd()
  25. {
  26. //$post = Yii::$app->request->post();
  27. $str = '{"sig":"3595534244c625331cb9e8b35cc63b13","app_id":"119094","retail_data":"%5B%257B%2522app_food_code%2522%253A%2522mtcode_1568954379552215085%2522%252C%2522app_id%2522%253A119094%252C%2522app_poi_code%2522%253A%252236524%2522%252C%2522app_spu_code%2522%253A%2522mtcode_1568954379552215085%2522%252C%2522categoryList%2522%253A%255B%257B%2522categoryCode%2522%253A%2522%2522%252C%2522categoryName%2522%253A%2522%25E6%259C%25AA%25E5%2588%2586%25E7%25B1%25BB%2522%257D%255D%252C%2522categoryName%2522%253A%2522%25E6%259C%25AA%25E5%2588%2586%25E7%25B1%25BB%2522%252C%2522createAppKey%2522%253A%2522%25E5%2595%2586%25E5%25AE%25B6%25E7%25AB%25AF_%25E7%25A7%25BB%25E5%258A%25A8%25E7%25AB%25AF%2522%252C%2522ctime%2522%253A1662902846%252C%2522name%2522%253A%25229%25E6%259C%25B5%25E7%25BA%25A2%25E7%258E%25AB%25E7%2591%25B0%25E6%2590%25AD%25E9%2585%258D%25E6%25BB%25A1%25E5%25A4%25A9%25E6%2598%259F%25E9%25B2%259C%25E8%258A%25B1%25E8%258A%25B1%25E6%259D%259F%2522%252C%2522opName%2522%253A%2522%25E5%2595%2586%25E5%25AE%25B6%25E8%25B4%25A6%25E5%258F%25B7112823666%2522%252C%2522timestamp%2522%253A1662902848%252C%2522utime%2522%253A1662902846%257D%5D","timestamp":"1662902848"}';
  28. $post = json_decode($str, true);
  29. if (isset($post['retail_data']) == false) {
  30. noticeUtil::push("美团添加商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  31. return Json::encode(['data' => 'ok']);
  32. }
  33. $retailData = $post['retail_data'];
  34. $retail = urldecode(urldecode($retailData));
  35. $shopId = $retail['app_poi_code'] ?? 0;
  36. $shop = ShopClass::getById($shopId, true);
  37. if (empty($shop)) {
  38. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到门店", '15280215347');
  39. return Json::encode(['data' => 'ok']);
  40. }
  41. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  42. if (empty($ext)) {
  43. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  44. return Json::encode(['data' => 'ok']);
  45. }
  46. $mainId = $shop->mainId ?? 0;
  47. if (empty($mainId)) {
  48. noticeUtil::push("美团添加商品,通知我们时出错了,没有main信息", '15280215347');
  49. return Json::encode(['data' => 'ok']);
  50. }
  51. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  52. if (empty($mtCategory)) {
  53. $sjId = $shop->sjId ?? 0;
  54. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  55. }
  56. $catId = $mtCategory->id ?? 0;
  57. if (empty($catId)) {
  58. noticeUtil::push("美团添加商品,通知我们时出错了,没有美团的分类id", '15280215347');
  59. return Json::encode(['data' => 'ok']);
  60. }
  61. $config = dict::getDict('mtConfig');
  62. $app = new Application($config);
  63. $accessToken = mtUtil::getAccessToken($ext);
  64. $app_spu_code = $retail['app_spu_code'] ?? '';
  65. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $app_spu_code, 'access_token' => $accessToken];
  66. $mgGoodsDetail = $app->retail->getDetail($params);
  67. Yii::info('获取商品详情:' . $mgGoodsDetail . ' ' . json_encode($params));
  68. return Json::encode(['data' => 'ok']);
  69. $mgGoodsDetail = json_decode($mgGoodsDetail, true);
  70. if (isset($mgGoodsDetail['error'])) {
  71. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到商品图片", '15280215347');
  72. return Json::encode(['data' => 'ok']);
  73. }
  74. $imgString = $mgGoodsDetail['data']['picture'] ?? '';
  75. if (empty($imgString)) {
  76. noticeUtil::push("美团添加商品,通知我们时出错了,,没有商品图片...", '15280215347');
  77. return Json::encode(['data' => 'ok']);
  78. }
  79. noticeUtil::push("美团添加商品,通知我们,商品图片:" . $imgString, '15280215347');
  80. $goodsName = $mtGoods['food_name'] ?? '';
  81. $goodsPrice = $mtGoods['price'] ?? 0;
  82. $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  83. $imgArr = explode(',', $imgString);
  84. foreach ($imgArr as $imgUrl) {
  85. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  86. }
  87. return Json::encode(['data' => 'ok']);
  88. }
  89. public function actionGoodsDel()
  90. {
  91. $post = Yii::$app->request->post();
  92. Yii::info('confirmOrderData: ' . json_encode($post));
  93. return Json::encode(['data' => 'ok']);
  94. }
  95. public function actionGoodsUpdate()
  96. {
  97. $post = Yii::$app->request->post();
  98. Yii::info('confirmOrderData: ' . json_encode($post));
  99. return Json::encode(['data' => 'ok']);
  100. }
  101. //已确认订单通知
  102. public function actionConfirmOrder()
  103. {
  104. //error_reporting(E_ALL);
  105. $post = Yii::$app->request->post();
  106. $mtOrderId = $post['order_id'] ?? 0;
  107. $thirdSn = $post['day_seq'] ?? 0;
  108. //4秒内不允许第三方重复通知
  109. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  110. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  111. if (!empty($has)) {
  112. Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  113. return Json::encode(['data' => 'ok']);
  114. }
  115. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  116. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  117. if (empty($post)) {
  118. return Json::encode(['data' => 'ok']);
  119. }
  120. Yii::info('confirmOrderData: ' . json_encode($post));
  121. $connection = Yii::$app->db;//事务处理
  122. $transaction = $connection->beginTransaction();
  123. try {
  124. $shopId = $post['app_poi_code'] ?? '';
  125. $shop = ShopClass::getById($shopId, true);
  126. if (empty($shop)) {
  127. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  128. return Json::encode(['data' => 'ok']);
  129. }
  130. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  131. if (empty($shopExt)) {
  132. noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  133. return Json::encode(['data' => 'ok']);
  134. }
  135. $mainId = $shop->mainId ?? 0;
  136. $sjId = $shop->sjId ?? 0;
  137. $customId = $shop->defaultCustomId ?? 0;
  138. if (empty($customId)) {
  139. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  140. return Json::encode(['data' => 'ok']);
  141. }
  142. $custom = CustomClass::getById($customId);
  143. if (empty($custom)) {
  144. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  145. return Json::encode(['data' => 'ok']);
  146. }
  147. foreach ($post as $k => $val) {
  148. $post[$k] = urldecode($val);
  149. }
  150. MtOrderClass::add($post);
  151. $detail = $post['detail'] ?? '';
  152. if (empty($detail)) {
  153. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  154. return Json::encode(['data' => 'ok']);
  155. }
  156. $mtGoodsData = json_decode($detail, true);
  157. if (empty($mtGoodsData)) {
  158. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  159. return Json::encode(['data' => 'ok']);
  160. }
  161. Yii::info('商品信息:' . json_encode($mtGoodsData));
  162. $productList = [];
  163. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  164. if (empty($mtCategory)) {
  165. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  166. }
  167. $catId = $mtCategory->id ?? 0;
  168. if (empty($catId)) {
  169. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  170. return Json::encode(['data' => 'ok']);
  171. }
  172. $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : '';
  173. $remark = $caution;
  174. $bookMobile = $post['order_phone_number'] ?? '';
  175. $receiveMobile = $post['recipient_phone'] ?? '';
  176. $receiveUserName = $post['recipient_name'] ?? '';
  177. $fullAddress = $post['recipient_address'] ?? '';
  178. $orderCover = '';
  179. $goodsNum = 0;
  180. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  181. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  182. if (empty($mtGoodsSpu)) {
  183. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  184. $mtGoodsSpu = $mtGoods['app_food_code'];
  185. }
  186. }
  187. $goodsPrice = $mtGoods['price'] ?? 0;
  188. $num = $mtGoods['quantity'] ?? 0;
  189. $goodsNum = bcadd($num, $goodsNum);
  190. $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  191. if (empty($currentGoods)) {
  192. noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  193. return Json::encode(['data' => 'ok']);
  194. }
  195. $id = $currentGoods->id ?? 0;
  196. $unitType = 0;
  197. $property = dict::getDict('property', 'goods');
  198. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  199. $productList = array_merge($goods, $productList);
  200. }
  201. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  202. if (empty($poi_receive_detail_yuan)) {
  203. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  204. util::stop();
  205. }
  206. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  207. if (is_array($poi_receive_detail_yuan_data) == false) {
  208. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  209. util::stop();
  210. }
  211. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  212. if ($onlinePayment == 0) {
  213. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  214. util::stop();
  215. }
  216. $modifyPrice = $onlinePayment;
  217. //技术服务费
  218. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  219. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  220. //履约服务费
  221. $performanceServiceFee = 0;
  222. if (!empty($reconciliationExtras)) {
  223. $extArr = json_decode($reconciliationExtras, true);
  224. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  225. }
  226. //运费
  227. $sendCost = $post['shipping_fee'] ?? 0;
  228. //打包费
  229. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  230. //服务费和手续费
  231. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  232. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  233. $reachPeriod = date("H:i", $estimate_arrival_time);
  234. $reachDate = date("Y-m-d", $estimate_arrival_time);
  235. $reachTime = $estimate_arrival_time;
  236. $orderData = [
  237. 'product' => $productList,
  238. 'fromType' => dict::getDict('fromType', 'mt'),
  239. 'labourCost' => $labourCost,
  240. 'sendCost' => $sendCost,
  241. 'shopId' => $shopId,
  242. 'mainId' => $mainId,
  243. 'sjId' => $sjId,
  244. 'modifyPrice' => $modifyPrice,
  245. 'serviceFee' => $serviceFee,
  246. 'thirdSn' => $thirdSn,
  247. 'cover' => $orderCover,
  248. 'reachTime' => $reachTime,
  249. 'reachDate' => $reachDate,
  250. 'reachPeriod' => $reachPeriod,
  251. 'remark' => $remark,
  252. 'goodsNum' => $goodsNum,
  253. 'bookMobile' => $bookMobile,
  254. 'receiveMobile' => $receiveMobile,
  255. 'receiveUserName' => $receiveUserName,
  256. 'fullAddress' => $fullAddress,
  257. 'thirdOrderId' => $mtOrderId,
  258. ];
  259. $hasPay = dict::getDict('hasPay', 'payed');
  260. $order = OrderService::createFinishOrder($orderData, $custom, $hasPay);
  261. $transaction->commit();
  262. //新制作单提示
  263. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  264. ShopExtClass::newWorkRemind($shopExt, $order);
  265. return Json::encode(['data' => 'ok']);
  266. } catch (\Exception $e) {
  267. $transaction->rollBack();
  268. $msg = $e->getMessage();
  269. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  270. return Json::encode(['data' => 'ok']);
  271. }
  272. }
  273. //取消订单
  274. public function actionCancelOrder()
  275. {
  276. $post = Yii::$app->request->post();
  277. $mtOrderId = $post['order_id'] ?? 0;
  278. $shopId = $post['app_poi_code'] ?? '';
  279. if (empty($post)) {
  280. return Json::encode(['data' => 'ok']);
  281. }
  282. Yii::info('cancelOrderData: ' . json_encode($post));
  283. //4秒内不允许第三方重复通知
  284. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  285. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  286. if (!empty($has)) {
  287. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  288. return Json::encode(['data' => 'ok']);
  289. }
  290. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  291. $mt = dict::getDict('fromType', 'mt');
  292. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  293. if (empty($order)) {
  294. //没有接单时取消订单
  295. return Json::encode(['data' => 'ok']);
  296. }
  297. //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
  298. $orderId = $order->id ?? 0;
  299. $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  300. $hasCompleteWork = false;
  301. if (!empty($workList)) {
  302. foreach ($workList as $work) {
  303. if ($work->status == 1) {
  304. $hasCompleteWork = true;
  305. }
  306. }
  307. }
  308. //取消订单和制作单
  309. if ($hasCompleteWork == false) {
  310. $order->status = 5;
  311. $order->save();
  312. if (!empty($workList)) {
  313. foreach ($workList as $work) {
  314. $work->status = 2;
  315. $work->save();
  316. }
  317. }
  318. }
  319. //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!!
  320. //制作单取消通知
  321. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  322. ShopExtClass::orderCancelRemind($shopExt, $order);
  323. return Json::encode(['data' => 'ok']);
  324. }
  325. //推送已支付订单
  326. public function actionNewOrder()
  327. {
  328. return Json::encode(['data' => 'ok']);
  329. }
  330. //部分退款
  331. public function actionPartRefund()
  332. {
  333. return Json::encode(['data' => 'ok']);
  334. }
  335. //全部退款
  336. public function actionAllRefund()
  337. {
  338. return Json::encode(['data' => 'ok']);
  339. }
  340. }