Take.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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."' />";
  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. }
  80. $goodsIds = array_diff($spu_id_list, $goods_id_list);
  81. $this->remove($goodsIds);
  82. return 'sussess';
  83. } catch (Exception $e) {
  84. Log::error('同步数据失败:' . $e->getMessage());
  85. echo '供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】';
  86. return 'error';
  87. }
  88. }
  89. public function seveImg($goods_id, $img_url, $targetDir)
  90. {
  91. if(!$img_url) return $img_url;
  92. if (is_array($img_url)){
  93. $new_img_url = [];
  94. foreach ($img_url as $value) {
  95. $new_img_url[] = $this->seveImgUrl($goods_id, $value, $targetDir);
  96. }
  97. }else{
  98. $new_img_url = $this->seveImgUrl($goods_id, $img_url, $targetDir);
  99. }
  100. return $new_img_url;
  101. }
  102. public function seveImgUrl($goods_id, $remoteImageUrl, $targetDir)
  103. {
  104. // 目标路径(确保目录存在)
  105. $targetDirTow = rand(1000, 10000). $this->upImgName(basename($remoteImageUrl));
  106. $targetPath = $targetDir . $targetDirTow; // 使用basename获取文件名
  107. // 检查目标目录是否存在,如果不存在则创建
  108. if (!is_dir($targetDir)) {
  109. mkdir($targetDir, 0777, true); // 第二个参数是权限,第三个参数是递归创建
  110. }
  111. // 获取远程图片内容
  112. $imageContent = file_get_contents($remoteImageUrl);
  113. // 检查是否成功获取内容
  114. if ($imageContent !== false) {
  115. // 保存图片到指定路径
  116. if (file_put_contents($targetPath, $imageContent)) {
  117. $new_img_url = env('APP_URL').'/images/gong/'.$goods_id.'/'. $targetDirTow;
  118. return $new_img_url;
  119. } else {
  120. echo "保存图片时发生错误。";
  121. return $remoteImageUrl;
  122. }
  123. } else {
  124. echo "无法获取远程图片内容。";
  125. return $remoteImageUrl;
  126. }
  127. }
  128. public function upImgName($filename)
  129. {
  130. $position = strpos($filename, '?');
  131. if ($position !== false) {
  132. $filename = substr($filename, 0, $position);
  133. }
  134. return $filename;
  135. }
  136. public function deleteFilesInDirectory($dir) {
  137. if (!is_dir($dir)) return true;
  138. // 打开目录
  139. $files = scandir($dir);
  140. // 遍历目录中的所有文件
  141. foreach ($files as $file) {
  142. // 如果是文件(排除 . 和 ..)
  143. if ($file !== '.' && $file !== '..') {
  144. $filePath = $dir . '/' . $file;
  145. // 删除文件
  146. if (is_file($filePath)) {
  147. unlink($filePath);
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. public function add($data)
  154. {
  155. $findData = Db::name('douhuomall')->field('id,spu_id,status')->where('spu_id', $data['spu_id'])->find();
  156. try {
  157. $data['create_time'] = date('Y-m-d H:i:s');
  158. $data['update_time'] = date('Y-m-d H:i:s');
  159. $skuInfo = $this->arrSort($data['skuId_info'], 'cost_price', SORT_ASC);
  160. $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
  161. $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
  162. $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
  163. $data['mer_profit'] = isset($skuInfo[0]['mer_profit']) ? $skuInfo[0]['mer_profit'] : 0;
  164. $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
  165. $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['market_price'], $data['cost_price'], 2);
  166. if (empty($findData)) {
  167. $data['skuId_info'] = json_encode($data['skuId_info']);
  168. $data['spuId_info'] = json_encode($data['spuId_info']);
  169. Db::name('douhuomall')->insert($data);
  170. } else {
  171. Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([
  172. 'status' => $data['status'],
  173. 'create_time' => date('Y-m-d H:i:s'),
  174. 'update_time' => date('Y-m-d H:i:s'),
  175. 'cost_price' => $data['cost_price'],
  176. 'market_price' => $data['market_price'],
  177. 'ot_price' => $data['ot_price'],
  178. 'mer_profit' => $data['mer_profit'],
  179. 'pension' => $data['pension'],
  180. 'profit' => $data['profit'],
  181. 'skuId_info' => json_encode($data['skuId_info']),
  182. 'spuId_info' => json_encode($data['spuId_info']),
  183. 'title' => $data['title'],
  184. 'cate_ids' => $data['cate_ids'],
  185. 'cxb_cate_id' => $data['cxb_cate_id']
  186. ]);
  187. }
  188. return true;
  189. } catch (Exception $e) {
  190. echo '供应链产品添加:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】';
  191. return false;
  192. }
  193. }
  194. public function remove($goodsIds)
  195. {
  196. Db::startTrans();
  197. try{
  198. // 删除选品库对应的数据
  199. Db::name('douhuomall')->whereIn('spu_id', $goodsIds)->update([
  200. 'status' => 3,
  201. 'update_time' => date('Y-m-d H:i:s')
  202. ]);
  203. // 删除商品管理对应的数据
  204. Db::name('store_product')->whereIn('spu_id', $goodsIds)->update([
  205. 'is_del' => 1,
  206. 'is_show' => 0
  207. ]);
  208. Db::commit();
  209. return true;
  210. } catch (Exception $e) {
  211. Db::rollback();
  212. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  213. return false;
  214. }
  215. }
  216. /**
  217. * 二维数组根据某个字段排序
  218. * @param array $array 要排序的数组
  219. * @param string $keys 要排序的键字段
  220. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  221. * @return array 排序后的数组
  222. */
  223. function arrSort($array, $keys, $sort = SORT_DESC) {
  224. $keysValue = [];
  225. foreach ($array as $k => $v) {
  226. $keysValue[$k] = $v[$keys];
  227. }
  228. array_multisort($keysValue, $sort, $array);
  229. return $array;
  230. }
  231. }