Take.php 10 KB

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