MtController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. } else {
  67. $shopImg = [];
  68. foreach ($imgArr as $imgUrl) {
  69. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  70. $shopImg[] = $shortUrl;
  71. }
  72. $cover = $shopImg[0];
  73. $goodsData = [
  74. 'spu' => $mtGoodsSpu,
  75. 'shopImg' => $shopImg,
  76. 'name' => $name,
  77. 'sjId' => $sjId,
  78. 'mainId' => $mainId,
  79. 'shopId' => $shopId,
  80. 'catId' => $catId,
  81. 'cover' => $cover,
  82. 'unitGoodsPrice' => $price,
  83. ];
  84. GoodsClass::orderCreateGoods($goodsData);
  85. }
  86. }
  87. }
  88. }