Пример #1
0
        public ActionResult SelectZw(string compid)
        {
            string accecctoken = GetQiyeToken.GetQiyeAttoken(compid);

            string url       = $"https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token={accecctoken}";
            string strResult = httpHelp.GetWebRequest(url);

            try
            {
                if (!string.IsNullOrWhiteSpace(strResult) && strResult.Substring(0, 1) == "{" && strResult.Substring(strResult.Length - 1, 1) == "}")
                {
                    WechatReturnDep wrd = JSON.parse <WechatReturnDep>(strResult);
                    if (wrd.errcode == "0")
                    {
                        List <WechatDep> listDep = wrd.department;
                        if (listDep.Count > 0)
                        {
                            //string sqlstr2 = "";
                            //foreach (var item in listDep)
                            //{
                            //    //循环添加到数据库中
                            //    string JX = Pinyin.GetInitials(item.name);//简写
                            //    string QP = Pinyin.GetPinyin(item.name).Replace(" ", "");//全拼
                            //    sqlstr2 += string.Format($"insert into Depart(DepartID,DepartName, PID, DepartPrincipal, Remark,SpellJX,SpellQP,CompanyId) values({item.id},'{item.name}', '{item.parentid}', null, '','{JX}','{QP}','{compid}') ");

                            //}
                            //string num = sql.EditDataCommand(sqlstr2);
                            //if (num == "0")
                            //{
                            //    return Content("同步成功");
                            //}
                            //else
                            //{
                            //    return Content("同步失败");
                            //}
                            return(Content("ok"));
                        }
                        else
                        {
                            return(Content("暂无信息需要同步"));
                        }
                    }
                    else
                    {
                        return(Content("error:" + strResult));
                    }
                }
                else
                {
                    return(Content("error:" + strResult));
                    //return new ReturnJson() { errmsg = string.IsNullOrWhiteSpace(strResult) ? "error" : strResult };
                }
            }
            catch (Exception ex)
            {
                //log.AppenLog("从微信中读取数据失败:" + ex.Message);
                return(Content("从微信中读取数据失败:" + strResult));
            }
        }
Пример #2
0
        /// <summary>
        /// 同步员工
        /// </summary>
        /// <param name="compid">加密后的公司id</param>
        /// <returns></returns>
        public ActionResult SelectEmp(string compid)
        {
            string accecctoken = GetQiyeToken.GetQiyeAttoken(compid);

            //应用须拥有指定部门的查看权限。
            //得到部门信息
            string    sqlstr   = string.Format("select * from Depart where PID=0 and CompanyID='{0}'", Base64MIMA.JIE(compid));
            DataTable dt       = sql.GetDataTableCommand(sqlstr);
            string    sqlUser2 = string.Format("select * from Employee where CompanyID='{0}'", Base64MIMA.JIE(compid));
            DataTable dtUser2  = sql.GetDataTableCommand(sqlUser2);

            if (dt?.Rows.Count > 0)
            {
                int    a       = 0;
                string sqlstr2 = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    a++;
                    int    depId     = int.Parse(dt.Rows[i]["DepartID"].ToString());
                    string url       = $"https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={accecctoken}&department_id={depId}&fetch_child=1";
                    string strResult = httpHelp.GetWebRequest(url);
                    try
                    {
                        if (!string.IsNullOrWhiteSpace(strResult) && strResult.Substring(0, 1) == "{" && strResult.Substring(strResult.Length - 1, 1) == "}")
                        {
                            WechatReturnUser wrd = JSON.parse <WechatReturnUser>(strResult);
                            if (wrd.errcode == "0")
                            {
                                List <WechatUserList> listUser = wrd.userlist;


                                if (listUser.Count > 0)
                                {
                                    #region 如果微信那边没有,数据库中有,就把数据库中的删掉
                                    if (dtUser2.Rows.Count > 0)
                                    {
                                        var vupdel = (from s in dtUser2.AsEnumerable()
                                                      where !listUser.Any(x => x.userid == s.Field <string>("EmpID") && Base64MIMA.JIE(compid) == s.Field <string>("CompanyID"))
                                                      select s).ToList();
                                        if (vupdel.Count > 0)
                                        {
                                            foreach (var item in vupdel)
                                            {
                                                sqlstr2 += string.Format($"delete Employee where EmpID='{item.Field<string>("EmpID")}' and CompanyID='{Base64MIMA.JIE(compid)}' ");
                                            }
                                        }
                                    }


                                    #endregion
                                    foreach (var item in listUser)
                                    {
                                        //比较数据库中是否又这个人
                                        string      sqlUser    = string.Format($"select * from Employee where EmpID ='{item.userid}' and CompanyID='{Base64MIMA.JIE(compid)}'");
                                        DataTable   dtUser     = sql.GetDataTableCommand(sqlUser);
                                        string      url2       = $"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={accecctoken}&userid={item.userid}";
                                        string      strResult2 = httpHelp.GetWebRequest(url2);
                                        UserDetials ud         = JSON.parse <UserDetials>(strResult2);
                                        if (dtUser?.Rows.Count > 0)
                                        {
                                            //数据库又这个人不用同步
                                            if (ud.errcode == 0)
                                            {
                                                #region  微信那边的信息有没有更改,以微信为准
                                                int b = 0;

                                                bool Isok = int.TryParse(dtUser.Rows[0]["DepartID"].ToString(), out b);//部门id
                                                if (Isok)
                                                {
                                                    b = int.Parse(dtUser.Rows[0]["DepartID"].ToString());
                                                }
                                                if (ud.name != dtUser.Rows[0]["Name"].ToString() || item.department[0] != b)
                                                {
                                                    string JX = Pinyin.GetInitials(ud.name);                //简写
                                                    string QP = Pinyin.GetPinyin(ud.name).Replace(" ", ""); //全拼
                                                    sqlstr2 += string.Format($"update Employee set SpellQP='{QP}',SpellJX='{JX}',Name='{ud.name}',DepartID={item.department[0]} where EmpID='{ud.userid}' and CompanyID='{Base64MIMA.JIE(compid)}' ");
                                                }
                                                #endregion
                                            }
                                        }
                                        else
                                        {
                                            //获取这个人的详情信息

                                            if (ud.errcode == 0)
                                            {
                                                //循环添加到数据库中
                                                string JX = Pinyin.GetInitials(item.name);                //简写
                                                string QP = Pinyin.GetPinyin(item.name).Replace(" ", ""); //全拼
                                                sqlstr2 += $@"INSERT INTO dbo.Employee
                                            (EmpID, Name ,Sex,EmpTel ,EmpEmail 
                                            ,IsOut ,SpellJX,SpellQP ,DepartID,CompanyID,EmpPhotos)VALUES 
                                            ('{ud.userid}','{ud.name}',{ud.gender},'{ud.mobile}','{ud.email}'
                                             ,{0},'{JX}','{QP}','{item.department[0]}','{Base64MIMA.JIE(compid)}','{ud.avatar}') ";
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                return(Content("error:" + strResult));
                            }
                        }
                        else
                        {
                            return(Content("error:" + strResult));
                            //return new ReturnJson() { errmsg = string.IsNullOrWhiteSpace(strResult) ? "error" : strResult };
                        }
                    }
                    catch (Exception ex)
                    {
                        //log.AppenLog("从微信中读取数据失败:" + ex.Message);
                        return(Content("从微信中读取数据失败:" + ex.Message));
                    }
                }
                if (a == 0)
                {
                    return(Content("暂无需要同步的信息"));
                }
                else
                {
                    if (sqlstr2 == "")
                    {
                        return(Content("数据一致,不需要同步"));
                    }
                    string num = sql.EditDataCommand(sqlstr2);
                    if (num == "0")
                    {
                        return(Content("ok"));
                    }
                    else
                    {
                        return(Content("同步失败"));
                    }
                }
            }
            else
            {
                return(Content("请先同步部门"));
            }
        }