MtController.php 21 KB

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