MtController.php 18 KB

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