MtController.php 7.5 KB

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