News.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\wechat\controller;
  15. use app\wechat\service\MediaService;
  16. use library\Controller;
  17. use think\Db;
  18. /**
  19. * 微信图文管理
  20. * Class News
  21. * @package app\wechat\controller
  22. */
  23. class News extends Controller
  24. {
  25. /**
  26. * 设置默认操作表
  27. * @var string
  28. */
  29. protected $table = 'WechatNews';
  30. /**
  31. * 微信图文管理
  32. * @auth true
  33. * @menu true
  34. * @throws \think\Exception
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. * @throws \think\exception\PDOException
  39. */
  40. public function index()
  41. {
  42. $this->title = '微信图文列表';
  43. $this->_query($this->table)->where(['is_deleted' => '0'])->order('id desc')->page();
  44. }
  45. /**
  46. * 图文列表数据处理
  47. * @param array $data
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. protected function _index_page_filter(&$data)
  53. {
  54. foreach ($data as &$vo) $vo = MediaService::news($vo['id']);
  55. }
  56. /**
  57. * 图文选择器
  58. * @return string
  59. * @auth true
  60. * @throws \think\Exception
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. * @throws \think\exception\PDOException
  65. */
  66. public function select()
  67. {
  68. $this->index();
  69. }
  70. /**
  71. * 图文列表数据处理
  72. * @param array $data
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. */
  77. protected function _select_page_filter(&$data)
  78. {
  79. foreach ($data as &$vo) $vo = MediaService::news($vo['id']);
  80. }
  81. /**
  82. * 添加微信图文
  83. * @auth true
  84. * @throws \think\Exception
  85. * @throws \think\exception\PDOException
  86. */
  87. public function add()
  88. {
  89. if ($this->request->isGet()) {
  90. $this->title = '新建图文';
  91. $this->fetch('form');
  92. } else {
  93. $data = $this->request->post();
  94. if (($ids = $this->_apply_news_article($data['data'])) && !empty($ids)) {
  95. if (data_save($this->table, ['article_id' => $ids, 'create_by' => session('user.id')], 'id') !== false) {
  96. $url = url('@admin') . '#' . url('@wechat/news/index') . '?spm=' . $this->request->get('spm');
  97. $this->success('图文添加成功!', $url);
  98. }
  99. }
  100. $this->error('图文添加失败,请稍候再试!');
  101. }
  102. }
  103. /**
  104. * 编辑微信图文
  105. * @auth true
  106. * @throws \think\Exception
  107. * @throws \think\exception\PDOException
  108. */
  109. public function edit()
  110. {
  111. if (($this->id = $this->request->get('id')) < 1) {
  112. $this->error('参数错误,请稍候再试!');
  113. }
  114. if ($this->request->isGet()) {
  115. if ($this->request->get('output') === 'json') {
  116. $this->success('获取数据成功!', MediaService::news($this->id));
  117. } else {
  118. $this->fetch('form', ['title' => '编辑图文']);
  119. }
  120. } else {
  121. $post = $this->request->post();
  122. if (isset($post['data']) && ($ids = $this->_apply_news_article($post['data']))) {
  123. if (data_save('wechat_news', ['id' => $this->id, 'article_id' => $ids], 'id')) {
  124. $this->success('图文更新成功!', url('@admin') . '#' . url('@wechat/news/index'));
  125. }
  126. }
  127. $this->error('图文更新失败,请稍候再试!');
  128. }
  129. }
  130. /**
  131. * 图文更新操作
  132. * @param array $data
  133. * @param array $ids
  134. * @return string
  135. * @throws \think\Exception
  136. * @throws \think\exception\PDOException
  137. */
  138. private function _apply_news_article($data, $ids = [])
  139. {
  140. foreach ($data as &$vo) {
  141. if (empty($vo['digest'])) {
  142. $vo['digest'] = mb_substr(strip_tags(str_replace(["\s", ' '], '', $vo['content'])), 0, 120);
  143. }
  144. $vo['create_at'] = date('Y-m-d H:i:s');
  145. if (empty($vo['id'])) {
  146. $result = $id = Db::name('WechatNewsArticle')->insertGetId($vo);
  147. } else {
  148. $id = intval($vo['id']);
  149. $result = Db::name('WechatNewsArticle')->where('id', $id)->update($vo);
  150. }
  151. if ($result !== false) {
  152. array_push($ids, $id);
  153. }
  154. }
  155. return join(',', $ids);
  156. }
  157. /**
  158. * 删除微信图文
  159. * @throws \think\Exception
  160. * @throws \think\exception\PDOException
  161. */
  162. public function remove()
  163. {
  164. $this->_delete($this->table);
  165. }
  166. }