using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using V5Shop.Entity;
using V5Shop.Data;
using System.IO;
using V5Shop.Config.Provider;
using V5Shop.Config;

public partial class InfoCenter_DZNT_UserApi : System.Web.UI.Page
{
    private BaseConfigInfo BaseConfig;
    private bool api;

    protected void Page_Load(object sender, EventArgs e)
    {
        //获取DZ!NT整合判断标志
        api = GetApiFlag();
        //生成API验证码
        string app = "DZNT";
        string code = MD5("v5shopv5mall");
        //获取DZ!NT通行证
        BaseConfig = BaseConfigInfoProvider.GetRealBaseConfig();
        string kftconfigpath = Server.MapPath(BaseConfig.VirtualPath + "config/ApiConfig.config");
        string[] kftconfigcontent = File.ReadAllText(kftconfigpath, Encoding.UTF8).Split('$');
        
        //如果开启则跳转整合页面
        if (api)
            Response.Redirect("../InfoCenter/InfoCenterApi.aspx?ic_typ=script&ic_key=User&ic_code=" + code + "&ic_app=" + app + "&dz1=" + kftconfigcontent[0] + "&dz2=" + kftconfigcontent[1] + "&dz3=" + kftconfigcontent[3] + "&" + collectQuery().Replace("user_name", "username"));
    }


    /// <summary>
    /// 获取是否整合NT标志
    /// </summary>
    /// <returns></returns>
    private bool GetApiFlag()
    {
        BaseConfig = BaseConfigInfoProvider.GetRealBaseConfig();
        string kftconfigpath = Server.MapPath(BaseConfig.VirtualPath + "config/ApiConfig.config");
        if (File.Exists(kftconfigpath))
        {
            string[] kftconfigcontent = File.ReadAllText(kftconfigpath, Encoding.UTF8).Split('$');
            if (kftconfigcontent[2] == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 合并传递的QUERYSTRING字符串
    /// </summary>
    /// <returns></returns>
    private string collectQuery()
    {
        StringBuilder queryString = new StringBuilder();
        int i = 0;

        foreach (string query in Request.QueryString)
        {
            if (i == 0)
            {
                queryString.Append(query + "=" + HttpUtility.HtmlEncode(Request.QueryString[query]));
                i = 1;
            }
            else
            {
                queryString.Append("&" + query + "=" + HttpUtility.HtmlEncode(Request.QueryString[query]));
            }
        }

        return queryString.ToString();
    }


    /// <summary> 
    /// MD5 加密函数 
    /// </summary> 
    /// <param name="str"></param> 
    /// <param name="code"></param> 
    /// <returns></returns> 
    public static string MD5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }
}
