MtController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace console\controllers;
  3. use common\components\noticeUtil;
  4. use common\components\util;
  5. use Yii;
  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. class MtController extends Controller
  16. {
  17. //同步商品 ssh 20220801
  18. public function actionSyncGoods()
  19. {
  20. global $argv;
  21. ini_set('memory_limit', '2045M');
  22. set_time_limit(0);
  23. $shopId = $argv[2];
  24. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  25. if (empty($ext)) {
  26. util::fail('没有找到门店相信息');
  27. }
  28. $shop = ShopClass::getById($shopId, true);
  29. if (empty($shop)) {
  30. util::fail('没有找到门店');
  31. }
  32. $mainId = $shop->mainId ?? 0;
  33. $sjId = $shop->sjId ?? 0;
  34. $config = dict::getDict('mtConfig');
  35. $app = new Application($config);
  36. $accessToken = mtUtil::getAccessToken($ext);
  37. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
  38. $json = $app->retail->retailList($params);
  39. $arr = json_decode($json, true);
  40. if (isset($arr['data']) == false || empty($arr['data'])) {
  41. util::fail('没有商品');
  42. }
  43. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  44. if (empty($mtCategory)) {
  45. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  46. }
  47. $catId = $mtCategory->id ?? 0;
  48. if (empty($catId)) {
  49. util::fail('没有找到美团的商品分类');
  50. }
  51. $goodsList = $arr['data'];
  52. foreach ($goodsList as $key => $val) {
  53. $picture = $val['picture'] ?? '';
  54. $price = $val['price'] ?? 0;
  55. $name = $val['name'] ?? '';
  56. $mtGoodsSpu = $val['app_spu_code'] ?? '';
  57. $imgArr = explode(',', $picture);
  58. if (empty($mtGoodsSpu)) {
  59. noticeUtil::push('商家有商品没有spuId,' . $name, '15280215347');
  60. continue;
  61. }
  62. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $mtGoodsSpu], true);
  63. if (!empty($current)) {
  64. } else {
  65. $shopImg = [];
  66. foreach ($imgArr as $imgUrl) {
  67. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  68. $shopImg[] = $shortUrl;
  69. }
  70. $cover = $shopImg[0];
  71. $goodsData = [
  72. 'spu' => $mtGoodsSpu,
  73. 'shopImg' => $shopImg,
  74. 'name' => $name,
  75. 'sjId' => $sjId,
  76. 'mainId' => $mainId,
  77. 'shopId' => $shopId,
  78. 'catId' => $catId,
  79. 'cover' => $cover,
  80. 'unitGoodsPrice' => $price,
  81. ];
  82. GoodsClass::orderCreateGoods($goodsData);
  83. }
  84. }
  85. }
  86. }