Pārlūkot izejas kodu

Update News.php

Anyon 5 gadi atpakaļ
vecāks
revīzija
974d20aa19
1 mainītis faili ar 24 papildinājumiem un 27 dzēšanām
  1. 24 27
      app/wechat/controller/News.php

+ 24 - 27
app/wechat/controller/News.php

@@ -17,6 +17,7 @@ namespace app\wechat\controller;
 
 use app\wechat\service\MediaService;
 use think\admin\Controller;
+use think\admin\service\AdminService;
 
 /**
  * 微信图文管理
@@ -60,11 +61,10 @@ class News extends Controller
 
     /**
      * 图文选择器
-     * @return string
+     * @auth true
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
-     * @auth true
      */
     public function select()
     {
@@ -72,7 +72,7 @@ class News extends Controller
     }
 
     /**
-     * 图文列表数据处理
+     * 列表数据处理
      * @param array $data
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
@@ -86,10 +86,7 @@ class News extends Controller
     /**
      * 添加微信图文
      * @auth true
-     * @throws \think\Exception
-     * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
-     * @throws \think\db\exception\ModelNotFoundException
      */
     public function add()
     {
@@ -97,14 +94,15 @@ class News extends Controller
             $this->title = '新建图文';
             $this->fetch('form');
         } else {
-            $data = $this->request->post();
-            if (($ids = $this->_buildArticle($data['data'])) && !empty($ids)) {
-                if (data_save($this->table, ['article_id' => $ids, 'create_by' => session('user.id')], 'id') !== false) {
-                    $url = url('@admin') . '#' . url('@wechat/news/index') . '?spm=' . $this->request->get('spm');
-                    $this->success('图文添加成功!', $url);
-                }
+            $update = [
+                'create_by'  => AdminService::instance()->getUserId(),
+                'article_id' => $this->_buildArticle($this->request->post('data', [])),
+            ];
+            if ($this->app->db->name($this->table)->insert($update) !== false) {
+                $this->success('图文添加成功!', 'javascript:history.back()');
+            } else {
+                $this->error('图文添加失败,请稍候再试!');
             }
-            $this->error('图文添加失败,请稍候再试!');
         }
     }
 
@@ -117,23 +115,23 @@ class News extends Controller
      */
     public function edit()
     {
-        if (($this->id = $this->request->get('id')) < 1) {
-            $this->error('参数错误,请稍候再试!');
-        }
+        $this->id = $this->request->get('id');
+        if (empty($this->id)) $this->error('参数错误,请稍候再试!');
         if ($this->request->isGet()) {
             if ($this->request->get('output') === 'json') {
                 $this->success('获取数据成功!', MediaService::instance()->news($this->id));
             } else {
-                $this->fetch('form', ['title' => '编辑图文']);
+                $this->title = '编辑图文';
+                $this->fetch('form');
             }
         } else {
-            $post = $this->request->post();
-            if (isset($post['data']) && ($ids = $this->_buildArticle($post['data']))) {
-                if (data_save('wechat_news', ['id' => $this->id, 'article_id' => $ids], 'id')) {
-                    $this->success('图文更新成功!', 'javascript:history.back()');
-                }
+            $ids = $this->_buildArticle($this->request->post('data', []));
+            list($map, $data) = [['id' => $this->id], ['article_id' => $ids]];
+            if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
+                $this->success('图文更新成功!', 'javascript:history.back()');
+            } else {
+                $this->error('图文更新失败,请稍候再试!');
             }
-            $this->error('图文更新失败,请稍候再试!');
         }
     }
 
@@ -146,7 +144,7 @@ class News extends Controller
      */
     private function _buildArticle($data, $ids = [])
     {
-        foreach ($data as &$vo) {
+        foreach ($data as $vo) {
             if (empty($vo['digest'])) {
                 $vo['digest'] = mb_substr(strip_tags(str_replace(["\s", ' '], '', $vo['content'])), 0, 120);
             }
@@ -157,15 +155,14 @@ class News extends Controller
                 $id = intval($vo['id']);
                 $result = $this->app->db->name('WechatNewsArticle')->where('id', $id)->update($vo);
             }
-            if ($result !== false) {
-                array_push($ids, $id);
-            }
+            if ($result !== false) array_push($ids, $id);
         }
         return join(',', $ids);
     }
 
     /**
      * 删除微信图文
+     * auth true
      * @throws \think\db\exception\DbException
      */
     public function remove()