一、背景

因为去年掌上论坛的app、小程序和API都开源了,解决了之前想开发移动端Dz应用而没有API的痛点。故最近打算抽空用uin-app做一下Dz的移动端开发。
Discuz!掌上论坛APP 源码免费下载体验

研究的API的时候发现获取主题列表的API只能获取到帖子的作者、标题及附件列表等信息,但是没有帖子的简介(即帖子内容的前几句话)。

这将使得用户不能在点进帖子前了解帖子内容,用户体验较差,故决定修改API源码使之支持获取帖子简介。

二、代码

在小程序源码的 /config.js 文件中找到了 获取帖子列表的API为 ${indexUrl}module=forumguide&view=newthread&version=5.

故可以得知该API的源码是 站点根目录/source/plugin/mobile/api/5/forumguide.php 文件:

<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: hotthread.php 34314 2014-02-20 01:04:24Z nemohou $
 */

if(!defined('IN_MOBILE_API')) {
	exit('Access Denied');
}

$_GET['mod'] = 'guide';
$_GET['view'] = isset($_GET['view']) ? $_GET['view'] : 'hot';
include_once 'forum.php';

class mobile_api {

	function common() {
	}

	function output() {
		global $_G;

		foreach($GLOBALS['data'][$_GET['view']]['threadlist'] as $tid=>$thread) {


			//获取message和附件
			$post = array_pop(C::t('forum_post')->fetch_all_by_tid('tid:'.$thread['tid'], $thread['tid'], true, '', 0, 0, 1));
			$attachlist = C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$thread['tid'], 'pid', $post['pid']);

			foreach($attachlist as $aid => $attach) {

				if($attach['remote']) {
					$attach['attachment'] = $_G['setting']['ftp']['attachurl'].'forum/'.$attach['attachment'];
				} else {
					$attach['attachment'] = strpos($_G['setting']['attachurl'], 'http') !== false ? $_G['setting']['attachurl'].'forum/'.$attach['attachment'] : $_G['siteurl'].$_G['setting']['attachurl'].'forum/'.$attach['attachment'];
				}

				$attach['thumb'] = mobile_core::thumb($attach['aid'], '0', '268', '380');
				$type = '';
				$fileext = addslashes(strtolower(substr(strrchr($attach['filename'], '.'), 1, 10)));
				if(in_array($fileext, array('jpg', 'jpeg', 'gif', 'png', 'bmp'))) {
					$type = 'image';
				} elseif($fileext === 'mp3') {
					$type = 'audio';
				} elseif($fileext === 'mp4') {
					$type = 'video';
				}
				$attach['type'] = $type;
				$attachlist[$aid] = $attach;
			}

			$GLOBALS['data'][$_GET['view']]['threadlist'][$tid]['attachlist'] = array_values($attachlist);

			$GLOBALS['data'][$_GET['view']]['threadlist'][$tid]['avatar'] = avatar($thread['authorid'], 'middle', true);


		}

		$GLOBALS['data'][$_GET['view']]['threadlist'] = $GLOBALS['data'][$_GET['view']]['threadlist'] ? $GLOBALS['data'][$_GET['view']]['threadlist'] : array();

		$variable = array(
			'data' => array_values($GLOBALS['data'][$_GET['view']]['threadlist']),
			'perpage' => $GLOBALS['perpage'],
		);
		mobile_core::result(mobile_core::variable($variable));
	}

}

?>

阅读发现$post 是查询主题表获得的数组,但是其仅仅只是为了获取帖子的附件,而没有返回帖子简介,故从$post 下手,在返回信息中构造帖子简介。

在第 59 行处插入如下代码

	// 获取主题缩略信息
	if (!$thread['price']) {
		// 不是付费主题,裁切帖子内容返回
		$GLOBALS['data'][$_GET['view']]['threadlist'][$tid]['message'] = mb_strcut(preg_replace('/\s+/', '', $post['message']), 0,100,'utf-8');
	} else {
		// 付费主题不反回帖子内容
		$GLOBALS['data'][$_GET['view']]['threadlist'][$tid]['message'] = diconv('***付费帖内容隐藏***', 'UTF-8');
	}

测试发现获取简介成功,故记录之。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐