MtController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\stock\classes\GoodsStockRecordClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizHd\goods\classes\CategoryClass;
  7. use bizHd\goods\classes\GoodsCategoryClass;
  8. use bizHd\goods\classes\GoodsClass;
  9. use bizHd\meituan\classes\MtOrderClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\order\classes\OrderGoodsClass;
  12. use bizHd\order\classes\OrderItemClass;
  13. use bizHd\order\services\OrderService;
  14. use bizHd\refund\classes\HdRefundClass;
  15. use bizHd\refund\services\HdRefundService;
  16. use bizHd\shop\classes\ShopClass;
  17. use bizHd\work\classes\WorkClass;
  18. use common\components\dict;
  19. use common\components\httpUtil;
  20. use common\components\mtUtil;
  21. use common\components\noticeUtil;
  22. use common\components\oss;
  23. use common\components\util;
  24. use Yii;
  25. use Shishaoqi\MeituanFlashPurchase\Application;
  26. use yii\helpers\Json;
  27. class MtController extends PublicController
  28. {
  29. public $guestAccess = [];
  30. //订单完成 ssh 20220928
  31. public function actionFinishOrder()
  32. {
  33. //error_reporting(E_ALL);
  34. $post = Yii::$app->request->post();
  35. $mtOrderId = $post['order_id'] ?? 0;
  36. $thirdSn = $post['day_seq'] ?? 0;
  37. //4秒内不允许第三方重复通知
  38. $cacheKey = 'mt_finish_order_' . $mtOrderId;
  39. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  40. if (!empty($has)) {
  41. Yii::info("美团推送完成订单报错,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  42. return Json::encode(['data' => 'ok']);
  43. }
  44. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  45. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  46. if (empty($post)) {
  47. return Json::encode(['data' => 'ok']);
  48. }
  49. Yii::info('confirmOrderData: ' . json_encode($post));
  50. $connection = Yii::$app->db;//事务处理
  51. $transaction = $connection->beginTransaction();
  52. try {
  53. $shopId = $post['app_poi_code'] ?? '';
  54. $shop = ShopClass::getById($shopId, true);
  55. if (empty($shop)) {
  56. noticeUtil::push("美团推送完成订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  57. return Json::encode(['data' => 'ok']);
  58. }
  59. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  60. if (empty($shopExt)) {
  61. noticeUtil::push("美团推送完成订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  62. return Json::encode(['data' => 'ok']);
  63. }
  64. $mainId = $shop->mainId ?? 0;
  65. $sjId = $shop->sjId ?? 0;
  66. $customId = $shop->defaultCustomId ?? 0;
  67. if (empty($customId)) {
  68. noticeUtil::push("美团推送完成订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  69. return Json::encode(['data' => 'ok']);
  70. }
  71. $custom = CustomClass::getById($customId);
  72. if (empty($custom)) {
  73. noticeUtil::push("美团推送完成订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  74. return Json::encode(['data' => 'ok']);
  75. }
  76. $order = OrderClass::getByCondition(['fromType' => 4, 'thirdOrderId' => $mtOrderId], true);
  77. if (empty($order)) {
  78. noticeUtil::push("美团推送完成订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  79. return Json::encode(['data' => 'ok']);
  80. }
  81. if ($order->status != 2 || $order->status != 3) {
  82. noticeUtil::push("美团推送完成订单报错,订单不是待完成订单,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  83. return Json::encode(['data' => 'ok']);
  84. }
  85. //开发中,待完成
  86. $transaction->commit();
  87. return Json::encode(['data' => 'ok']);
  88. } catch (\Exception $e) {
  89. $transaction->rollBack();
  90. $msg = $e->getMessage();
  91. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  92. return Json::encode(['data' => 'ok']);
  93. }
  94. }
  95. public function actionGoodsAdd()
  96. {
  97. $post = Yii::$app->request->post();
  98. $connection = Yii::$app->db;//事务处理
  99. $transaction = $connection->beginTransaction();
  100. try {
  101. if (isset($post['retail_data']) == false) {
  102. //noticeUtil::push("..美团添加商品,通知我们时出错了,没有获取到请求的具体信息", '15280215347');
  103. return Json::encode(['data' => 'ok']);
  104. Yii::$app->end();
  105. }
  106. $retailData = $post['retail_data'];
  107. $retail = urldecode(urldecode($retailData));
  108. $info = json_decode($retail);
  109. $obj = $info[0] ?? null;
  110. $shopId = $obj->app_poi_code ?? 0;
  111. $shop = ShopClass::getById($shopId, true);
  112. if (empty($shop)) {
  113. noticeUtil::push("..美团添加商品,通知我们时出错了,没有找到门店", '15280215347');
  114. //return Json::encode(['data' => 'ok']);
  115. Yii::$app->end();
  116. }
  117. $sjId = $shop->sjId ?? 0;
  118. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  119. if (empty($ext)) {
  120. noticeUtil::push("..美团添加商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  121. //return Json::encode(['data' => 'ok']);
  122. Yii::$app->end();
  123. }
  124. $mainId = $shop->mainId ?? 0;
  125. if (empty($mainId)) {
  126. noticeUtil::push("..美团添加商品,通知我们时出错了,没有main信息", '15280215347');
  127. //return Json::encode(['data' => 'ok']);
  128. Yii::$app->end();
  129. }
  130. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  131. if (empty($mtCategory)) {
  132. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  133. }
  134. $catId = $mtCategory->id ?? 0;
  135. if (empty($catId)) {
  136. noticeUtil::push("..美团添加商品,通知我们时出错了,没有美团的分类id", '15280215347');
  137. //return Json::encode(['data' => 'ok']);
  138. Yii::$app->end();
  139. }
  140. $config = dict::getDict('mtConfig');
  141. $app = new Application($config);
  142. $accessToken = mtUtil::getAccessToken($ext);
  143. $appSpuCode = $obj->app_spu_code ?? '';
  144. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  145. if (!empty($hasGoods)) {
  146. noticeUtil::push("..美团添加商品,通知我们时,发现已经有这个商品了,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
  147. //return Json::encode(['data' => 'ok']);
  148. Yii::$app->end();
  149. }
  150. $params = ['app_poi_code' => (string)$shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  151. $goodsInfo = $app->retail->getDetail($params);
  152. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  153. $goodsInfo = json_decode($goodsInfo, true);
  154. if (isset($goodsInfo['error'])) {
  155. noticeUtil::push("..美团添加商品,通知我们时出错了,没有找到商品!", '15280215347');
  156. //return Json::encode(['data' => 'ok']);
  157. Yii::$app->end();
  158. }
  159. $imgString = $goodsInfo['data']['picture'] ?? '';
  160. if (empty($imgString)) {
  161. noticeUtil::push("..美团添加商品,通知我们时出错了,没有商品图片...", '15280215347');
  162. //return Json::encode(['data' => 'ok']);
  163. Yii::$app->end();
  164. }
  165. noticeUtil::push("..美团添加商品,通知我们,商品图片:" . $imgString);
  166. $goodsName = $goodsInfo['data']['name'] ?? '';
  167. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  168. $description = $goodsInfo['data']['description'] ?? '';
  169. $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
  170. $shopImg = [];
  171. $cover = '';
  172. $imgArr = explode(',', $imgString);
  173. if (!empty($imgArr)) {
  174. foreach ($imgArr as $imgUrl) {
  175. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  176. $shopImg[] = $shortUrl;
  177. if ($cover == '') {
  178. $cover = $shortUrl;
  179. }
  180. }
  181. }
  182. $catIds = [$catId];
  183. $addData = [
  184. 'name' => $goodsName,
  185. 'flower' => 1,
  186. 'property' => 0,
  187. 'price' => $goodsPrice,
  188. 'sjId' => $sjId,
  189. 'mainId' => $mainId,
  190. 'shopId' => $shopId,
  191. 'status' => 0,
  192. 'delStatus' => 0,
  193. 'shopImg' => $shopImg,
  194. 'catIds' => $catIds,
  195. 'cover' => $cover,
  196. 'spu' => $appSpuCode,
  197. 'sh' => $sh,
  198. ];
  199. GoodsClass::addGoods($addData);
  200. $transaction->commit();
  201. return Json::encode(['data' => 'ok']);
  202. } catch (\Exception $e) {
  203. $transaction->rollBack();
  204. $msg = $e->getMessage();
  205. noticeUtil::push("..美团添加商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  206. //return Json::encode(['data' => 'ok']);
  207. Yii::$app->end();
  208. }
  209. }
  210. //删除商品 ssh 20220914
  211. public function actionGoodsDel()
  212. {
  213. $post = Yii::$app->request->post();
  214. if (isset($post['retail_data']) == false) {
  215. //noticeUtil::push("美团删除商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  216. return Json::encode(['data' => 'ok']);
  217. }
  218. $retailData = $post['retail_data'];
  219. $retail = urldecode(urldecode($retailData));
  220. $info = json_decode($retail);
  221. $obj = $info[0] ?? null;
  222. $shopId = $obj->app_poi_code ?? 0;
  223. $shop = ShopClass::getById($shopId, true);
  224. if (empty($shop)) {
  225. noticeUtil::push("美团删除商品,通知我们时出错了,没有找到门店", '15280215347');
  226. return Json::encode(['data' => 'ok']);
  227. }
  228. $sjId = $shop->sjId ?? 0;
  229. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  230. if (empty($ext)) {
  231. noticeUtil::push("美团删除商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  232. return Json::encode(['data' => 'ok']);
  233. }
  234. $mainId = $shop->mainId ?? 0;
  235. if (empty($mainId)) {
  236. noticeUtil::push("美团删除商品,通知我们时出错了,没有main信息", '15280215347');
  237. return Json::encode(['data' => 'ok']);
  238. }
  239. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  240. if (empty($mtCategory)) {
  241. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  242. }
  243. $catId = $mtCategory->id ?? 0;
  244. if (empty($catId)) {
  245. noticeUtil::push("美团删除商品,通知我们时出错了,没有美团的分类id", '15280215347');
  246. return Json::encode(['data' => 'ok']);
  247. }
  248. $appSpuCode = $obj->app_spu_code ?? '';
  249. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  250. if (empty($hasGoods)) {
  251. return Json::encode(['data' => 'ok']);
  252. }
  253. $hasGoods->delStatus = 1;
  254. $hasGoods->save();
  255. $goodsId = $hasGoods->id ?? 0;
  256. GoodsCategoryClass::updateByCondition(['gId' => $goodsId], ['delStatus' => 1]);
  257. return Json::encode(['data' => 'ok']);
  258. }
  259. //修改商品 ssh 20220914
  260. public function actionGoodsUpdate()
  261. {
  262. $post = Yii::$app->request->post();
  263. $connection = Yii::$app->db;//事务处理
  264. $transaction = $connection->beginTransaction();
  265. try {
  266. if (isset($post['retail_data']) == false) {
  267. //noticeUtil::push("美团修改商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  268. return Json::encode(['data' => 'ok']);
  269. }
  270. $retailData = $post['retail_data'];
  271. $retail = urldecode(urldecode($retailData));
  272. $info = json_decode($retail);
  273. $obj = $info[0] ?? null;
  274. $shopId = $obj->app_poi_code ?? 0;
  275. $shop = ShopClass::getById($shopId, true);
  276. if (empty($shop)) {
  277. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店", '15280215347');
  278. return Json::encode(['data' => 'ok']);
  279. }
  280. $sjId = $shop->sjId ?? 0;
  281. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  282. if (empty($ext)) {
  283. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  284. return Json::encode(['data' => 'ok']);
  285. }
  286. $mainId = $shop->mainId ?? 0;
  287. if (empty($mainId)) {
  288. noticeUtil::push("美团修改商品,通知我们时出错了,没有main信息", '15280215347');
  289. return Json::encode(['data' => 'ok']);
  290. }
  291. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  292. if (empty($mtCategory)) {
  293. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  294. }
  295. $catId = $mtCategory->id ?? 0;
  296. if (empty($catId)) {
  297. noticeUtil::push("美团修改商品,通知我们时出错了,没有美团的分类id", '15280215347');
  298. return Json::encode(['data' => 'ok']);
  299. }
  300. $config = dict::getDict('mtConfig');
  301. $app = new Application($config);
  302. $accessToken = mtUtil::getAccessToken($ext);
  303. $appSpuCode = $obj->app_spu_code ?? '';
  304. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  305. if (empty($hasGoods)) {
  306. noticeUtil::push("美团修改商品,通知我们时,没有发现这个商品,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
  307. return Json::encode(['data' => 'ok']);
  308. }
  309. $hasGoodsId = $hasGoods->id ?? 0;
  310. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  311. $goodsInfo = $app->retail->getDetail($params);
  312. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  313. $goodsInfo = json_decode($goodsInfo, true);
  314. if (isset($goodsInfo['error'])) {
  315. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到商品图片", '15280215347');
  316. return Json::encode(['data' => 'ok']);
  317. }
  318. $imgString = $goodsInfo['data']['picture'] ?? '';
  319. if (empty($imgString)) {
  320. noticeUtil::push("美团修改商品,通知我们时出错了,没有商品图片...", '15280215347');
  321. return Json::encode(['data' => 'ok']);
  322. }
  323. $goodsName = $goodsInfo['data']['name'] ?? '';
  324. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  325. $description = $goodsInfo['data']['description'] ?? '';
  326. $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
  327. $currentStr = md5($goodsName . '_' . $goodsPrice . '_' . $description . '_' . $sh . '_' . $imgString);
  328. $cacheKey = 'mt_goods_id_' . $hasGoodsId;
  329. $cacheStr = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  330. if ($cacheStr == $currentStr) {
  331. //商品没有更新
  332. return Json::encode(['data' => 'ok']);
  333. } else {
  334. Yii::$app->redis->executeCommand('SET', [$cacheKey, $currentStr]);
  335. }
  336. noticeUtil::push("美团修改商品,通知我们,商品图片:" . $imgString, '15280215347');
  337. $shopImg = [];
  338. $cover = '';
  339. $imgArr = explode(',', $imgString);
  340. if (!empty($imgArr)) {
  341. foreach ($imgArr as $imgUrl) {
  342. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  343. $shopImg[] = $shortUrl;
  344. if ($cover == '') {
  345. $cover = $shortUrl;
  346. }
  347. }
  348. }
  349. $hasGoods->price = $goodsPrice;
  350. $hasGoods->name = $goodsName;
  351. $hasGoods->cover = $cover;
  352. $hasGoods->sh = $sh;
  353. $hasGoods->shopImg = json_encode($shopImg);
  354. $hasGoods->save();
  355. $transaction->commit();
  356. return Json::encode(['data' => 'ok']);
  357. } catch (\Exception $e) {
  358. $transaction->rollBack();
  359. $msg = $e->getMessage();
  360. noticeUtil::push("美团修改商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  361. return Json::encode(['data' => 'ok']);
  362. }
  363. }
  364. //已确认订单通知
  365. public function actionConfirmOrder()
  366. {
  367. //error_reporting(E_ALL);
  368. $post = Yii::$app->request->post();
  369. $mtOrderId = $post['order_id'] ?? 0;
  370. $thirdSn = $post['day_seq'] ?? 0;
  371. //4秒内不允许第三方重复通知
  372. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  373. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  374. if (!empty($has)) {
  375. Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  376. return Json::encode(['data' => 'ok']);
  377. }
  378. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  379. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  380. if (empty($post)) {
  381. return Json::encode(['data' => 'ok']);
  382. }
  383. Yii::info('confirmOrderData: ' . json_encode($post));
  384. $connection = Yii::$app->db;//事务处理
  385. $transaction = $connection->beginTransaction();
  386. try {
  387. $shopId = $post['app_poi_code'] ?? '';
  388. $shop = ShopClass::getById($shopId, true);
  389. if (empty($shop)) {
  390. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  391. return Json::encode(['data' => 'ok']);
  392. }
  393. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  394. if (empty($shopExt)) {
  395. noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  396. return Json::encode(['data' => 'ok']);
  397. }
  398. $mainId = $shop->mainId ?? 0;
  399. $sjId = $shop->sjId ?? 0;
  400. $customId = $shop->defaultCustomId ?? 0;
  401. if (empty($customId)) {
  402. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  403. return Json::encode(['data' => 'ok']);
  404. }
  405. $custom = CustomClass::getById($customId);
  406. if (empty($custom)) {
  407. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  408. return Json::encode(['data' => 'ok']);
  409. }
  410. foreach ($post as $k => $val) {
  411. $post[$k] = urldecode($val);
  412. }
  413. MtOrderClass::add($post);
  414. $detail = $post['detail'] ?? '';
  415. if (empty($detail)) {
  416. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  417. return Json::encode(['data' => 'ok']);
  418. }
  419. $mtGoodsData = json_decode($detail, true);
  420. if (empty($mtGoodsData)) {
  421. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  422. return Json::encode(['data' => 'ok']);
  423. }
  424. Yii::info('商品信息:' . json_encode($mtGoodsData));
  425. $productList = [];
  426. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  427. if (empty($mtCategory)) {
  428. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  429. }
  430. $catId = $mtCategory->id ?? 0;
  431. if (empty($catId)) {
  432. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  433. return Json::encode(['data' => 'ok']);
  434. }
  435. $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : '';
  436. $remark = $caution;
  437. $bookMobile = $post['order_phone_number'] ?? '';
  438. $receiveMobile = $post['recipient_phone'] ?? '';
  439. $receiveUserName = $post['recipient_name'] ?? '';
  440. $fullAddress = $post['recipient_address'] ?? '';
  441. $cover = '';
  442. $goodsNum = 0;
  443. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  444. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  445. if (empty($mtGoodsSpu)) {
  446. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  447. $mtGoodsSpu = $mtGoods['app_food_code'];
  448. }
  449. }
  450. $goodsPrice = $mtGoods['price'] ?? 0;
  451. $num = $mtGoods['quantity'] ?? 0;
  452. $goodsNum = bcadd($num, $goodsNum);
  453. $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  454. if (empty($currentGoods)) {
  455. noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  456. return Json::encode(['data' => 'ok']);
  457. }
  458. $id = $currentGoods->id ?? 0;
  459. $unitType = 0;
  460. $property = dict::getDict('property', 'goods');
  461. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  462. $productList = array_merge($goods, $productList);
  463. }
  464. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  465. if (empty($poi_receive_detail_yuan)) {
  466. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  467. util::stop();
  468. }
  469. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  470. if (is_array($poi_receive_detail_yuan_data) == false) {
  471. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  472. util::stop();
  473. }
  474. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  475. if ($onlinePayment == 0) {
  476. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  477. util::stop();
  478. }
  479. $modifyPrice = $onlinePayment;
  480. //技术服务费
  481. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  482. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  483. //履约服务费
  484. $performanceServiceFee = 0;
  485. if (!empty($reconciliationExtras)) {
  486. $extArr = json_decode($reconciliationExtras, true);
  487. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  488. }
  489. //运费
  490. $sendCost = $post['shipping_fee'] ?? 0;
  491. //打包费
  492. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  493. //服务费和手续费
  494. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  495. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  496. $reachPeriod = date("H:i", $estimate_arrival_time);
  497. $reachDate = date("Y-m-d", $estimate_arrival_time);
  498. $reachTime = $estimate_arrival_time;
  499. $orderData = [
  500. 'product' => $productList,
  501. 'fromType' => dict::getDict('fromType', 'mt'),
  502. 'labourCost' => $labourCost,
  503. 'sendCost' => $sendCost,
  504. 'shopId' => $shopId,
  505. 'mainId' => $mainId,
  506. 'sjId' => $sjId,
  507. 'modifyPrice' => $modifyPrice,
  508. 'serviceFee' => $serviceFee,
  509. 'thirdSn' => $thirdSn,
  510. 'cover' => $cover,
  511. 'reachTime' => $reachTime,
  512. 'reachDate' => $reachDate,
  513. 'reachPeriod' => $reachPeriod,
  514. 'remark' => $remark,
  515. 'goodsNum' => $goodsNum,
  516. 'bookMobile' => $bookMobile,
  517. 'receiveMobile' => $receiveMobile,
  518. 'receiveUserName' => $receiveUserName,
  519. 'fullAddress' => $fullAddress,
  520. 'thirdOrderId' => $mtOrderId,
  521. ];
  522. $hasPay = dict::getDict('hasPay', 'payed');
  523. $order = OrderService::createFinishOrder($orderData, $custom, $hasPay);
  524. $transaction->commit();
  525. //新制作单提示
  526. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  527. ShopExtClass::newWorkRemind($shopExt, $order);
  528. return Json::encode(['data' => 'ok']);
  529. } catch (\Exception $e) {
  530. $transaction->rollBack();
  531. $msg = $e->getMessage();
  532. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  533. return Json::encode(['data' => 'ok']);
  534. }
  535. }
  536. //取消订单
  537. public function actionCancelOrder()
  538. {
  539. $post = Yii::$app->request->post();
  540. $mtOrderId = $post['order_id'] ?? 0;
  541. $shopId = $post['app_poi_code'] ?? '';
  542. if (empty($post)) {
  543. return Json::encode(['data' => 'ok']);
  544. }
  545. Yii::info('cancelOrderData: ' . json_encode($post));
  546. $connection = Yii::$app->db;//事务处理
  547. $transaction = $connection->beginTransaction();
  548. try {
  549. //4秒内不允许第三方重复通知
  550. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  551. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  552. if (!empty($has)) {
  553. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  554. return Json::encode(['data' => 'ok']);
  555. }
  556. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  557. $mt = dict::getDict('fromType', 'mt');
  558. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  559. if (empty($order)) {
  560. //没有接单时取消订单
  561. return Json::encode(['data' => 'ok']);
  562. }
  563. //订单取消,金额全部退还
  564. $shop = ShopClass::getById($shopId, true);
  565. if (empty($shop)) {
  566. return Json::encode(['data' => 'ok']);
  567. }
  568. $sjId = $shop->sjId ?? 0;
  569. $mainId = $shop->mainId ?? 0;
  570. $post = [];
  571. $post['price'] = $order->actPrice ?? 0;
  572. $post['refundType'] = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
  573. $post['id'] = $order->id ?? 0;
  574. $post['shopId'] = $shopId;
  575. $post['sjId'] = $sjId;
  576. $post['shopAdminId'] = 0;
  577. $post['mainId'] = $mainId;
  578. $post['shopAdminName'] = '美团APP';
  579. $orderSn = $order->orderSn ?? '';
  580. $orderGoods = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  581. $productData = [];
  582. if (!empty($orderGoods)) {
  583. foreach ($orderGoods as $goodsItem) {
  584. $productData[] = ['productId' => $goodsItem->goodsId, 'num' => $goodsItem->num, 'unitPrice' => $goodsItem->unitPrice, 'property' => 0,];
  585. }
  586. }
  587. $orderItem = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  588. if (!empty($orderItem)) {
  589. foreach ($orderItem as $singe) {
  590. $productData[] = ['productId' => $singe->itemId, 'num' => $singe->num, 'unitPrice' => $singe->unitPrice, 'property' => 1,];
  591. }
  592. }
  593. $post['product'] = $productData;
  594. HdRefundService::addRefund($post, $order);
  595. $transaction->commit();
  596. //制作单取消通知
  597. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  598. ShopExtClass::orderCancelRemind($shopExt, $order);
  599. return Json::encode(['data' => 'ok']);
  600. } catch (\Exception $e) {
  601. $transaction->rollBack();
  602. $msg = $e->getMessage();
  603. noticeUtil::push("取消报错了,美团订单id:{$mtOrderId} ,错误信息:{$msg}", '15280215347');
  604. return Json::encode(['data' => 'ok']);
  605. }
  606. }
  607. //推送已支付订单
  608. public function actionNewOrder()
  609. {
  610. return Json::encode(['data' => 'ok']);
  611. }
  612. //部分退款
  613. public function actionPartRefund()
  614. {
  615. return Json::encode(['data' => 'ok']);
  616. }
  617. //全部退款
  618. public function actionAllRefund()
  619. {
  620. return Json::encode(['data' => 'ok']);
  621. }
  622. }