News.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\controller;
  14. use controller\BasicAdmin;
  15. use service\DataService;
  16. use service\LogService;
  17. use service\WechatService;
  18. use think\Db;
  19. use think\Log;
  20. use think\response\View;
  21. /**
  22. * 微信图文管理
  23. * Class News
  24. * @package app\wechat\controller
  25. * @author Anyon <zoujingli@qq.com>
  26. * @date 2017/03/27 14:43
  27. */
  28. class News extends BasicAdmin
  29. {
  30. /**
  31. * 设置默认操作表
  32. * @var string
  33. */
  34. public $table = 'WechatNews';
  35. /**
  36. * 图文列表
  37. */
  38. public function index()
  39. {
  40. $this->assign('title', '图文列表');
  41. $db = Db::name($this->table)->where('is_deleted', '0')->order('id desc');
  42. return parent::_list($db);
  43. }
  44. /**
  45. * 图文选择器
  46. * @return string
  47. */
  48. public function select()
  49. {
  50. return $this->index();
  51. }
  52. /**
  53. * 媒体资源显示
  54. * @return array
  55. */
  56. public function image()
  57. {
  58. $_GET['rows'] = 18;
  59. $this->assign('field', $this->request->get('field', 'local_url'));
  60. return $this->_list(Db::name('WechatNewsMedia')->where('type', 'image'));
  61. }
  62. /**
  63. * 图文列表数据处理
  64. * @param $data
  65. */
  66. protected function _index_data_filter(&$data)
  67. {
  68. foreach ($data as &$vo) {
  69. $vo = WechatService::getNewsById($vo['id']);
  70. }
  71. }
  72. /**
  73. * 图文列表数据处理
  74. * @param $data
  75. */
  76. protected function _select_data_filter(&$data)
  77. {
  78. foreach ($data as &$vo) {
  79. $vo = WechatService::getNewsById($vo['id']);
  80. }
  81. }
  82. /**
  83. * 添加图文
  84. * @return View
  85. */
  86. public function add()
  87. {
  88. if ($this->request->isGet()) {
  89. return view('form', ['title' => '新建图文']);
  90. }
  91. if ($this->request->isPost()) {
  92. $data = $this->request->post();
  93. if (($ids = $this->_apply_news_article($data['data'])) && !empty($ids)) {
  94. $post = ['article_id' => $ids, 'create_by' => session('user.id')];
  95. if (DataService::save($this->table, $post, 'id') !== false) {
  96. $this->success('图文添加成功!', '');
  97. }
  98. }
  99. $this->error('图文添加失败,请稍候再试!');
  100. }
  101. }
  102. /**
  103. * 编辑图文
  104. * @return View
  105. */
  106. public function edit()
  107. {
  108. $id = $this->request->get('id', '');
  109. if ($this->request->isGet()) {
  110. empty($id) && $this->error('参数错误,请稍候再试!');
  111. return view('form', ['title' => '编辑图文', 'vo' => WechatService::getNewsById($id)]);
  112. }
  113. $data = $this->request->post();
  114. $ids = $this->_apply_news_article($data['data']);
  115. if (!empty($ids)) {
  116. $post = ['id' => $id, 'article_id' => $ids, 'create_by' => session('user.id')];
  117. if (false !== DataService::save('wechat_news', $post, 'id')) {
  118. $this->success('图文更新成功!', '');
  119. }
  120. }
  121. $this->error('图文更新失败,请稍候再试!');
  122. }
  123. /**
  124. * 图文更新操作
  125. * @param array $data
  126. * @param array $ids
  127. * @return string
  128. */
  129. protected function _apply_news_article($data, $ids = [])
  130. {
  131. foreach ($data as &$vo) {
  132. $vo['create_by'] = session('user.id');
  133. $vo['create_at'] = date('Y-m-d H:i:s');
  134. $vo['digest'] = empty($vo['digest']) ? mb_substr(strip_tags(str_replace(["\s", ' '], '', $vo['content'])), 0, 120) : $vo['digest'];
  135. if (empty($vo['id'])) {
  136. $result = $id = Db::name('WechatNewsArticle')->insertGetId($vo);
  137. } else {
  138. $id = intval($vo['id']);
  139. $result = Db::name('WechatNewsArticle')->where('id', $id)->update($vo);
  140. }
  141. if ($result !== false) {
  142. $ids[] = $id;
  143. }
  144. }
  145. return join(',', $ids);
  146. }
  147. /**
  148. * 删除用户
  149. */
  150. public function del()
  151. {
  152. if (DataService::update($this->table)) {
  153. $this->success("图文删除成功!", '');
  154. }
  155. $this->error("图文删除失败, 请稍候再试!");
  156. }
  157. /**
  158. * 推荐图文
  159. * @return array
  160. */
  161. public function push()
  162. {
  163. # 获取将要推送的粉丝列表
  164. switch (strtolower($this->request->get('action', ''))) {
  165. case 'getuser':
  166. if ('' === ($params = $this->request->post('group', ''))) {
  167. return ['code' => 'SUCCESS', 'data' => []];
  168. }
  169. list($ids, $db) = [explode(',', $params), Db::name('WechatFans')];
  170. !in_array('0', $ids) && $db->where("concat(',',tagid_list,',') REGEXP '," . join(',|,', $ids) . ",'");
  171. return ['code' => "SUCCESS", 'data' => $db->where('subscribe', '1')->limit(200)->column('nickname')];
  172. default :
  173. $news_id = $this->request->get('id', '');
  174. // 显示及图文
  175. $newsinfo = WechatService::getNewsById($news_id);
  176. // Get 请求,显示选择器界面
  177. if ($this->request->isGet()) {
  178. $fans_tags = Db::name('WechatFansTags')->select();
  179. array_unshift($fans_tags, [
  180. 'id' => 0, 'name' => '全部',
  181. 'count' => Db::name('WechatFans')->where('subscribe', '1')->count(),
  182. ]);
  183. return view('push', ['vo' => $newsinfo, 'fans_tags' => $fans_tags]);
  184. }
  185. // Post 请求,执行图文推送操作
  186. $post = $this->request->post();
  187. empty($post['fans_tags']) && $this->error('还没有选择要粉丝对象!');
  188. // 图文上传操作
  189. !$this->_uploadWechatNews($newsinfo) && $this->error('图文上传失败,请稍候再试!');
  190. // 数据拼装
  191. $data = [];
  192. if (in_array('0', $post['fans_tags'])) {
  193. $data['msgtype'] = 'mpnews';
  194. $data['filter'] = ['is_to_all' => true];
  195. $data['mpnews'] = ['media_id' => $newsinfo['media_id']];
  196. } else {
  197. $data['msgtype'] = 'mpnews';
  198. $data['filter'] = ['is_to_all' => false, 'tag_id' => join(',', $post['fans_tags'])];
  199. $data['mpnews'] = ['media_id' => $newsinfo['media_id']];
  200. }
  201. $wechat = load_wechat('Receive');
  202. if (false !== $wechat->sendGroupMassMessage($data)) {
  203. LogService::write('微信管理', "图文[{$news_id}]推送成功");
  204. $this->success('微信图文推送成功!', '');
  205. }
  206. $this->error("微信图文推送失败,{$wechat->errMsg} [{$wechat->errCode}]");
  207. }
  208. }
  209. /**
  210. * 上传永久图文
  211. * @param array $newsinfo
  212. * @return bool
  213. */
  214. private function _uploadWechatNews(&$newsinfo)
  215. {
  216. foreach ($newsinfo['articles'] as &$article) {
  217. $article['thumb_media_id'] = WechatService::uploadForeverMedia($article['local_url']);
  218. $article['content'] = preg_replace_callback("/<img(.*?)src=['\"](.*?)['\"](.*?)\/?>/i", function ($matches) {
  219. $src = WechatService::uploadImage($matches[2]);
  220. return "<img{$matches[1]}src=\"{$src}\"{$matches[3]}/>";
  221. }, htmlspecialchars_decode($article['content']));
  222. }
  223. $wechat = load_wechat('media');
  224. // 如果已经上传过,先删除之前的历史记录
  225. !empty($newsinfo['media_id']) && $wechat->delForeverMedia($newsinfo['media_id']);
  226. // 上传图文到微信服务器
  227. $result = $wechat->uploadForeverArticles(['articles' => $newsinfo['articles']]);
  228. if (isset($result['media_id'])) {
  229. $newsinfo['media_id'] = $result['media_id'];
  230. return Db::name('WechatNews')->where('id', $newsinfo['id'])->setField('media_id', $result['media_id']);
  231. }
  232. Log::error("上传永久图文失败, {$wechat->errMsg}[{$wechat->errCode}]");
  233. return false;
  234. }
  235. }