Forráskód Böngészése

add 增加過期商品下架功能,後臺手動上架後後重置add_time,

joe 3 éve
szülő
commit
da92614d6e

+ 8 - 2
app/admin/controller/store/StoreProduct.php

@@ -91,7 +91,10 @@ class StoreProduct extends AuthController
     public function set_show($is_show = '', $id = '')
     {
         ($is_show == '' || $id == '') && Json::fail('缺少参数');
-        $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
+        $res = ProductModel::where(['id' => $id])->update([
+            'is_show' => (int)$is_show,
+            'add_time' => time(),
+        ]);
         if ($res) {
             return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
         } else {
@@ -126,7 +129,10 @@ class StoreProduct extends AuthController
         if (empty($post['ids'])) {
             return Json::fail('请选择需要上架的产品');
         } else {
-            $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
+            $res = ProductModel::where('id', 'in', $post['ids'])->update([
+                'is_show' => 1,
+                'add_time' => time(),
+            ]);
             if ($res)
                 return Json::successful('上架成功');
             else

+ 1 - 1
app/admin/model/store/StoreProduct.php

@@ -45,7 +45,7 @@ class StoreProduct extends BaseModel
 
 
     /**
-     * 获取连表MOdel
+     * 获取连表Model
      * @param $model
      * @return object
      */

+ 1 - 0
config/app.php

@@ -43,6 +43,7 @@ return [
     // 显示错误信息
     'show_error_msg'   => false,
 
+    'off_days_before'  => 30,   // 超過(n)天數下架
     // 部署的云服务器或 VPS IP 地址
     'server_ip'        => '81.70.81.74',
     // 微信提现开关

+ 30 - 1
tw/command/Maintain.php

@@ -2,9 +2,11 @@
 
 namespace tw\command;
 
+use app\admin\model\store\StoreProduct;
 use app\admin\model\store\StoreProductAttr;
 use app\admin\model\store\StoreProductAttrResult;
 use app\admin\model\store\StoreProductAttrValue;
+use app\admin\model\system\SystemLog;
 use app\models\store\StoreProductRule;
 use think\console\Command;
 use think\console\Input;
@@ -260,7 +262,25 @@ class Maintain extends Command
      */
     protected function prod()
     {
-        console::log(__FUNCTION__);
+        $now = time();
+        $days = Config::get('app.off_days_before', 30);
+        $days_ago = $now - $days * SECONDS_OF_ONEDAY;
+        // 找到过期商品
+        $products = StoreProduct::where([
+            'is_del' => 0,
+            'is_show' => 1,
+        ])->where('add_time', '<', $days_ago)
+            ->select()->toArray();
+        // 下架
+        $ids = [];
+        foreach ($products as $p) {
+            array_push($ids, $p['id']);
+        }
+        $ret = Storeproduct::where('id', 'in', $ids)->update([
+            'is_show' => 0,
+        ]);
+        // 记录日志
+        warnlog("auto off-shelves executed: $ret" . json_encode($ids));
     }
 
     /**
@@ -277,6 +297,15 @@ class Maintain extends Command
      */
     protected function backup()
     {
+        $db = config('database.connections.' . config('database.default'));
+        $user = $db['username'];
+        $password = $db['password'];
+        $host = $db['hostname'];
+        $database = $db['database'];
+        $now = time();
+        $dir = "backup-$now.sql";
+        exec("mysqldump --user={$user} --password={$password} --host={$host} {$database} --result-file={$dir} 2>&1", $output);
+        warnlog('backup database:' . json_encode($output));
     }
 
     protected function none()