Пример #1
0
        /// <summary>
        ///同步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSyn_Click(object sender, EventArgs e)
        {
            string err = "";

            Model.wx_userweixin weixin = GetWeiXinCode();
            WeiXinCRMComm       wcr    = new WeiXinCRMComm();
            string accessToken         = wcr.getAccessToken(weixin.id, out err);

            if (err != "")
            {
                return;
            }
            BLL.wx_crm_users uBll = new BLL.wx_crm_users();


            //更新时间,作为一个标记
            DateTime updateTime = DateTime.Now;

            try
            {
                string ret = SysPersonFun(0, weixin.id, accessToken, "", updateTime);
                //查找此次为同步到的,则为已经跑路的
                IList <Model.wx_crm_users> userlist = uBll.GetModelList("updateDate<'" + updateTime.ToString() + "'");
                if (userlist != null && userlist.Count > 0)
                {
                    for (int i = 0; i < userlist.Count; i++)
                    {
                        userlist[i].uStatus = 2;
                        uBll.Update(userlist[i]);
                    }
                }

                JscriptMsg(ret, "user_list.aspx", "Success");
            }
            catch (Exception ex)
            {
                lblInfo.Text = ex.Message;
            }
        }
Пример #2
0
        /// <summary>
        /// 同步一个用户信息,如果库里存在,则修改,不存在,则新增
        /// </summary>
        /// <param name="userDetail"></param>
        private int InsertUserInfo(int wid, string accessToken, List <string> openidStr, DateTime updateTime)
        {
            int totCount = 0;

            //   将数据插入到数据库里
            BLL.wx_crm_users   uBll    = new BLL.wx_crm_users();
            Model.wx_crm_users uEntity = new Model.wx_crm_users();
            try
            {
                UserInfoJson userDetail = new UserInfoJson();
                for (int i = 0; i < openidStr.Count; i++)
                {
                    userDetail = Senparc.Weixin.MP.AdvancedAPIs.User.Info(accessToken, openidStr[i]);
                    uEntity    = uBll.GetModel(wid, openidStr[i]);
                    bool isAdd = true;
                    if (uEntity == null)
                    {
                        isAdd   = true;
                        uEntity = new Model.wx_crm_users();
                    }
                    else
                    {
                        isAdd = false;
                    }
                    uEntity.wid            = wid;
                    uEntity.openid         = openidStr[i];
                    uEntity.nickname       = userDetail.nickname;
                    uEntity.sex            = userDetail.sex.ToString();
                    uEntity.city           = userDetail.city;
                    uEntity.country        = userDetail.country;
                    uEntity.province       = userDetail.province;
                    uEntity.language       = userDetail.language;
                    uEntity.headimgurl     = userDetail.headimgurl;
                    uEntity.subscribe_time = userDetail.subscribe_time.ToString();

                    uEntity.updateDate = updateTime;
                    uEntity.uStatus    = 1;

                    //查询分组号
                    GetGroupIdResult gid = Groups.GetId(accessToken, openidStr[i]);
                    uEntity.groupId = gid.groupid;

                    if (isAdd)
                    {
                        //新增
                        uEntity.createDate = DateTime.Now;
                        int succ = uBll.Add(uEntity);
                        if (succ > 0)
                        {
                            totCount++;
                        }
                    }
                    else
                    {
                        //修改
                        bool succ = uBll.Update(uEntity);
                        if (succ)
                        {
                            totCount++;
                        }
                    }
                }
                return(totCount);
            }
            catch (Exception ex)
            {
                throw new Exception("用户信息同步失败!" + ex.Message);
            }
        }