MtController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace console\controllers;
  3. use common\components\noticeUtil;
  4. use common\components\util;
  5. use yii\console\Controller;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use bizHd\goods\classes\CategoryClass;
  9. use bizHd\goods\classes\GoodsClass;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use common\components\mtUtil;
  13. use Shishaoqi\MeituanFlashPurchase\Application;
  14. class MtController extends Controller
  15. {
  16. //同步商品 ssh 20220801
  17. public function actionSyncGoods()
  18. {
  19. global $argv;
  20. ini_set('memory_limit', '2045M');
  21. set_time_limit(0);
  22. $shopId = $argv[2] ?? 0;
  23. if (empty($shopId)) {
  24. util::stop('请填写门店id');
  25. }
  26. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  27. if (empty($ext)) {
  28. util::stop('没有找到门店相信息');
  29. }
  30. $shop = ShopClass::getById($shopId, true);
  31. if (empty($shop)) {
  32. util::stop('没有找到门店');
  33. }
  34. $mainId = $shop->mainId ?? 0;
  35. $sjId = $shop->sjId ?? 0;
  36. $config = dict::getDict('mtConfig');
  37. $app = new Application($config);
  38. $accessToken = mtUtil::getAccessToken($ext);
  39. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
  40. $json = $app->retail->retailList($params);
  41. $arr = json_decode($json, true);
  42. if (isset($arr['data']) == false || empty($arr['data'])) {
  43. util::stop('没有商品');
  44. }
  45. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  46. if (empty($mtCategory)) {
  47. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  48. }
  49. $catId = $mtCategory->id ?? 0;
  50. if (empty($catId)) {
  51. util::stop('没有找到美团的商品分类');
  52. }
  53. $goodsList = $arr['data'];
  54. foreach ($goodsList as $key => $val) {
  55. $picture = $val['picture'] ?? '';
  56. $price = $val['price'] ?? 0;
  57. $name = $val['name'] ?? '';
  58. $mtGoodsSpu = $val['app_spu_code'] ?? '';
  59. $imgArr = explode(',', $picture);
  60. if (empty($mtGoodsSpu)) {
  61. noticeUtil::push('商家有商品没有spuId,' . $name, '15280215347');
  62. continue;
  63. }
  64. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $mtGoodsSpu], true);
  65. if (!empty($current)) {
  66. $shopImg = [];
  67. foreach ($imgArr as $imgUrl) {
  68. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  69. $shopImg[] = $shortUrl;
  70. }
  71. $current->shopImg = json_encode($shopImg);
  72. $current->save();
  73. } else {
  74. $shopImg = [];
  75. foreach ($imgArr as $imgUrl) {
  76. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  77. $shopImg[] = $shortUrl;
  78. }
  79. $cover = $shopImg[0];
  80. $goodsData = [
  81. 'spu' => $mtGoodsSpu,
  82. 'shopImg' => $shopImg,
  83. 'name' => $name,
  84. 'sjId' => $sjId,
  85. 'mainId' => $mainId,
  86. 'shopId' => $shopId,
  87. 'catId' => $catId,
  88. 'cover' => $cover,
  89. 'unitGoodsPrice' => $price,
  90. ];
  91. GoodsClass::orderCreateGoods($goodsData);
  92. }
  93. }
  94. }
  95. //同步商品 ssh 20220801
  96. public function actionBc()
  97. {
  98. global $argv;
  99. ini_set('memory_limit', '2045M');
  100. set_time_limit(0);
  101. $shopId = $argv[2] ?? 0;
  102. if (empty($shopId)) {
  103. util::stop('请填写门店id');
  104. }
  105. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  106. if (empty($ext)) {
  107. util::stop('没有找到门店相信息');
  108. }
  109. $shop = ShopClass::getById($shopId, true);
  110. if (empty($shop)) {
  111. util::stop('没有找到门店');
  112. }
  113. $mainId = $shop->mainId ?? 0;
  114. $sjId = $shop->sjId ?? 0;
  115. $config = dict::getDict('mtConfig');
  116. $app = new Application($config);
  117. $accessToken = mtUtil::getAccessToken($ext);
  118. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
  119. $json = $app->retail->retailList($params);
  120. $arr = json_decode($json, true);
  121. if (isset($arr['data']) == false || empty($arr['data'])) {
  122. util::stop('没有商品');
  123. }
  124. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  125. if (empty($mtCategory)) {
  126. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  127. }
  128. $catId = $mtCategory->id ?? 0;
  129. if (empty($catId)) {
  130. util::stop('没有找到美团的商品分类');
  131. }
  132. $goodsList = $arr['data'];
  133. foreach ($goodsList as $key => $val) {
  134. $picture = $val['picture'] ?? '';
  135. $price = $val['price'] ?? 0;
  136. $name = $val['name'] ?? '';
  137. $mtGoodsSpu = $val['app_spu_code'] ?? '';
  138. $imgArr = explode(',', $picture);
  139. if (empty($mtGoodsSpu)) {
  140. noticeUtil::push('商家有商品没有spuId,' . $name, '15280215347');
  141. continue;
  142. }
  143. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $mtGoodsSpu], true);
  144. if (!empty($current)) {
  145. $current->price = $price;
  146. $current->save();
  147. }
  148. }
  149. }
  150. }