MtController.php 7.0 KB

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