返回列表 发帖
查看: 2083|回复: 1

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

64

主题

407

回帖

589

积分

应用开发者

贡献
10 点
金币
26 个
QQ
发表于 2023-5-20 15:01:41 | 显示全部楼层 |阅读模式
QQ20230520-145519@2x.png QQ20230520-145740@2x.png

最近在做discuz的api插件,用来对接小程序和app。目前有个问题,就是插入帖子的图片能显示,但是没有插入的附件无法显示,获取附件的代码如下,请教下怎么才能获取到发帖时所有上传的图片附件(包括没有插入帖子的)
  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. }
复制代码


⭐️推荐插件
Uniapp小程序和APP
所有插件打包

⭐️推荐模板
美化APP手机版
小米社区
回复

使用道具 举报

64

主题

407

回帖

589

积分

应用开发者

贡献
10 点
金币
26 个
QQ
 楼主| 发表于 2023-5-20 15:41:03 | 显示全部楼层
已解决 ,处理帖子信息的问题,我一直在找获取附件的问题。
  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. }
复制代码
⭐️推荐插件
Uniapp小程序和APP
所有插件打包

⭐️推荐模板
美化APP手机版
小米社区
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 关注公众号
  • 有偿服务微信
  • 有偿服务QQ

手机版|小黑屋|Discuz! 官方交流社区 ( 皖ICP备16010102号 |皖公网安备34010302002376号 )|网站地图|star

GMT+8, 2024-5-15 14:41 , Processed in 0.036797 second(s), 8 queries , Redis On.

Powered by Discuz! W1.0 Licensed

Cpoyright © 2001-2024 Discuz! Team.

关灯 在本版发帖
有偿服务QQ
有偿服务微信
返回顶部
快速回复 返回顶部 返回列表