1 新建一个空白项目,引入Discuz.Cache.dll(缓存),Discuz.Common.dll(公用类),Discuz.Config.dll(文件配置类),Discuz.Data.dll(数据访问类),Discuz.Entity.dll(实体类),
Discuz.Forum.dll
2.新建UserPop类,加入自己代码。
Code
1
using System;
2
using System.IO;
3
using System.Text;
4
using System.Data;
5
using System.Data.SqlClient;
6
using Discuz.Common;
7
using Discuz.Forum;
8
using Discuz.Data;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace Izhufan
11![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
13
/// Discuz!NT2.1 用户排行插件
14
/// </summary>
15
public class UserPop
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
18
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
20
/// 返回当天的用户排行的xhtml
21
/// </summary>
22
/// <returns>xhtml会员列表字符串</returns>
23
public static string GetUserList()
24![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
//如果缓存文件存在则直接返回文件内容
34
if (File.Exists(cacheFilepath))
35![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
using(StreamReader strReader = new StreamReader(cacheFilepath, Encoding.UTF8))
37![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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 //缓存文件不存在则创建缓存文件
47![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
//清理缓存文件
49
ClearCacheFile(cacheFiledir);
50
//创建缓存文件并返回当日排名前10位用户列表
51
return CreateBirthdayCacheFile(cacheFilepath);
52
}
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有方法#region 私有方法
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
59
/// 清理缓存文件, 如果缓存目录不存在则创建
60
/// </summary>
61
/// <param name="cacheFiledir">目录</param>
62
private static void ClearCacheFile(string cacheFiledir)
63![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
if (Directory.Exists(cacheFiledir))
65![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
DirectoryInfo dirinfo = new DirectoryInfo(cacheFiledir);
67
foreach (FileInfo file in dirinfo.GetFiles())
68![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
if (file != null && file.Extension == ".config")
70![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
file.Delete();
72
}
73
}
74
}
75
else
76![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
Utils.CreateDir(cacheFiledir);
78
}
79
80
}
81
82![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
83
/// 产生当天用户排行的xhtml代码的缓存文件并返回文件内容
84
/// </summary>
85
/// <param name="cacheFilepath">缓存文件的物理路径</param>
86
/// <returns>文件内容</returns>
87
private static string CreateBirthdayCacheFile(string cacheFilepath)
88![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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)
92![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
while (reader.Read())
94![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
}
102
}
103
reader.Close();
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
string strCache = strUsers.ToString();
108
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
using (FileStream fs = new FileStream(cacheFilepath, FileMode.Create,FileAccess.ReadWrite, FileShare.ReadWrite))
111![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
Byte[] info = System.Text.Encoding.UTF8.GetBytes(strCache);
113
fs.Write(info, 0, info.Length);
114
fs.Close();
115
}
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
return strCache;
118
119
}
120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
#endregion
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
124
}
125
}
126
3. 把编译好的dll文件放入论坛文件夹bin中。
4. 在你要用页加入命名空间<%namespace namespacename%>和使用类{ UserPop.GetUserList()
}
5.在后台重新生成模版,就可以看到效果了。。![](https://www.cnblogs.com/Emoticons/baimantou/202015214.gif)