using LYFZ.Weixin.SDK;
using LYFZ.WXLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LYFZ.WeixinServers.WeiXinAPP
{
    public partial class GetServerAddress : System.Web.UI.Page
    {
        LYFZ.WeixinServiceDate.DAL.DAL_CustomerInterfaces cusDal = new WeixinServiceDate.DAL.DAL_CustomerInterfaces();
        protected void Page_Load(object sender, EventArgs e)
        {
            string signature = Request["signature"];
            string timestamp = Request["timestamp"];// yyyyMMddhhmmss 
            string nonce = Request["nonce"];
            if (Request.HttpMethod == "GET")
            {
                string JMGDomainName = "";
                if (Request.QueryString["JMGD"] == null)
                {
                    Response.Write("如果你在浏览器中看到这句话,说明此地址可以接口的Url,请注意保持Token一致。" + LYFZ.Weixin.SDK.BasicAPI.GetSignature(timestamp, nonce, Global.lyfzToken));
                }
                else {

                    JMGDomainName = LYFZ.WinAPI.SDKSecurity.Decode(Request.QueryString["JMGD"].ToString());
                    var ent = "";
                    if (!BasicAPI.CheckSignature(signature, timestamp, nonce, Global.lyfzToken, out ent))
                    {
                        CommonHandleClass.WriteLog("POST:验证失败,非法提交");
                        WriteContent("验证失败,非法提交!");
                        return;
                    }
                    else
                    {
                        string retValue = "127.0.0.1:8082";
                        try
                        {
                            LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel = Global.GetCustomerModel(JMGDomainName);
                            if (cusModel != null && cusModel.ID > 0)
                            {
                                if (cusModel.ShellDomainName.Trim().Length > 2)
                                {
                                    if (cusModel.ShellDomainName.Trim().Contains(":"))
                                    {
                                        retValue = cusModel.ShellDomainName.Trim();
                                    }
                                    else
                                    {
                                        retValue = LYFZ.WXLibrary.CommonHandleClass.GetNetUrl(cusModel.ShellDomainName.Trim(), cusModel.PortNumber);
                                    }
                                }
                                else
                                {
                                    retValue = LYFZ.WXLibrary.CommonHandleClass.GetNetUrl(cusModel.IPAddress.Trim(), cusModel.PortNumber);
                                }
                            }
                        }
                        catch { }
                        WriteContent(retValue);
                    }
                }
               
            }
            
        }

        private void WriteContent(string str)
        {
            Response.Output.Write(str);
        }
    }
}