Explorar o código

定时任务-拉取同步唯微宝商品数据-代码格式化

sunxbiao hai 1 ano
pai
achega
58e9dab586
Modificáronse 1 ficheiros con 36 adicións e 33 borrados
  1. 36 33
      app/controller/api/douhuomall/Take.php

+ 36 - 33
app/controller/api/douhuomall/Take.php

@@ -1,4 +1,5 @@
1
 <?php
1
 <?php
2
+
2
 namespace app\controller\api\douhuomall;
3
 namespace app\controller\api\douhuomall;
3
 
4
 
4
 use app\traits\GongApiRequest;
5
 use app\traits\GongApiRequest;
@@ -119,55 +120,55 @@ class Take
119
         }
120
         }
120
     }
121
     }
121
 
122
 
122
-    public function updata_product_img($spu_id){
123
+    public function updata_product_img($spu_id)
124
+    {
123
         // 1、验证该 SPUId 是否上架了 如果上架了 需要去更新商品 图片
125
         // 1、验证该 SPUId 是否上架了 如果上架了 需要去更新商品 图片
124
-        $product_id = Db::name('store_product')->where('spu_id',$spu_id)->value('product_id');
125
-        if(!$product_id) return true;
126
+        $product_id = Db::name('store_product')->where('spu_id', $spu_id)->value('product_id');
127
+        if (!$product_id) return true;
126
 
128
 
127
         // 2、查找微唯宝 商品
129
         // 2、查找微唯宝 商品
128
-        $data = Db::name('douhuomall')->where('spu_id',$spu_id)->find();
130
+        $data = Db::name('douhuomall')->where('spu_id', $spu_id)->find();
129
 
131
 
130
         // 3、获取 商品主图
132
         // 3、获取 商品主图
131
-        $spuId_info=json_decode($data['spuId_info'],true);
132
-        $slider_image=$spuId_info['detail_img_list']??'';
133
-        $image=$spuId_info['cover_url']??'';
133
+        $spuId_info = json_decode($data['spuId_info'], true);
134
+        $slider_image = $spuId_info['detail_img_list'] ?? '';
135
+        $image = $spuId_info['cover_url'] ?? '';
134
         try {
136
         try {
135
-            $slider_image = implode(',',json_decode($slider_image,true));
136
-        }catch (\Exception $exception){
137
+            $slider_image = implode(',', json_decode($slider_image, true));
138
+        } catch (\Exception $exception) {
137
             $slider_image = $image;
139
             $slider_image = $image;
138
         }
140
         }
139
 
141
 
140
         //更新产品图片数据
142
         //更新产品图片数据
141
-        $product_insert_data=[
142
-            'image'=>$image,
143
-            'slider_image'=>$slider_image,
143
+        $product_insert_data = [
144
+            'image' => $image,
145
+            'slider_image' => $slider_image,
144
         ];
146
         ];
145
-        Db::name('store_product')->where('product_id',$product_id)->update($product_insert_data);
146
-        echo '更新产品图片数据成功'.json_encode($product_insert_data);
147
+        Db::name('store_product')->where('product_id', $product_id)->update($product_insert_data);
148
+        echo '更新产品图片数据成功' . json_encode($product_insert_data);
147
         $detail = $spuId_info['detail'];
149
         $detail = $spuId_info['detail'];
148
-        if(!is_null(json_decode($detail))){
150
+        if (!is_null(json_decode($detail))) {
149
             $detail_list = json_decode($detail);
151
             $detail_list = json_decode($detail);
150
             $detail = '';
152
             $detail = '';
151
-            foreach($detail_list as $value){
152
-                $detail .= "<img src='".$value."' referrerpolicy='no-referrer' /></img>";
153
+            foreach ($detail_list as $value) {
154
+                $detail .= "<img src='" . $value . "' referrerpolicy='no-referrer' /></img>";
153
             }
155
             }
154
         }
156
         }
155
-        Db::name('store_product_content')->where('product_id',$product_id)->update(['content'=>$detail]);
157
+        Db::name('store_product_content')->where('product_id', $product_id)->update(['content' => $detail]);
156
         return true;
158
         return true;
157
     }
159
     }
158
 
160
 
159
 
161
 
160
-
161
     public function seveImg($goods_id, $img_url, $targetDir)
162
     public function seveImg($goods_id, $img_url, $targetDir)
162
     {
163
     {
163
         return $img_url;
164
         return $img_url;
164
-        if(!$img_url) return $img_url;
165
-        if (is_array($img_url)){
165
+        if (!$img_url) return $img_url;
166
+        if (is_array($img_url)) {
166
             $new_img_url = [];
167
             $new_img_url = [];
167
             foreach ($img_url as $value) {
168
             foreach ($img_url as $value) {
168
                 $new_img_url[] = $this->seveImgUrl($goods_id, $value, $targetDir);
169
                 $new_img_url[] = $this->seveImgUrl($goods_id, $value, $targetDir);
169
             }
170
             }
170
-        }else{
171
+        } else {
171
             $new_img_url = $this->seveImgUrl($goods_id, $img_url, $targetDir);
172
             $new_img_url = $this->seveImgUrl($goods_id, $img_url, $targetDir);
172
         }
173
         }
173
         return $new_img_url;
174
         return $new_img_url;
@@ -176,7 +177,7 @@ class Take
176
     public function seveImgUrl($goods_id, $remoteImageUrl, $targetDir)
177
     public function seveImgUrl($goods_id, $remoteImageUrl, $targetDir)
177
     {
178
     {
178
         // 目标路径(确保目录存在)
179
         // 目标路径(确保目录存在)
179
-        $targetDirTow = rand(1000, 10000). $this->upImgName(basename($remoteImageUrl));
180
+        $targetDirTow = rand(1000, 10000) . $this->upImgName(basename($remoteImageUrl));
180
         $targetPath = $targetDir . $targetDirTow; // 使用basename获取文件名
181
         $targetPath = $targetDir . $targetDirTow; // 使用basename获取文件名
181
 
182
 
182
         // 检查目标目录是否存在,如果不存在则创建
183
         // 检查目标目录是否存在,如果不存在则创建
@@ -193,7 +194,7 @@ class Take
193
         if ($imageContent !== false) {
194
         if ($imageContent !== false) {
194
             // 保存图片到指定路径
195
             // 保存图片到指定路径
195
             if (file_put_contents($targetPath, $imageContent)) {
196
             if (file_put_contents($targetPath, $imageContent)) {
196
-                $new_img_url = env('APP_URL').'/images/gong/'.$goods_id.'/'. $targetDirTow;
197
+                $new_img_url = env('APP_URL') . '/images/gong/' . $goods_id . '/' . $targetDirTow;
197
                 return $new_img_url;
198
                 return $new_img_url;
198
             } else {
199
             } else {
199
                 echo "保存图片时发生错误。";
200
                 echo "保存图片时发生错误。";
@@ -215,7 +216,8 @@ class Take
215
         return $filename;
216
         return $filename;
216
     }
217
     }
217
 
218
 
218
-    public function deleteFilesInDirectory($dir) {
219
+    public function deleteFilesInDirectory($dir)
220
+    {
219
         if (!is_dir($dir)) return true;
221
         if (!is_dir($dir)) return true;
220
 
222
 
221
         // 打开目录
223
         // 打开目录
@@ -262,12 +264,12 @@ class Take
262
                     && $data['market_price'] == $findData['market_price']
264
                     && $data['market_price'] == $findData['market_price']
263
                     && $data['profit'] == $findData['profit']) {
265
                     && $data['profit'] == $findData['profit']) {
264
                     $count = Db::name('store_product')
266
                     $count = Db::name('store_product')
265
-                        ->where('spu_id','=', $data['spu_id'])
266
-                        ->where('is_show','=', 0)
267
-                        ->where('is_status','=', 0)
267
+                        ->where('spu_id', '=', $data['spu_id'])
268
+                        ->where('is_show', '=', 0)
269
+                        ->where('is_status', '=', 0)
268
                         ->count();
270
                         ->count();
269
                     if ($count) {
271
                     if ($count) {
270
-                        Db::name('store_product')->where('spu_id','=', $data['spu_id'])->update([
272
+                        Db::name('store_product')->where('spu_id', '=', $data['spu_id'])->update([
271
                             'is_show' => 1
273
                             'is_show' => 1
272
                         ]);
274
                         ]);
273
                     }
275
                     }
@@ -301,7 +303,7 @@ class Take
301
     public function remove($goodsIds)
303
     public function remove($goodsIds)
302
     {
304
     {
303
         Db::startTrans();
305
         Db::startTrans();
304
-        try{
306
+        try {
305
             // 删除选品库对应的数据
307
             // 删除选品库对应的数据
306
             Db::name('douhuomall')->whereIn('spu_id', $goodsIds)->update([
308
             Db::name('douhuomall')->whereIn('spu_id', $goodsIds)->update([
307
                 'status' => 3,
309
                 'status' => 3,
@@ -325,11 +327,12 @@ class Take
325
     /**
327
     /**
326
      * 二维数组根据某个字段排序
328
      * 二维数组根据某个字段排序
327
      * @param array $array 要排序的数组
329
      * @param array $array 要排序的数组
328
-     * @param string $keys   要排序的键字段
329
-     * @param string $sort  排序类型  SORT_ASC     SORT_DESC
330
+     * @param string $keys 要排序的键字段
331
+     * @param string $sort 排序类型  SORT_ASC     SORT_DESC
330
      * @return array 排序后的数组
332
      * @return array 排序后的数组
331
      */
333
      */
332
-    function arrSort($array, $keys, $sort = SORT_DESC) {
334
+    function arrSort($array, $keys, $sort = SORT_DESC)
335
+    {
333
         $keysValue = [];
336
         $keysValue = [];
334
         foreach ($array as $k => $v) {
337
         foreach ($array as $k => $v) {
335
             $keysValue[$k] = $v[$keys];
338
             $keysValue[$k] = $v[$keys];