News.php 4.9 KB

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