通达OA简易中间件(同时向OA系统和域系统增加登录帐号)

通达OA简易中间件分成两部分:

     1. OA服务器接口PHP文件

     2. 桌面操作程序(exe)


实现原理:从OA获取所有配置信息(省去再一次配置的麻烦),然后通过桌面程序向AD域和OA分别增加帐号。


OA服务器接口PHP文件:负责把OA的数据配置、域配置、以及增加帐号所需要的基础数据回传给 桌机操作程序

PHP接口代码主要是返回配置的数据(部分代码),在桌面程序通过获取的HTML文件,使用正则解释 <option value=值>文本</option>。

<?php
if($TYPE == "gender")
{
    echo "<option value=\"0\">男</option>";
    echo "<option value=\"1\">女</option>";
}
?>


桌面操作程序(exe):使用C#开发

在填写了OA的访问地址后,如 http://192.168.56.101:81/

窗体使用了异步加载的方法,好较好的用户体验。

初始化,使用代理实现异步操作

            //禁用状态
            gbBasicInfo.Enabled = false;
            bgOtherInfo.Enabled = false;
            btnClose.Enabled = false;
            btnSubmit.Enabled = false;
            lbMessage.Visible = true;

            //异步处理加载配置数据
            //包括域地址、MySQL帐号等
            TransmitDelegate transmit = Transmit;
            IAsyncResult asyncResult = transmit.BeginInvoke(new AccountInfo(), TransmitCallBack, transmit);

信息加载主要代码

void Transmit(AccountInfo state)
        {
            #region 获取OA接口地址
            //OA地址判断是否有效
            m_oaHttpUrl = FileHelper.ReadFileByFullname(m_config);
            if (!Regex.IsMatch(m_oaHttpUrl, @"^http://([^/]+)(/?){1}quot;))
            {
                frmOAInterface frm = new frmOAInterface(m_config);
                frm.ShowDialog(this);
                frm.ShowInTaskbar = false;

                if (frm.DialogResult == DialogResult.OK)
                {
                    m_oaHttpUrl = frm.OAHttpUrl;
                }
                else
                {
                    Application.Exit();
                }
            }

            //追加接口地址
            if (m_oaHttpUrl.Substring(m_oaHttpUrl.Length - 1) != "/")
            {
                m_oaHttpUrl += "/";
            }
            m_oaHttpUrl += "OAMidHelper.php";
            #endregion

            #region 初始化
            Spider spider = new Spider();
            string html = "";
            MatchCollection mc = null;
            IList<OptionValue> ovs = null;
            #endregion

            try
            {
                //省略部分代码
                #region 获取性别
                SetLableMessageValue("获取性别中...");
                html = spider.GetFullHtml(m_oaHttpUrl + "?TYPE=gender", "GBK", null);

                ovs = new List<OptionValue>();
                mc = m_optionRegex.Matches(html);
                if (mc.Count == 0)
                {
                    throw new Exception("无法获取性别数据,请重试。");
                }
                foreach (Match item in mc)
                {
                    ovs.Add(new OptionValue(item.Result("$2"), item.Result("$1")));
                }
                cbGender.DataSource = ovs;
                cbGender.DisplayMember = "Text";
                cbGender.ValueMember = "Value";
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }


回调成功后,把禁用的对象还原出来:

        void TransmitCallBack(IAsyncResult asyncResult)
        {
            gbBasicInfo.Enabled = true;
            bgOtherInfo.Enabled = true;
            btnClose.Enabled = true;
            btnSubmit.Enabled = true;
            lbMessage.Visible = false;
        }

向AD域增加帐号的关键代码:

                #region 向AD域增加用户
                SetLableMessageValue("向AD增加帐号...");
                string ouUserName = userID + "(" + userName + ")";
                using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + addept))
                {
                    DirectoryEntry entry2 = entry.Children.Add("CN=" + ouUserName, "user");
                    entry2.Properties["sAMAccountName"].Add(userID);
                    entry2.Properties["displayname"].Add(ouUserName);
                    entry2.Properties["userPrincipalName"].Add(userID + "@" + DOMAIN_NAME);
                    if (telphone.Length > 0)
                    {
                        entry2.Properties["telephonenumber"].Add(telphone);
                    }
                    entry2.Properties["company"].Add(oadept2);
                    entry2.Properties["userAccountControl"].Value = 0x200;
                    entry2.CommitChanges();
                    entry2.Invoke("SetPassword", new object[] { password });
                    entry2.CommitChanges();
                }
                #endregion


用于获取远程HTML的关键代码:

        /// <summary>
        /// 网页文件页头内容正则
        /// </summary>
        public static Regex ContentTypeTextRegex = new Regex(@"text/(\w+)", RegexOptions.IgnoreCase);

/// <summary>
        /// 创建访问URL的Response
        /// </summary>
        private HttpWebResponse getWebResponse(string uri, Cookie cookie)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            try
            {
                //请求
                request = HttpWebRequest.Create(uri) as HttpWebRequest;
                CookieContainer myCookieContainer = new CookieContainer();

                request.KeepAlive = true;
                request.AllowAutoRedirect = true;
                request.AllowWriteStreamBuffering = true;
                request.Referer = "http://www.lanxe.net/";
                request.Timeout = 20000;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215; fqSpider)";
                request.CookieContainer = myCookieContainer;

                //加入Cookie
                if (cookie != null)
                {
                    myCookieContainer.Add(cookie);
                }

                //回应
                response = request.GetResponse() as HttpWebResponse;
                response.Cookies = myCookieContainer.GetCookies(request.RequestUri);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return response;
                }
            }
            catch
            {

            }

            //返回
            return null;
        }

/// <summary>
        /// 获取网页源代码
        /// </summary>
        public string GetFullHtml(string uri, string encodeName, Cookie cookie)
        {
            HttpWebResponse response = getWebResponse(uri, cookie);
            if (response == null)
            {
                return null;
            }

            try
            {
                //HTML容器
                StringBuilder outs = new StringBuilder();

                //判断文档是否为Text形式
                string contentType = response.Headers["Content-Type"].ToLower();
                if (ContentTypeTextRegex.IsMatch(contentType))
                {
                    //网页编码
                    Encoding encode;
                    if (encodeName.ToLower() == "default")
                    {
                        encode = Encoding.Default;
                    }
                    else
                    {
                        encode = Encoding.GetEncoding(encodeName);
                    }

                    Stream receiveStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(receiveStream, encode);

                    // 每次读取1024
                    char[] read = new char[1024];
                    int count = reader.Read(read, 0, 1024);
                    while (count > 0)
                    {
                        outs.Append(new String(read, 0, count));
                        count = reader.Read(read, 0, 1024);
                    }

                    // 读取完毕
                    reader.Close();
                    receiveStream.Close();
                }

                return outs.ToString();
            }
            catch
            {

            }

            return null;
        }



需要有需要请发邮件索要源程序 deiva@tom.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值