Discuz!nt插件开发简单实现
1 新建一个空白项目,引入Discuz.Cache.dll(缓存),Discuz.Common.dll(公用类),Discuz.Config.dll(文件配置类),Discuz.Data.dll(数据访问类),Discuz.Entity.dll(实体类),
Discuz.Forum.dll
2.新建UserPop类,加入自己代码。
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
11{
12 /**//// <summary>
13 /// Discuz!NT2.1 用户排行插件
14 /// </summary>
15 public class UserPop
16 {
17
18
19 /**//// <summary>
20 /// 返回当天的用户排行的xhtml
21 /// </summary>
22 /// <returns>xhtml会员列表字符串</returns>
23 public static string GetUserList()
24 {
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))
35 {
36 using(StreamReader strReader = new StreamReader(cacheFilepath, Encoding.UTF8))
37 {
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 {
48 //清理缓存文件
49 ClearCacheFile(cacheFiledir);
50 //创建缓存文件并返回当日排名前10位用户列表
51 return CreateBirthdayCacheFile(cacheFilepath);
52 }
53 }
54
55
56 私有方法#region 私有方法
57
58 /**//// <summary>
59 /// 清理缓存文件, 如果缓存目录不存在则创建
60 /// </summary>
61 /// <param name="cacheFiledir">目录</param>
62 private static void ClearCacheFile(string cacheFiledir)
63 {
64 if (Directory.Exists(cacheFiledir))
65 {
66 DirectoryInfo dirinfo = new DirectoryInfo(cacheFiledir);
67 foreach (FileInfo file in dirinfo.GetFiles())
68 {
69 if (file != null && file.Extension == ".config")
70 {
71 file.Delete();
72 }
73 }
74 }
75 else
76 {
77 Utils.CreateDir(cacheFiledir);
78 }
79
80 }
81
82 /**//// <summary>
83 /// 产生当天用户排行的xhtml代码的缓存文件并返回文件内容
84 /// </summary>
85 /// <param name="cacheFilepath">缓存文件的物理路径</param>
86 /// <returns>文件内容</returns>
87 private static string CreateBirthdayCacheFile(string cacheFilepath)
88 {
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 {
93 while (reader.Read())
94 {
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))
111 {
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
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
11{
12 /**//// <summary>
13 /// Discuz!NT2.1 用户排行插件
14 /// </summary>
15 public class UserPop
16 {
17
18
19 /**//// <summary>
20 /// 返回当天的用户排行的xhtml
21 /// </summary>
22 /// <returns>xhtml会员列表字符串</returns>
23 public static string GetUserList()
24 {
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))
35 {
36 using(StreamReader strReader = new StreamReader(cacheFilepath, Encoding.UTF8))
37 {
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 {
48 //清理缓存文件
49 ClearCacheFile(cacheFiledir);
50 //创建缓存文件并返回当日排名前10位用户列表
51 return CreateBirthdayCacheFile(cacheFilepath);
52 }
53 }
54
55
56 私有方法#region 私有方法
57
58 /**//// <summary>
59 /// 清理缓存文件, 如果缓存目录不存在则创建
60 /// </summary>
61 /// <param name="cacheFiledir">目录</param>
62 private static void ClearCacheFile(string cacheFiledir)
63 {
64 if (Directory.Exists(cacheFiledir))
65 {
66 DirectoryInfo dirinfo = new DirectoryInfo(cacheFiledir);
67 foreach (FileInfo file in dirinfo.GetFiles())
68 {
69 if (file != null && file.Extension == ".config")
70 {
71 file.Delete();
72 }
73 }
74 }
75 else
76 {
77 Utils.CreateDir(cacheFiledir);
78 }
79
80 }
81
82 /**//// <summary>
83 /// 产生当天用户排行的xhtml代码的缓存文件并返回文件内容
84 /// </summary>
85 /// <param name="cacheFilepath">缓存文件的物理路径</param>
86 /// <returns>文件内容</returns>
87 private static string CreateBirthdayCacheFile(string cacheFilepath)
88 {
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 {
93 while (reader.Read())
94 {
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))
111 {
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.在后台重新生成模版,就可以看到效果了。。