News.php 5.2 KB

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