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.IO;
using System.Net;

namespace InfoCenter
{
    public partial class V5Shop_UserApi : System.Web.UI.Page
    {
        private string code = string.Empty;
        private string apiUrl = string.Empty;
        private string verifyCode = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string action = GetRequest("action");
            code = GetRequest("code");
            apiUrl = GetRequest("apiurl");

            //判断V5验证码是否通过
            verifyCode = HttpUtility.UrlEncode("sdkfdsjj$#12r90dfjs@1*&()_^&0fdjs");
            apiUrl = "../../Api/V5ShopApi.aspx";  //GetTrueUrl().Replace("/InfoCenter/apilist/", "") + "/Api/V5ShopApi.aspx";
            //if (verifyCode != code)
            //    return;
            switch (action)
            {
                case "login":
                    LoginV5Shop();
                    break;
                case "logout":
                    LogoutV5Shop();
                    break;
                case "deleteuser":
                    DeleteUserV5Shop();
                    break;
                case "updatepwd":
                    UpdateUserPasswordV5Shop();
                    break;
                case "updatecredits":
                    UpdateUserScoreV5Shop();
                    break;
            }
        }

        /// <summary>
        /// 获取真实URL地址
        /// </summary>
        /// <returns></returns>
        public static string GetTrueUrl()
        {
            string TrueUrl = string.Empty;
            string TheUrl = HttpContext.Current.Request.Url.ToString();
            string[] UrlArray = TheUrl.Split('?')[0].Split('/');

            TrueUrl = TheUrl.Split('?')[0].Replace(UrlArray[UrlArray.Length - 1], "");

            return TrueUrl;
        }

        /// <summary>
        /// 同步修改会员积分
        /// </summary>
        private void UpdateUserScoreV5Shop()
        {
            //拼装请求URL
            string username = GetRequest("username");
            string score = GetRequest("score");
            string appUrl = apiUrl + "?action=updatecredits&username=" + HttpUtility.UrlEncode(username) + "&score=" + score + "&code=" + code;
            //发送同步请求
            RequestAppUrl(appUrl);
        }

        /// <summary>
        /// 同步修改会员密码
        /// </summary>
        private void UpdateUserPasswordV5Shop()
        {
            //拼装请求URL
            string username = GetRequest("username");
            string password = GetRequest("password");
            string appUrl = apiUrl + "?action=updatepwd&username=" + HttpUtility.UrlEncode(username) + "&password=" + HttpUtility.UrlEncode(password) + "&v5_code=" + verifyCode;
            //发送同步请求
            RequestAppUrl(appUrl);
        }

        /// <summary>
        /// 同步删除会员
        /// </summary>
        private void DeleteUserV5Shop()
        {
            //拼装请求URL
            string username = GetRequest("username");
            string appUrl = apiUrl + "?action=deleteuser&username=" + HttpUtility.UrlEncode(username) + "&v5_code=" + verifyCode;
            //发送同步请求
            RequestAppUrl(appUrl);
        }

        /// <summary>
        /// 同步注销会员
        /// </summary>
        private void LogoutV5Shop()
        {
            //拼装请求URL
            string appUrl = apiUrl + "?action=logout&v5_code=" + verifyCode;
            //发送同步请求
            RequestAppUrl(appUrl);
        }

        /// <summary>
        /// 同步登录会员
        /// </summary>
        private void LoginV5Shop()
        {
            //拼装请求URL
            string username = GetRequest("username");
            string password = GetRequest("password");
            string email = username + "@v5shop.com.cn";
            string appUrl = apiUrl + "?action=login&username=" + HttpUtility.UrlEncode(username) + "&password=" + HttpUtility.UrlEncode(password) + "&email=" + HttpUtility.UrlEncode(email) + "&v5_code=" + verifyCode;
            //发送同步请求
            RequestAppUrl(appUrl);
        }

        /// <summary>
        /// 获取传递过来的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string GetRequest(string key)
        {
            return Request.QueryString[key] == null ? "" : Request.QueryString[key].ToString();
        }

        /// <summary>
        /// 根据同步地址发送QUERYSTRING请求
        /// </summary>
        /// <param name="appUrl"></param>
        private void RequestAppUrl(string appUrl)
        {
            Server.Transfer(appUrl);

            //HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(appUrl);
            //myReq.BeginGetResponse(new AsyncCallback(RespCallback), new object());
        }

        /// <summary>
        /// 返回处理函数
        /// </summary>
        /// <param name="ar"></param>
        private static void RespCallback(IAsyncResult ar) { }
    }
}