示例#1
0
        private void btn_login_Click(object sender, EventArgs e)
        {
            if (reqireLogin)
            {
                string print_address = txt_print_address.Text.Trim();
                if (!CheckTools.isUrl(print_address))
                {
                    MessageBoxEx.Show(this, "服务地址须为有效域名或IP", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (print_address.EndsWith("/"))
                {
                    print_address = print_address.Substring(0, print_address.Length - 1);
                }
                string company_code = txt_company_code_port.Text.Trim();
                if (company_code.Length < 3)
                {
                    MessageBoxEx.Show(this, "公司代码不允许为空", "提示", MessageBoxButtons.OK);
                    return;
                }
                string login_name = txt_login_name.Text.Trim();
                if (login_name.Length < 3)
                {
                    MessageBoxEx.Show(this, "登录账号不允许为空", "提示", MessageBoxButtons.OK);
                    return;
                }
                string login_password = txt_login_password.Text.Trim();
                if (login_password.Length < 3)
                {
                    MessageBoxEx.Show(this, "账号密码不允许为空", "提示", MessageBoxButtons.OK);
                    return;
                }
                StaticUtil.PRINT_ADDRESS = print_address;
                Dictionary <string, string> parameters = new Dictionary <string, string>(3);
                parameters.Add("companyCode", company_code);
                parameters.Add("loginName", login_name);
                parameters.Add("loginPassword", login_password);
                string result = null;
                result = HttpRequestClientUtil.doPost(StaticUtil.PRINT_ADDRESS + "/user/login", StaticUtil.DEFAULT_CHARSET, 60000, null, parameters, null);
                if (null == result)
                {
                    MessageBoxEx.Show(this, "登录超时,请检查网络", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    Result <UserCookieBean> data = JsonConvert.DeserializeObject <Result <UserCookieBean> >(result);
                    if (data.success && data.bizErrCode == 20000)
                    {
                        StaticUtil.USER_COOKIE_BEAN = data.data;
                        string domain = StaticUtil.getDomainByPrintAddress(StaticUtil.PRINT_ADDRESS);

                        Encoding encoding     = Encoding.GetEncoding(StaticUtil.DEFAULT_CHARSET);
                        Cookie   userIdCookie = new Cookie("user_id", HttpUtility.UrlEncode(StaticUtil.USER_COOKIE_BEAN.userId.ToString(), encoding), "/", domain);
                        StaticUtil.USER_COOKIE_BEAN.Cookies.Add(userIdCookie);

                        Cookie companyCodeCookie = new Cookie("company_code", HttpUtility.UrlEncode(StaticUtil.USER_COOKIE_BEAN.companyCode, encoding), "/", domain);
                        StaticUtil.USER_COOKIE_BEAN.Cookies.Add(companyCodeCookie);

                        Cookie loginNameCookie = new Cookie("login_name", HttpUtility.UrlEncode(StaticUtil.USER_COOKIE_BEAN.loginName, encoding), "/", domain);
                        StaticUtil.USER_COOKIE_BEAN.Cookies.Add(loginNameCookie);

                        Cookie userNameCookie = new Cookie("user_name", HttpUtility.UrlEncode(StaticUtil.USER_COOKIE_BEAN.userName, encoding), "/", domain);
                        StaticUtil.USER_COOKIE_BEAN.Cookies.Add(userNameCookie);

                        Cookie cookie_sessionCookie = new Cookie("cookie_session", HttpUtility.UrlEncode(StaticUtil.USER_COOKIE_BEAN.cookie_session, encoding), "/", domain);
                        StaticUtil.USER_COOKIE_BEAN.Cookies.Add(cookie_sessionCookie);

                        Ini ini = new Ini(Application.StartupPath + Path.DirectorySeparatorChar + "user.ini");
                        ini.Write("user", "address", StaticUtil.PRINT_ADDRESS);
                        ini.Write("user", "company", FingerUtil.Encrypt(company_code, key));
                        ini.Write("user", "name", FingerUtil.Encrypt(login_name, key));
                        ini.Write("user", "password", FingerUtil.Encrypt(login_password, key));

                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBoxEx.Show(this, data.defaultRequestFailShow(), "提示", MessageBoxButtons.OK);
                    }
                }
            }
            else
            {
                string address = txt_print_address.Text.Trim();
                if (!CheckTools.isIp(address))
                {
                    MessageBoxEx.Show(this, "请输入本地打印服务器地址且必须为IP格式", "提示", MessageBoxButtons.OK);
                    return;
                }
                string ports = txt_company_code_port.Text.Trim();
                if (!CheckTools.isPositiveNumber(ports))
                {
                    ports = "27027";
                }
                int port = int.Parse(ports);
                if (port <= 1024 || port > 65535)
                {
                    MessageBoxEx.Show(this, "请输入手机打印服务器端口号,有效范围1025~65535", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
        }