News.php 4.4 KB

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