接口代码实现demo

  • 回调接口
      #region 回调接口

        /// <summary>
        /// 回调接口
        /// </summary>
        /// <returns></returns>
        [HttpPost("ThirdCallBack")]
        public ActionResult ThirdCallBack()
        {
            Model.ResultBase result = new Model.ResultBase();
            try
            {
                Dictionary<string, object> dic = GetPostDic();
                if (dic == null)
                {
                    result.code = 0;
                    result.msg = "不是Content-Type:application/json类型";
                    return ToJson(result);
                }
                if (dic.Keys.Contains("sign") && dic.Keys.Contains("appid") && dic.Keys.Contains("rand") && dic["sign"] != null && dic["appid"] != null && dic["rand"] != null)
                {
                    if (Common.SignHelper.SignVerify(dic["sign"].ToString(), dic, AppSercretKey))
                    {
                        string appid = dic["appid"].ToString();//appid
                        string method = dic["method"].ToString();//方法名
                        Dictionary<string, object> data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(dic["data"].ToString());
                        data.Add("actionname", method);
                        //string deviceno = data["deviceno"].ToString();      //设备编号
                        //string callid = data["callid"].ToString();          //呼叫ID
                        //string bnum = data["bnum"].ToString();              //楼栋编号
                        //string hnum = data["hnum"].ToString();              //房间号
                        //string account = data["account"].ToString();        //账号
                        //string pwd = data["pwd"].ToString();                //密码

                        if (method == "applycall")
                        {
                            //TODO ...
                            string json = JsonConvert.SerializeObject(data);
                            //发送TCP消息(此处用HTTP请求TCP服务转发)
                            string action = "/Talking?terminalId=" + UserNo + "&pullDataStr=" + HttpUtility.UrlEncode(JsonConvert.SerializeObject(data).Replace("\r\n", ""), System.Text.Encoding.UTF8);
                            string r = HttpHelper.Send(TcpTimeUrl.TrimEnd('/') + action, "");
                            //同时转发多个账号
                            //string actionwin = "/Talking?terminalId=" + UserNowin + "&pullDataStr=" + HttpUtility.UrlEncode(JsonConvert.SerializeObject(data).Replace("\r\n", ""), System.Text.Encoding.UTF8);
                            //string rwin = HttpHelper.Send(TcpTimeUrl.TrimEnd('/') + actionwin, "");
                        }
                        else if (method == "closecall")
                        {
                            //TODO ...

                            //发送TCP消息(此处用HTTP请求TCP服务转发)
                            string action = "/Talking?terminalId=" + UserNo + "&pullDataStr=" + HttpUtility.UrlEncode(JsonConvert.SerializeObject(data).Replace("\r\n", ""), System.Text.Encoding.UTF8);
                            string r = HttpHelper.Send(TcpTimeUrl.TrimEnd('/') + action, "");
                            //同时转发多个账号
                            //string actionwin = "/Talking?terminalId=" + UserNowin + "&pullDataStr=" + HttpUtility.UrlEncode(JsonConvert.SerializeObject(data).Replace("\r\n", ""), System.Text.Encoding.UTF8);
                            //string rwin = HttpHelper.Send(TcpTimeUrl.TrimEnd('/') + actionwin, "");
                        }
                    }
                    else
                    {
                        result.code = 0;
                        result.msg = "签名校验失败";
                        return ToJson(result);
                    }
                }
                else
                {
                    result.code = 0;
                    result.msg = "参数错误";
                    return ToJson(result);
                }
                result.code = 1;
                result.msg = "ok";
                return ToJson(result);

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
                return ToJson(result);
            }
        }
        #endregion
  • 主动呼叫接口

    #region 测试
          string appid = "";
          string key = "";
          #endregion
    
          #region 主动呼叫
    
          /// <summary>
          /// 终端主动呼叫 模拟第三方业务平台处理呼叫请求接口
          /// </summary>
          /// <returns></returns>
          [HttpPost("StartStopTalking")]
          public ActionResult StartStopTalking()
          {
              Model.ResultBase result = new Model.ResultBase();
              try
              {
                  Dictionary<string, object> dic = GetPostDic();
                  if (dic == null)
                  {
                      result.code = 0;
                      result.msg = "不是Content-Type:application/json类型";
                      return ToJson(result);
                  }
                  if ( dic.Keys.Contains("userid") && dic.Keys.Contains("method") && dic.Keys.Contains("deviceno") && dic["userid"] != null && dic["method"] != null && dic["deviceno"] != null)
                  {
                      string userid = dic["userid"].ToString();//用户名
                      string method = dic["method"].ToString();//方法名
                      string deviceno = dic["deviceno"].ToString();//设备编号
                      string roomNo = "";
                      if (dic.Keys.Contains("roomNo"))
                      {
                          roomNo = dic["roomNo"].ToString();//房间号
                      }
                      string eventNo = "";
                      if (dic.Keys.Contains("eventNo"))
                      {
                          eventNo = dic["eventNo"].ToString();//房间号
                      }
    
                      if (userid == UserNo)
                      {
                          //string appid = dic["appid"].ToString();//appid
                          //string method = dic["method"].ToString();//方法名
                          Dictionary<string, object> data = new Dictionary<string, object>();
                          data.Add("appid", appid);
                          data.Add("account", "xxxxx");
                          data.Add("rand", "1");         
                          data.Add("key", deviceno);
                          data.Add("eventNo", eventNo);//停止呼叫 事件号不能为空
                          data.Add("type", "1");
                          if(method == "closecall")
                          {
                              data.Add("roomNo", roomNo);
                          }
                          //
                          data.Add("sign", Common.SignHelper.CreateMd5Sign(data, key));
    
                          string json = JsonConvert.SerializeObject(data);
                          if (method == "applycall")
                          {
                              //TODO ...
    
                              //转发呼叫请求给 呼叫管理平台
    
                              string res = HttpHelper.Post(StartCallUrl,json);
    
                              if (!string.IsNullOrEmpty(res))
                              {
                                  Dictionary<string, object> resdata = JsonConvert.DeserializeObject<Dictionary<string, object>>(res);
    
                                  if (resdata["resultcode"].ToString() == "1")
                                  {
                                      var outdata= JsonConvert.DeserializeObject<Dictionary<string,object>>(resdata["data"].ToString());
                                      string account = outdata["dr_account"].ToString();
                                      string pwd = outdata["dr_pwd"].ToString();
                                      string outeventNo = outdata["eventNo"].ToString();
                                      string dr_ppsvraddr = outdata["dr_ppsvraddr"].ToString();
                                      string dr_talktype = outdata["dr_talktype"].ToString();
                                      string dr_roomno = outdata["dr_roomno"].ToString();
    
                                      result.code = 1;
                                      result.msg = "呼叫请求成功";
                                      result.data = outdata;
                                  }
                                  else
                                  {
                                      result.code = 0;
                                      result.msg = resdata["msg"].ToString();
                                  }
                              }
                          }
                          else if (method == "closecall")
                          {
                              //TODO ...
                              string res = HttpHelper.Post(StopCallUrl, json);
    
                              if (!string.IsNullOrEmpty(res))
                              {
                                  Dictionary<string, object> resdata = JsonConvert.DeserializeObject<Dictionary<string, object>>(res);
                                  if (resdata["resultcode"].ToString() == "1")
                                  {
                                      result.code = 1;
                                  }
                                  else
                                  {
                                      result.code = 0;
    
                                  }
                                  result.msg = resdata["msg"].ToString();
                              }
                          }
    
                          return ToJson(result);
                      }
                      else
                      {
                          result.code = 0;
                          result.msg = "签名校验失败";
                          return ToJson(result);
                      }
                  }
                  else
                  {
                      result.code = 0;
                      result.msg = "参数错误";
                      return ToJson(result);
                  }
                  result.code = 1;
                  result.msg = "ok";
                  return ToJson(result);
    
              }
              catch (Exception ex)
              {
                  result.code = -1;
                  result.msg = ex.Message;
                  return ToJson(result);
              }
          }
          #endregion
作者:郭祥西  创建时间:2024-04-10 17:18
最后编辑:郭祥西  更新时间:2024-11-07 15:18