Update News.php

This commit is contained in:
Anyon 2019-12-27 13:30:29 +08:00
parent 34b46fda0a
commit 974d20aa19

View File

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