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

返回列表 發(fā)帖
查看: 3650|回復(fù): 1

如何獲取discuz帖子的所有附件信息?

71

主題

465

回帖

687

積分

應(yīng)用開發(fā)者

貢獻
14 點
金幣
26 個
QQ
樓主
發(fā)表于 2023-5-20 15:01:41 | 只看樓主 |只看大圖 |倒序瀏覽 |閱讀模式


最近在做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. }
復(fù)制代碼


回復(fù)

使用道具 舉報

71

主題

465

回帖

687

積分

應(yīng)用開發(fā)者

貢獻
14 點
金幣
26 個
QQ
沙發(fā)
 樓主| 發(fā)表于 2023-5-20 15:41:03 | 只看Ta
已解決 ,處理帖子信息的問題,我一直在找獲取附件的問題。
  1. // 獲取帖子內(nèi)容
  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. // 處理帖子內(nèi)容
  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.         // 記錄已經(jīng)被引用的附件
  19.         $used_attachments[$aid] = $aid;
  20.         return '<img src="'.$attachments[$aid]['url'].'" />'; // 你需要根據(jù)你的情況來修改這里的代碼,使其返回正確的圖片鏈接
  21.     } else {
  22.         return '';
  23.     }
  24. }, $post['message']);

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

  • 關(guān)注公眾號
  • 有償服務(wù)微信
  • 有償服務(wù)QQ

手機版|小黑屋|Discuz! 官方交流社區(qū) ( 皖I(lǐng)CP備16010102號 |皖公網(wǎng)安備34010302002376號 )|網(wǎng)站地圖|star

GMT+8, 2025-7-1 15:50 , Processed in 0.058741 second(s), 13 queries , Redis On.

Powered by Discuz! W1.0 Licensed

Cpoyright © 2001-2025 Discuz! Team.

關(guān)燈 在本版發(fā)帖
有償服務(wù)QQ
有償服務(wù)微信
返回頂部
快速回復(fù) 返回頂部 返回列表