MtController.php 31 KB

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