Ejemplo n.º 1
0
        /// <summary>
        /// 批量操作--导入成员
        /// </summary>
        /// <param name="OPType"></param>
        /// <returns></returns>
        public ActionResult batchuser(int?OPType)
        {
            string strRet = "[\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"{0}\"]";

            string strErrRet = "[\"jsonrpc\" : \"2.0\", \"error\" : [\"code\": {0}, \"message\": \"{1}\"], \"id\" : \"id\"]";

            string operateType = "";

            try
            {
                if (Request.Files.Count == 0)
                {
                    strRet = string.Format(strErrRet, "100", "未发现上传文件!");
                    return(Content(strRet));
                }

                Dictionary <string, Stream> dic = new Dictionary <string, Stream>();

                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase objFile = Request.Files[i];
                    dic.Add(objFile.FileName, objFile.InputStream);
                }


                var ret = MediaApi.Upload(GetToken(), UploadMediaFileType.file, dic, "");

                System.Threading.Thread.Sleep(1000);

                //上传失败
                if (!string.IsNullOrEmpty(ret.errmsg) && ret.errmsg.IndexOf("ok") < 0)
                {
                    strRet = string.Format(strErrRet, ret.errcode, ret.errmsg);
                    return(Content(strRet));
                }

                BatchUserResult objRet;

                var       objConfig   = WeChatCommonService.GetWeChatConfig(AppId);
                string    callbackUrl = CommonService.GetSysConfig(SysConfigCode.CallBackUrl, "");//从Sysconfig数据库中读url
                BatchUser objUser     = new BatchUser()
                {
                    media_id = ret.media_id,
                    callback = new callback()
                    {
                        token          = objConfig.WeixinToken,          // Token,
                        encodingaeskey = objConfig.WeixinEncodingAESKey, // EncodingAESKey,
                        url            = callbackUrl                     //app.innocellence.com/wechatqy/led
                    }
                };

                if (OPType == 1)
                {
                    objRet      = MailListApi.BatchUpdateUser(GetToken(), objUser);
                    operateType = "sync_user";
                }
                else if (OPType == 2)
                {
                    objRet      = MailListApi.BatchReplaceUser(GetToken(), objUser);
                    operateType = "replace_user";
                }
                else if (OPType == 3)
                {
                    objRet      = MailListApi.BatchReplaceDept(GetToken(), objUser);
                    operateType = "replace_party";
                }
                else
                {
                    objRet = new BatchUserResult()
                    {
                        errmsg = "参数错误!"
                    };
                }

                //当执行导入操作时将返回的jobId插入到表中去
                if (!string.IsNullOrEmpty(objRet.errmsg))
                {
                    strRet = string.Format(strRet, objRet.jobid);
                    var jobLog = new BatchJobLog()
                    {
                        JobID       = objRet.jobid,
                        Status      = 0,
                        Type        = operateType,
                        CreatedDate = DateTime.Now
                    };

                    _batchJobLogService.Repository.Insert(jobLog);//插入表中
                }
                else
                {
                    strRet = string.Format(strErrRet, objRet.errcode, objRet.errmsg);
                }
            }
            catch (Exception ex)
            {
                strRet = string.Format(strErrRet, "102", ex.Message);
            }


            return(Content(strRet.Replace("[", "{").Replace("]", "}")));
        }