久久久久av_欧美日韩一区二区在线_国产精品三区四区_日韩中字在线

Discuz! 官方交流社區(qū)

標題: 如何獲取discuz帖子的所有附件信息? [打印本頁]

作者: qzuser1212    時間: 2023-5-20 15:01
標題: 如何獲取discuz帖子的所有附件信息?
(, 下載次數: 58) (, 下載次數: 12)

最近在做discuz的api插件,用來對接小程序和app。目前有個問題,就是插入帖子的圖片能顯示,但是沒有插入的附件無法顯示,獲取附件的代碼如下,請教下怎么才能獲取到發(fā)帖時所有上傳的圖片附件(包括沒有插入帖子的)
  1. $attachments = array();
  2. if($attachtags) {
  3.     $attachs = C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$post['tid'], 'aid', array_keys($attachtags));
  4.     $attachs = $attachs ? $attachs : array();
  5.     foreach($attachs as $attach) {
  6.         if($attach['isimage']) {
  7.             $attach['url'] = $_G['siteurl'] . 'data/attachment/forum/' . $attach['attachment'];

  8.             $attachments[$attach['aid']] = $attach;
  9.         }
  10.     }
  11. }
復制代碼



作者: qzuser1212    時間: 2023-5-20 15:41
已解決 ,處理帖子信息的問題,我一直在找獲取附件的問題。
  1. // 獲取帖子內容
  2. $post = C::t('forum_post')->fetch_threadpost_by_tid_invisible($tid);

  3. // 獲取所有附件的信息
  4. $attachments = array();
  5. $used_attachments = array();
  6. $attachs = C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$post['tid'], 'tid', $post['tid']);
  7. $attachs = $attachs ? $attachs : array();
  8. foreach($attachs as $attach) {
  9.     if($attach['isimage']) {
  10.         $attach['url'] = $_G['siteurl'] . 'data/attachment/forum/' . $attach['attachment'];

  11.         $attachments[$attach['aid']] = $attach;
  12.     }
  13. }

  14. // 處理帖子內容
  15. $post['message'] = preg_replace_callback("/\[attach\](\d+)\[\/attach\]/i", function($matches) use ($attachments, &$used_attachments) {
  16.     $aid = $matches[1];
  17.     if(isset($attachments[$aid])) {
  18.         // 記錄已經被引用的附件
  19.         $used_attachments[$aid] = $aid;
  20.         return '<img src="'.$attachments[$aid]['url'].'" />'; // 你需要根據你的情況來修改這里的代碼,使其返回正確的圖片鏈接
  21.     } else {
  22.         return '';
  23.     }
  24. }, $post['message']);

  25. // 附加未被引用的附件到帖子內容的末尾
  26. foreach ($attachments as $aid => $attachment) {
  27.     if (!isset($used_attachments[$aid])) {
  28.         $post['message'] .= '<img src="'.$attachment['url'].'" />';
  29.     }
  30. }
復制代碼





歡迎光臨 Discuz! 官方交流社區(qū) (http://www.9999xn.com/) Powered by Discuz! W1.0