Take.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace app\controller\api\douhuomall;
  3. use app\traits\GongApiRequest;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Log;
  7. class Take
  8. {
  9. use GongApiRequest;
  10. public function get_goods_list(){
  11. try {
  12. set_time_limit(0);
  13. $allData = [];
  14. $page = 1;
  15. $limit = 100;
  16. $hasNextPage = true; // 是否有下一页的标志
  17. while ($hasNextPage) {
  18. $data = GongApiRequest::get_goods_list($page, $limit);
  19. if (!$data) {
  20. break; // 如果请求失败,停止循环
  21. }
  22. $allData = array_merge($allData, $data['list']); // 数据在list键下
  23. if (!count($data['list']) || count($data['list']) < $limit) {
  24. $hasNextPage = false; // 如果没有更多页面,停止循环
  25. } else {
  26. $page++; // 增加页码数
  27. }
  28. }
  29. $goods_id_list = [];
  30. $spu_id_list = Db::name('douhuomall')->column('spu_id');
  31. // 同步到数据库
  32. foreach ($allData as $key=>$value) {
  33. sleep(1);
  34. // if($key != 0) continue;
  35. $goodsInfo = GongApiRequest::get_goods_info($value['goods_id']);
  36. if(!$goodsInfo) continue;
  37. //图片存放路径
  38. $targetDir = public_path() . 'images/gong/'.$value['goods_id'].'/';
  39. $this->deleteFilesInDirectory($targetDir);
  40. //spuId_info商品详细信息处理
  41. $goodsInfo['cover_url'] = $this->seveImg($value['goods_id'], $goodsInfo['main_img'], $targetDir);
  42. $goodsInfo['spu_id'] = $goodsInfo['goods_id'];
  43. $goodsInfo['detail'] = "";
  44. $detail_img = $this->seveImg($value['goods_id'], $goodsInfo['detail_img'], $targetDir);
  45. foreach($detail_img as $img_url){
  46. $img_url = "<img src='".$img_url."' referrerpolicy='no-referrer' />";
  47. $goodsInfo['detail'] = $goodsInfo['detail'].$img_url;
  48. }
  49. //sku信息处理
  50. $skuInfo = $goodsInfo['sku_list'];
  51. foreach ($skuInfo as $k=>$v) {
  52. $skuInfo[$k]['img_url'] = $this->seveImg($value['goods_id'], $v['main_img'], $targetDir);//规格图片
  53. $skuInfo[$k]['cost_price'] = bcadd($v['plat_price'], bcmul($v['plat_price'], 0.03, 2),2);//成本价格 原来进价的基础上加百分之3
  54. $skuInfo[$k]['market_price'] = bcadd($skuInfo[$k]['cost_price'], bcmul($skuInfo[$k]['cost_price'], 0.2, 2),2);//销售价格 售价在成本价格的基础上加百分20
  55. $skuInfo[$k]['profit'] = bcdiv($skuInfo[$k]['market_price'], $skuInfo[$k]['cost_price'], 2);
  56. $skuInfo[$k]['mer_profit'] = bcsub($skuInfo[$k]['market_price'], $skuInfo[$k]['cost_price'], 2);
  57. $skuInfo[$k]['yanglaojin'] = '0.00';
  58. if(count($v['attr'])){
  59. $skuInfo[$k]['attribute_json'] = $v['attr'];
  60. }else{
  61. $skuInfo[$k]['attribute_json'] = [['val'=> '默认', 'name'=> '默认']];
  62. }
  63. }
  64. unset($goodsInfo['sku_list']);
  65. $seveData = [
  66. 'spu_id' => $goodsInfo['spu_id'],
  67. 'skuId_info' => $skuInfo,
  68. 'sys_id' => '112',
  69. 'goods_id' => $goodsInfo['goods_id'],
  70. 'spuId_info' => $goodsInfo,
  71. 'sku_id' => $skuInfo[0]['sku_id'] ? $skuInfo[0]['sku_id'] : 0,
  72. 'status' => $goodsInfo['status'],
  73. 'cate_ids' => $goodsInfo['cate_ids'],
  74. 'title' => $goodsInfo['spu_name'],
  75. 'cxb_cate_id' => 0,
  76. ];
  77. $this->add($seveData);
  78. $goods_id_list[] = $goodsInfo['spu_id'];
  79. $this->updata_product_img($goodsInfo['spu_id']);
  80. }
  81. $goodsIds = array_diff($spu_id_list, $goods_id_list);
  82. $this->remove($goodsIds);
  83. return 'sussess';
  84. } catch (Exception $e) {
  85. Log::error('供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】');
  86. echo '供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】';
  87. Db::name('log')->insert(['data' => '供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】']);
  88. return 'error';
  89. }
  90. }
  91. public function updata_product_img($spu_id){
  92. $product_id = Db::name('store_product')->where('spu_id',$spu_id)->value('product_id');
  93. if(!$product_id) return true;
  94. $data = Db::name('douhuomall')->where('spu_id',$spu_id)->find();
  95. //商品主图
  96. $spuId_info=json_decode($data['spuId_info'],true);
  97. $slider_image=$spuId_info['detail_img_list']??'';
  98. $image=$spuId_info['cover_url']??'';
  99. try {
  100. $slider_image = implode(',',json_decode($slider_image,true));
  101. }catch (\Exception $exception){
  102. $slider_image = $image;
  103. }
  104. //更新产品图片数据
  105. $product_insert_data=[
  106. 'image'=>$image,
  107. 'slider_image'=>$slider_image,
  108. ];
  109. Db::name('store_product')->where('product_id',$product_id)->update($product_insert_data);
  110. echo '更新产品图片数据成功'.json_encode($product_insert_data);
  111. $detail = $spuId_info['detail'];
  112. if(!is_null(json_decode($detail))){
  113. $detail_list = json_decode($detail);
  114. $detail = '';
  115. foreach($detail_list as $value){
  116. $detail .= "<img src='".$value."' referrerpolicy='no-referrer' /></img>";
  117. }
  118. }
  119. Db::name('store_product_content')->where('product_id',$product_id)->update(['content'=>$detail]);
  120. return true;
  121. }
  122. public function seveImg($goods_id, $img_url, $targetDir)
  123. {
  124. return $img_url;
  125. if(!$img_url) return $img_url;
  126. if (is_array($img_url)){
  127. $new_img_url = [];
  128. foreach ($img_url as $value) {
  129. $new_img_url[] = $this->seveImgUrl($goods_id, $value, $targetDir);
  130. }
  131. }else{
  132. $new_img_url = $this->seveImgUrl($goods_id, $img_url, $targetDir);
  133. }
  134. return $new_img_url;
  135. }
  136. public function seveImgUrl($goods_id, $remoteImageUrl, $targetDir)
  137. {
  138. // 目标路径(确保目录存在)
  139. $targetDirTow = rand(1000, 10000). $this->upImgName(basename($remoteImageUrl));
  140. $targetPath = $targetDir . $targetDirTow; // 使用basename获取文件名
  141. // 检查目标目录是否存在,如果不存在则创建
  142. if (!is_dir($targetDir)) {
  143. mkdir($targetDir, 0777, true); // 第二个参数是权限,第三个参数是递归创建
  144. }
  145. ini_set("max_execution_time", 2);
  146. // 获取远程图片内容
  147. $imageContent = @file_get_contents($remoteImageUrl);
  148. // 检查是否成功获取内容
  149. if ($imageContent !== false) {
  150. // 保存图片到指定路径
  151. if (file_put_contents($targetPath, $imageContent)) {
  152. $new_img_url = env('APP_URL').'/images/gong/'.$goods_id.'/'. $targetDirTow;
  153. return $new_img_url;
  154. } else {
  155. echo "保存图片时发生错误。";
  156. return $remoteImageUrl;
  157. }
  158. } else {
  159. echo "无法获取远程图片内容。";
  160. return $remoteImageUrl;
  161. }
  162. }
  163. public function upImgName($filename)
  164. {
  165. $position = strpos($filename, '?');
  166. if ($position !== false) {
  167. $filename = substr($filename, 0, $position);
  168. }
  169. return $filename;
  170. }
  171. public function deleteFilesInDirectory($dir) {
  172. if (!is_dir($dir)) return true;
  173. // 打开目录
  174. $files = scandir($dir);
  175. // 遍历目录中的所有文件
  176. foreach ($files as $file) {
  177. // 如果是文件(排除 . 和 ..)
  178. if ($file !== '.' && $file !== '..') {
  179. $filePath = $dir . '/' . $file;
  180. // 删除文件
  181. if (is_file($filePath)) {
  182. unlink($filePath);
  183. }
  184. }
  185. }
  186. return true;
  187. }
  188. public function add($data)
  189. {
  190. $findData = Db::name('douhuomall')->field('id,spu_id,status')->where('spu_id', $data['spu_id'])->find();
  191. try {
  192. $data['create_time'] = date('Y-m-d H:i:s');
  193. $data['update_time'] = date('Y-m-d H:i:s');
  194. $skuInfo = $this->arrSort($data['skuId_info'], 'cost_price', SORT_ASC);
  195. $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
  196. $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
  197. $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
  198. $data['mer_profit'] = isset($skuInfo[0]['mer_profit']) ? $skuInfo[0]['mer_profit'] : 0;
  199. $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
  200. $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['market_price'], $data['cost_price'], 2);
  201. if (empty($findData)) {
  202. $data['skuId_info'] = json_encode($data['skuId_info']);
  203. $data['spuId_info'] = json_encode($data['spuId_info']);
  204. Db::name('douhuomall')->insert($data);
  205. } else {
  206. Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([
  207. 'status' => $data['status'],
  208. 'create_time' => date('Y-m-d H:i:s'),
  209. 'update_time' => date('Y-m-d H:i:s'),
  210. 'cost_price' => $data['cost_price'],
  211. 'market_price' => $data['market_price'],
  212. 'ot_price' => $data['ot_price'],
  213. 'mer_profit' => $data['mer_profit'],
  214. 'pension' => $data['pension'],
  215. 'profit' => $data['profit'],
  216. 'skuId_info' => json_encode($data['skuId_info']),
  217. 'spuId_info' => json_encode($data['spuId_info']),
  218. 'title' => $data['title'],
  219. 'cate_ids' => $data['cate_ids'],
  220. 'cxb_cate_id' => $data['cxb_cate_id']
  221. ]);
  222. }
  223. return true;
  224. } catch (Exception $e) {
  225. echo '供应链产品添加:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】';
  226. return false;
  227. }
  228. }
  229. public function remove($goodsIds)
  230. {
  231. Db::startTrans();
  232. try{
  233. // 删除选品库对应的数据
  234. Db::name('douhuomall')->whereIn('spu_id', $goodsIds)->update([
  235. 'status' => 3,
  236. 'update_time' => date('Y-m-d H:i:s')
  237. ]);
  238. // 删除商品管理对应的数据
  239. Db::name('store_product')->whereIn('spu_id', $goodsIds)->update([
  240. // 'is_del' => 1,
  241. 'is_show' => 0
  242. ]);
  243. Db::commit();
  244. return true;
  245. } catch (Exception $e) {
  246. Db::rollback();
  247. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  248. return false;
  249. }
  250. }
  251. /**
  252. * 二维数组根据某个字段排序
  253. * @param array $array 要排序的数组
  254. * @param string $keys 要排序的键字段
  255. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  256. * @return array 排序后的数组
  257. */
  258. function arrSort($array, $keys, $sort = SORT_DESC) {
  259. $keysValue = [];
  260. foreach ($array as $k => $v) {
  261. $keysValue[$k] = $v[$keys];
  262. }
  263. array_multisort($keysValue, $sort, $array);
  264. return $array;
  265. }
  266. }