private void UpdateSetting(TransmitData transmitData)
        {
            //https://oapi.dingtalk.com/call_back/update_call_back?access_token=ACCESS_TOKEN
            string url = string.Format(CallbackSettingUrl.callBackSettingTemplate, CallbackSettingUrl.update_call_back, this.access_token);
            Dictionary <string, object> dicPara = new Dictionary <string, object>();

            dicPara.Add("call_back_tag", transmitData.callbackTag.ToArray());
            dicPara.Add("token", transmitData.token);
            dicPara.Add("aes_key", transmitData.aesKey);
            dicPara.Add("url", transmitData.callbackUrl);

            string result = SettingCallback.Request(url, "POST", dicPara);

            JObject rObj    = JObject.Parse(result);
            int     errcode = Convert.ToInt32(rObj["errcode"].ToString());

            if (errcode == 0)
            {
                SettingCallback.callbackNameList = transmitData.callbackTag;
            }
            else
            {
                throw new Exception("更新事件回调发生异常!响应内容:" + result);
            }
        }
        private bool HasSetting(out List <string> tagList)
        {
            tagList = new List <string>();

            string url    = string.Format(CallbackSettingUrl.callBackSettingTemplate, CallbackSettingUrl.get_call_back, this.access_token);
            string result = SettingCallback.Request(url, "GET", null);

            JObject rObj    = JObject.Parse(result);
            int     errcode = Convert.ToInt32(rObj["errcode"].ToString());

            if (errcode == 0)
            {
                JArray call_back_tag = (JArray)rObj["call_back_tag"];
                if (call_back_tag == null)
                {
                    throw new Exception("查询注册成功的回调事件响应内容中无“call_back_tag”属性!响应内容:" + result);
                }
                foreach (JToken jto in call_back_tag)
                {
                    string tag = jto + string.Empty;
                    tagList.Add(tag);
                }
                return(call_back_tag.Count > 0);
            }
            else if (errcode == 71007)
            {
                return(false);
            }
            else
            {
                throw new Exception("查询注册成功的回调事件发生异常!响应内容:" + result);
            }
        }
 public static SettingCallback GetInstance(TransmitData transmitData)
 {
     if (instance == null)
     {
         lock (locker)
         {
             if (instance == null)
             {
                 instance = new SettingCallback(transmitData);
             }
         }
         return(instance);
     }
     instance.access_token = instance.GetToken(transmitData.appKey, transmitData.appSecret);
     return(instance);
 }
        private string GetToken(string appkey, string appsecret)
        {
            string  url     = string.Format(CallbackSettingUrl.getTokenUrlTemplate, appkey, appsecret);
            string  result  = SettingCallback.Request(url, "GET", null);
            JObject rObj    = JObject.Parse(result);
            int     errcode = Convert.ToInt32(rObj["errcode"].ToString());

            if (errcode == 0)
            {
                string access_token = rObj["access_token"].ToString().Replace("\"", "");
                return(access_token);
            }
            else
            {
                throw new Exception("获取应用凭证发生异常!响应内容:" + result);
            }
        }
        private void RemoveSetting(TransmitData transmitData)
        {
            string url = string.Format(CallbackSettingUrl.callBackSettingTemplate, CallbackSettingUrl.delete_call_back, this.access_token);

            string result = SettingCallback.Request(url, "GET", null);

            JObject rObj    = JObject.Parse(result);
            int     errcode = Convert.ToInt32(rObj["errcode"].ToString());

            if (errcode == 0)
            {
                SettingCallback.callbackNameList = transmitData.callbackTag;
            }
            else
            {
                throw new Exception("删除事件回调发生异常!响应内容:" + result);
            }
        }
        private SettingCallback(TransmitData transmitData)
        {
            this.access_token = this.GetToken(transmitData.appKey, transmitData.appSecret);

            List <string> tagList    = new List <string>();
            bool          hasSetting = this.HasSetting(out tagList);

            if (transmitData.callbackTag == null || transmitData.callbackTag.Count == 0)
            {
                if (hasSetting)
                {
                    new LaterExecute.LaterThread(RemoveSetting, transmitData);
                }
                else
                {
                    SettingCallback.callbackNameList = transmitData.callbackTag;
                }
            }
            else
            {
                if (hasSetting)
                {
                    if (!SettingCallback.ArrayIsEqual(tagList, transmitData.callbackTag))
                    {
                        //修改
                        new LaterExecute.LaterThread(UpdateSetting, transmitData);
                    }
                    else
                    {
                        SettingCallback.callbackNameList = tagList;
                    }
                }
                else
                {
                    //新增
                    new LaterExecute.LaterThread(RegisterSetting, transmitData);
                }
            }
        }