News.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://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 think\admin\Controller;
  17. use think\admin\service\AdminService;
  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\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function index()
  39. {
  40. $this->title = '微信图文列表';
  41. $this->_query($this->table)->where(['is_deleted' => '0'])->order('id desc')->page();
  42. }
  43. /**
  44. * 图文列表数据处理
  45. * @param array $data
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. protected function _index_page_filter(&$data)
  51. {
  52. foreach ($data as &$vo) {
  53. $vo = MediaService::instance()->news($vo['id']);
  54. }
  55. }
  56. /**
  57. * 图文选择器
  58. * @auth true
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function select()
  64. {
  65. $this->index();
  66. }
  67. /**
  68. * 列表数据处理
  69. * @param array $data
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. protected function _select_page_filter(&$data)
  75. {
  76. foreach ($data as &$vo) {
  77. $vo = MediaService::instance()->news($vo['id']);
  78. }
  79. }
  80. /**
  81. * 添加微信图文
  82. * @auth true
  83. * @throws \think\db\exception\DbException
  84. */
  85. public function add()
  86. {
  87. if ($this->request->isGet()) {
  88. $this->title = '新建图文';
  89. $this->fetch('form');
  90. } else {
  91. $update = [
  92. 'create_by' => AdminService::instance()->getUserId(),
  93. 'article_id' => $this->_buildArticle($this->request->post('data', [])),
  94. ];
  95. if ($this->app->db->name($this->table)->insert($update) !== false) {
  96. $this->success('图文添加成功!', 'javascript:history.back()');
  97. } else {
  98. $this->error('图文添加失败,请稍候再试!');
  99. }
  100. }
  101. }
  102. /**
  103. * 编辑微信图文
  104. * @auth true
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public function edit()
  110. {
  111. $this->id = $this->request->get('id');
  112. if (empty($this->id)) $this->error('参数错误,请稍候再试!');
  113. if ($this->request->isGet()) {
  114. if ($this->request->get('output') === 'json') {
  115. $this->success('获取数据成功!', MediaService::instance()->news($this->id));
  116. } else {
  117. $this->title = '编辑图文';
  118. $this->fetch('form');
  119. }
  120. } else {
  121. $ids = $this->_buildArticle($this->request->post('data', []));
  122. [$map, $data] = [['id' => $this->id], ['article_id' => $ids]];
  123. if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
  124. $this->success('图文更新成功!', 'javascript:history.back()');
  125. } else {
  126. $this->error('图文更新失败,请稍候再试!');
  127. }
  128. }
  129. }
  130. /**
  131. * 图文更新操作
  132. * @param array $data
  133. * @param array $ids
  134. * @return string
  135. * @throws \think\db\exception\DbException
  136. */
  137. private function _buildArticle($data, $ids = [])
  138. {
  139. foreach ($data as $vo) {
  140. if (empty($vo['digest'])) {
  141. $vo['digest'] = mb_substr(strip_tags(str_replace(["\s", ' '], '', $vo['content'])), 0, 120);
  142. }
  143. $vo['create_at'] = date('Y-m-d H:i:s');
  144. if (empty($vo['id'])) {
  145. $result = $id = $this->app->db->name('WechatNewsArticle')->insertGetId($vo);
  146. } else {
  147. $id = intval($vo['id']);
  148. $result = $this->app->db->name('WechatNewsArticle')->where('id', $id)->update($vo);
  149. }
  150. if ($result !== false) array_push($ids, $id);
  151. }
  152. return join(',', $ids);
  153. }
  154. /**
  155. * 删除微信图文
  156. * auth true
  157. * @throws \think\db\exception\DbException
  158. */
  159. public function remove()
  160. {
  161. $this->_delete($this->table);
  162. }
  163. }