Discuz!nt插件开发简单实现

1 新建一个空白项目,引入Discuz.Cache.dll(缓存),Discuz.Common.dll(公用类),Discuz.Config.dll(文件配置类),Discuz.Data.dll(数据访问类),Discuz.Entity.dll(实体类),

Discuz.Forum.dll

2.新建UserPop类,加入自己代码。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.IO;
  3using System.Text;
  4using System.Data;
  5using System.Data.SqlClient;
  6using Discuz.Common;
  7using Discuz.Forum;
  8using Discuz.Data;
  9
 10namespace Izhufan
 11ExpandedBlockStart.gifContractedBlock.gif{
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 13    /// Discuz!NT2.1 用户排行插件
 14    /// </summary>

 15    public class UserPop
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 17      
 18        
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 20        /// 返回当天的用户排行的xhtml
 21        /// </summary>
 22        /// <returns>xhtml会员列表字符串</returns>

 23        public static string GetUserList()
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 25            //缓存文件目录为: 论坛目录/cache/plugin/birthday/
 26            string cacheFiledir = Utils.GetMapPath(BaseConfigFactory.GetForumPath + "cache/plugin/userlist/");
 27            //缓存文件名
 28            string cacheFilename = string.Format("{0}-{1}-{2}.config", DateTime.Now.Year.ToString(), DateTime.Now.Month, DateTime.Now.Day.ToString());
 29            //缓存文件完整路径=目录+文件名
 30            string cacheFilepath = cacheFiledir + cacheFilename;
 31            
 32
 33            //如果缓存文件存在则直接返回文件内容
 34            if (File.Exists(cacheFilepath))
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 36                using(StreamReader strReader = new StreamReader(cacheFilepath, Encoding.UTF8))
 37ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 38                    System.Text.StringBuilder strOutput = new System.Text.StringBuilder();
 39                
 40                    strOutput.Append(strReader.ReadToEnd());
 41                    strReader.Close();
 42                    return strOutput.ToString();
 43                }

 44                
 45            }

 46            else //缓存文件不存在则创建缓存文件
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 48                //清理缓存文件
 49                ClearCacheFile(cacheFiledir);
 50                //创建缓存文件并返回当日排名前10位用户列表
 51                return CreateBirthdayCacheFile(cacheFilepath);
 52            }

 53        }

 54
 55
 56ContractedSubBlock.gifExpandedSubBlockStart.gif        私有方法#region 私有方法
 57
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 59        /// 清理缓存文件, 如果缓存目录不存在则创建
 60        /// </summary>
 61        /// <param name="cacheFiledir">目录</param>

 62        private static void ClearCacheFile(string cacheFiledir)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 64            if (Directory.Exists(cacheFiledir))
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 66                DirectoryInfo dirinfo = new DirectoryInfo(cacheFiledir);
 67                foreach (FileInfo file in dirinfo.GetFiles())
 68ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 69                    if (file != null && file.Extension == ".config")
 70ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 71                        file.Delete();
 72                    }

 73                }

 74            }

 75            else
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 77                Utils.CreateDir(cacheFiledir);
 78            }

 79            
 80        }

 81        
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 83        /// 产生当天用户排行的xhtml代码的缓存文件并返回文件内容
 84        /// </summary>
 85        /// <param name="cacheFilepath">缓存文件的物理路径</param>
 86        /// <returns>文件内容</returns>

 87        private static string CreateBirthdayCacheFile(string cacheFilepath)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 89            StringBuilder strUsers = new StringBuilder();
 90            IDataReader reader = DbHelper.ExecuteReader(CommandType.Text, "select top 10 uid,username,nickname,posts,credits from [" + BaseConfigFactory.GetTablePrefix + "users]  order by credits desc");
 91            if (reader != null)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 93                while (reader.Read())
 94ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 95                        strUsers.Append("<tr>");                    
 96                       // strUsers.Append("<td class=\"order\">" + i.ToString() + "</td>");                    
 97                        strUsers.Append("<td class=\"topicUser\">" + reader["username"].ToString() + "</td>");
 98                        strUsers.Append("<td class=\"ckickCount\">" + reader["credits"].ToString() + "</td>");
 99                        strUsers.Append("<td class=\"ckickCount\">" + reader["posts"].ToString() + "</td></tr>");
100
101                    }

102                }

103                reader.Close();
104
105            
106
107            string strCache = strUsers.ToString();
108         
109
110            using (FileStream fs = new FileStream(cacheFilepath, FileMode.Create,FileAccess.ReadWrite, FileShare.ReadWrite))
111ExpandedSubBlockStart.gifContractedSubBlock.gif            {
112                Byte[] info = System.Text.Encoding.UTF8.GetBytes(strCache);
113                fs.Write(info, 0, info.Length);
114                fs.Close();
115            }

116
117            return strCache;
118            
119        }

120
121        #endregion

122
123        
124    }

125}

126

3. 把编译好的dll文件放入论坛文件夹bin中。

4. 在你要用页加入命名空间<%namespace namespacename%>和使用类{ UserPop.GetUserList()

}

5.在后台重新生成模版,就可以看到效果了。。

转载于:https://www.cnblogs.com/lgh/archive/2008/09/24/1297613.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Discuz! 是一款流行的论坛系统,支持开发,以下是一个简单开发实例: 1. 创建件目录 在 Discuz! 的 plugin 目录下创建一个新的目录,例如 myplugin。 2. 创建件文件 在 myplugin 目录下创建一个新的文件,例如 myplugin.inc.php。这个文件是件的主文件,需要包含以下代码: ``` <?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } class plugin_myplugin { // 件代码 } ?> ``` 3. 注册件 在 myplugin.inc.php 文件中,使用以下代码注册件: ``` <?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } class plugin_myplugin { function __construct() { // 注册件 global $_G; $_G['cache']['plugin']['myplugin'] = array( 'name' => '我的件', 'version' => '1.0', 'description' => '这是我的第一个 Discuz! 件', 'author' => '我的名字', 'pluginurl' => '', 'url' => '', 'lang' => '', 'modules' => array() ); } } ?> ``` 4. 实现件功能 在件的主文件中,实现件的功能。例如,在 myplugin.inc.php 文件中加入以下代码: ``` <?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } class plugin_myplugin { function __construct() { // 注册件 global $_G; $_G['cache']['plugin']['myplugin'] = array( 'name' => '我的件', 'version' => '1.0', 'description' => '这是我的第一个 Discuz! 件', 'author' => '我的名字', 'pluginurl' => '', 'url' => '', 'lang' => '', 'modules' => array() ); } function global_footer() { // 在页面底部添加一段文本 echo '<div>这是我的第一个 Discuz! 件</div>'; } } ?> ``` 这段代码将在页面底部添加一段文本,展示件的功能。 5. 安装件 将 myplugin 目录上传到 Discuz! 的 plugin 目录下,然后在后台件管理页面中安装件即可。 这是一个简单Discuz! 开发实例,你可以根据自己的需求,实现不同的件功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值