我们的discuz接口写好之后,就可以可以调用啦。复制我们测试返回的JSON数据,在VS2015中 编辑->选择性粘贴->将JSON粘贴为类即可。生成的代码如:
#region 板块分类
public class getForumClass
{
public int state {
get; set;
}
public ForumClass[] data {
get; set;
}
}
public class ForumClass
{
public int id {
get; set;
}
public int fup {
get; set;
}
public string name {
get; set;
}
public int px {
get; set;
}
}
#endregion
#region 帖子信息
public class getForumInfo
{
public int state {
get; set;
}
public ForumInfo[] data {
get; set;
}
}
public class ForumInfo
{
public int id {
get; set;
}
public string title {
get; set;
}
public string price {
get; set;
}
public string views {
get; set;
}
public string dateline {
get; set;
}
}
#endregion
前台操作布局:
第一步就是页面初始化需要获取BBS板块信息,我们获取的是JSON数据,需要对JSON数据进行处理,处理之后绑定下拉框即可。
JSON数据处理代码:
string strForumClass = net.PostDataGetHtml("http://域名地址/json.php", "type=getForumClass");
try {
fclass = json.DeserializeObject < getForumClass > (strForumClass);
//下拉看初始化
if (fclass.state == 10000) {
DataTable fclasstb = new DataTable();
fclasstb.Columns.Add("id", typeof(int));
fclasstb.Columns.Add("fup", typeof(int));
fclasstb.Columns.Add("name", typeof(string));
fclasstb.Columns.Add("selectname", typeof(string));
fclasstb.Columns.Add("px", typeof(int));
foreach(var item in fclass.data.Where(o = >o.fup == 0).OrderBy(o = >o.px)) {
DataRow newRow = fclasstb.NewRow();
newRow["id"] = item.id;
newRow["fup"] = item.fup;
newRow["name"] = item.name;
newRow["selectname"] = item.name;
newRow["px"] = item.px;
fclasstb.Rows.Add(newRow);
foreach(var itemch in fclass.data.Where(o = >o.fup == item.id).OrderBy(o = >o.px)) {
DataRow newRowch = fclasstb.NewRow();
newRowch["id"] = itemch.id;
newRowch["fup"] = itemch.fup;
newRowch["name"] = itemch.name;
newRowch["selectname"] = " ├" + itemch.name;
newRowch["px"] = itemch.px;
fclasstb.Rows.Add(newRowch);
}
}
shequclass.DataSource = fclasstb;
shequclass.DataTextField = "selectname";
shequclass.DataValueField = "id";
shequclass.DataBind();
} else {
shequclass.Items.Add("BBS文库分类获取失败!");
}
} catch(System.Exception ex) {
log.WriteErrorLog(ex);
}
这里需要说明一下,我们获取的是JSON数据,由于BBS论坛那边嵌套分类,所以我们在自己的项目中进行处理。
处理之后,绑定下拉框效果如图:
再就是点击按钮绑定事件了,点击导入按钮是会触发流程为:先封装数据条件,然后获取discuz的JSON数据,最后就是循环导入数据库中,导入数据库中我们根据名字做了该字段不能重复的方法。
if ( dateline_min.Text.Trim() != "" && dateline_max.Text.Trim() != "" )
{
try
{
string fid = shequclass.SelectedItem.Value;
DateTime datetime_min = DateTime.Parse( dateline_min.Text.Trim() );
DateTime datetime_max = DateTime.Parse( dateline_max.Text.Trim() );
if ( datetime_min <= datetime_max )
{
/* 内控分类 */
string strfcon = net.PostDataGetHtml( "http://域名地址/json.php", "type=getForumInfo&fid=" + fid + "&dateline_min=" + strHelp.DateTimeToUnixTimestamp( datetime_min ) + "&dateline_max=" + strHelp.DateTimeToUnixTimestamp( datetime_max ) );
try
{
fcon = json.DeserializeObject<getForumInfo>( strfcon );
/*下拉看初始化 */
if ( fcon.state == 10000 )
{
int dr_num = 0;
foreach ( var item in fcon.data.OrderBy( o => o.dateline ) )
{
string isHavetitle = "select count(*) from V1_FileCon where Fname='" + item.title + "'";
if ( sql.sqlcount( isHavetitle ) == 0 )
{
string insertThis = "insert into V1_FileCon(FID,CID,Fname,LX,FURL,FBR) values('" +
leibie1.SelectedItem.Value + "',0,'" +
item.title + "','','http://域名地址/forum.php?mod=viewthread&tid=" +
item.id + "','" +
item.price + "')";
sql.execsql( insertThis );
dr_num++;
}
}
JSBox.Alert( "查询到数据:" + fcon.data.Count() + ",成功添加:" + dr_num );
}else if ( fcon.state == 10001 )
{
JSBox.Alert( "数据不存在!" );
}else {
JSBox.Alert( "获取数据失败!" );
}
}
catch ( System.Exception ex )
{
log.WriteErrorLog( ex );
JSBox.Alert( "导入失败!" );
}
}else {
JSBox.Alert( "开始时间不能大于结束时间!" );
}
}
catch ( System.Exception ex )
{
log.WriteErrorLog( ex );
JSBox.Alert( "时间格式不正确!" );
}
}else {
JSBox.Alert( "请选择时间段!" );
return;
}
这里需要注意一下,discuz中所有的数据记录都是使用时间戳来记录的,所以我们还需要时间与时间戳转换方法:
/*
* / <summary>
* / 日期转换成unix时间戳
* / </summary>
* / <param name="dateTime"></param>
* / <returns></returns>
*/
public long DateTimeToUnixTimestamp( DateTime dateTime )
{
var start = new DateTime( 1970, 1, 1, 0, 0, 0, dateTime.Kind );
return(Convert.ToInt64( (dateTime - start).TotalSeconds ) );
}
/*
* / <summary>
* / unix时间戳转换成日期
* / </summary>
* / <param name="unixTimeStamp">时间戳(秒)</param>
* / <returns></returns>
*/
public DateTime UnixTimestampToDateTime( DateTime target, long timestamp )
{
var start = new DateTime( 1970, 1, 1, 0, 0, 0, target.Kind );
return(start.AddSeconds( timestamp ) );
}
附件方法:
#region 向网址提交参数数据
/*
* / <summary>
* / 向网址提交参数数据
* / </summary>
* / <param name="uri">网址</param>
* / <param name="postData">参数数据</param>
* / string postData = string.Format("id={0}&pwd={1}&to={2}",userid,password,phone); // 要发放的数据
* / <returns></returns>
*/
public string PostDataGetHtml( string uri, string postData )
{
try
{
byte[] data = Encoding.UTF8.GetBytes( postData );
Uri uRI = new Uri( uri );
HttpWebRequest req = WebRequest.Create( uRI ) as HttpWebRequest;
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
req.AllowAutoRedirect = true;
Stream outStream = req.GetRequestStream();
outStream.Write( data, 0, data.Length );
outStream.Close();
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
Stream inStream = res.GetResponseStream();
StreamReader sr = new StreamReader( inStream, Encoding.GetEncoding( "GBK" ) );
string htmlResult = sr.ReadToEnd();
return(htmlResult);
}
catch ( Exception ex )
{
log.WriteErrorLog( ex );
return("error");
}
}
#endregion
555
555