var modelObj = {};
var modelFieldsArray = $('#AddMusicCategory').serializeArray();
$.each(modelFieldsArray, function () {
modelObj[this.name] = this.value;
});
var modelStr = JSON.stringify(modelObj);
var requestModel = {};
requestModel.method = "MusicCategoryCreate";
requestModel.modelstr = modelStr;
$.ajax({
type: "POST",
url: "../../ServiceCenter/Handler/BlogMusicHandler.ashx",
dataType: "json",
data: requestModel,
success: function (rep) {
if (rep.code == 0) {
layer.msg('保存成功', {
time: 1000
});
layer.close(index);
musicApp.musicCategoryMng.musicCategoryList();
}
else {
console.log(rep.code + ":" + rep.msg);
}
}
});
public class BlogMusicHandler : BaseHandler, IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string method = context.Request["method"];
switch (method)
{
case "MusicUpload":
this.MusicUpload(context);
break;
case "MusicList":
this.MusicList(context);
break;
case "MusicDelete":
this.MusicDelete(context);
break;
case "MusicUpdate":
this.MusicUpdate(context);
break;
case "MusicCategoryCreate":
this.MusicCategoryCreate(context);
break;
case "MusicCategoryList":
this.MusicCategoryList(context);
break;
case "MusicCategoryDelete":
this.MusicCategoryDelete(context);
break;
case "MusicCategoryUpdate":
this.MusicCategoryUpdate(context);
break;
case "MusicHomepage":
this.MusicHomepage(context);
break;
default:
break;
}
context.Response.End();
}
private void MusicUpload(HttpContext context)
{
}
private void MusicList(HttpContext context)
{
}
private void MusicDelete(HttpContext context)
{
}
private void MusicUpdate(HttpContext context)
{
}
/// <summary>
/// 新增音乐分类
/// </summary>
/// <param name="context"></param>
private void MusicCategoryCreate(HttpContext context)
{
try
{
LogWriter.ToTrace("开始调用新增音乐分类服务接口");
var strModel = context.Request.Params["modelstr"];
var model= JsonConvert.DeserializeObject<MusicCategoryModel>(strModel);
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"Name",model.Name},
{"ImgUrl",model.ImgUrl },
{"SortNo", (model.SortNo??0).ToString()}
});
HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.PostAsync("api/Music/CreateCategory", content).Result;
if (apiResponse.IsSuccessStatusCode)
{
var result = new { code = 0, msg = "新增音乐分类成功", data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("新增音乐分类服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("新增音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "新增音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
}
catch (Exception ex)
{
LogWriter.ToError("新增音乐分类内部错误:" + ex.Message, ex);
}
LogWriter.ToTrace("结束调用新增音乐分类服务接口");
}
private void MusicCategoryList(HttpContext context)
{
LogWriter.ToTrace("开始调用查询音乐分类列表服务接口");
HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.GetAsync("api/Music/CategoryList").Result;
if (apiResponse.IsSuccessStatusCode)
{
var responseValue = apiResponse.Content.ReadAsStringAsync().Result;
LogWriter.ToDebug("开始查询音乐分类列表->服务端返回数据:" + responseValue);
LogWriter.ToDebug("开始解析数据");
var serviceMusicCategoryModel = JsonConvert.DeserializeObject<List<MusicCategoryModel>>(responseValue);
LogWriter.ToDebug("解析数据成功");
var result = new { code = 0, msg = "获取数据成功", data = serviceMusicCategoryModel };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("查询音乐分类列表服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("查询音乐分类列表服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "查询音乐分类列表服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
}
private void MusicCategoryDelete(HttpContext context)
{
try
{
LogWriter.ToTrace("开始调用删除音乐分类服务接口");
var strModel = context.Request.Params["musicCategoryIds"];
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"musicCategoryIds",strModel}
});
HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.PostAsync("api/Music/DeleteCategory", content).Result;
if (apiResponse.IsSuccessStatusCode)
{
var result = new { code = 0, msg = "删除音乐分类成功", data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("删除音乐分类服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("删除音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "删除音乐分类服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
}
catch (Exception ex)
{
LogWriter.ToError("删除音乐分类内部错误:" + ex.Message, ex);
}
LogWriter.ToTrace("结束调用删除音乐分类服务接口");
}
private void MusicCategoryUpdate(HttpContext context)
{
}
/// <param name="context"></param>
private void MusicHomepage(HttpContext context)
{
LogWriter.ToTrace("开始调用获取圈子音乐主页服务接口");
HttpClient httpClient = GetUseHttpClient();
var apiResponse = httpClient.GetAsync("api/Music/Homepage").Result;
if (apiResponse.IsSuccessStatusCode)
{
var responseValue = apiResponse.Content.ReadAsStringAsync().Result;
LogWriter.ToDebug("开始获取圈子音乐主页->服务端返回数据:" + responseValue);
LogWriter.ToDebug("开始解析数据");
var serviceMusicHomepageResponse = JsonConvert.DeserializeObject<MusicHomepageResponse>(responseValue);
LogWriter.ToDebug("解析数据成功");
var result = new { code = 0,msg = "获取数据成功", data = serviceMusicHomepageResponse };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//webapi返回errcode errmsg
//104为远程webapi服务系统异常,否则为webapi服务业务异常
var receivedErrcode = apiResponse.Headers.GetValues("errcode");
var receivedErrmdg = apiResponse.Headers.GetValues("errmsg");
var errorCode = ((string[])receivedErrcode)[0];
var errorMsg = System.Web.HttpUtility.UrlDecode(((string[])receivedErrmdg)[0]);
if (errorCode == "104")
{
LogWriter.ToError("圈子音乐主页服务接口响应结果异常(系统异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
else
{
LogWriter.ToError("圈子音乐主页服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg);
}
var result = new { code = -1, msg = "圈子音乐主页服务接口响应结果异常(业务异常):[ERRCODE:" + errorCode + "]" + errorMsg, data = "" };
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
public class BaseHandler
{
private static readonly Dictionary<string, HttpClient> HttpClients = new Dictionary<string, HttpClient>();
private static readonly string circleRelationServiceName = "ZJCX.ZJX.CircleRelation.Service";
private static readonly object HttpClientObj = new object();
public static HttpClient GetUseHttpClient()
{
#if !DEBUG
string serviceUrl = ConfigHelper.GetBaseUrl(circleRelationServiceName);
#endif
#if DEBUG
string serviceUrl = "10.250.3.197:9313";
#endif
if (!HttpClients.ContainsKey(serviceUrl))
{
lock (HttpClientObj)
{
if (!HttpClients.ContainsKey(serviceUrl))
{
string serviceBaseUrl = string.Format("http://{0}/", serviceUrl);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(serviceBaseUrl);
client.DefaultRequestHeaders.Connection.Add("keep-alive");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpClients.Add(serviceUrl, client);
}
}
}
return HttpClients[serviceUrl];
}
}