using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Jyson.IZhanShi.Model.WebApiModel; namespace Jyson.IZhanShi.WinDesktop.Utility { public class OperateHttp { private OperateHttp() { } /// <summary> /// 单例模式 /// </summary> private static OperateHttp _instance = null; public static OperateHttp Instance() { if (_instance == null) { _instance = new OperateHttp(); } return _instance; } static HttpClient _httpClient; public HttpClient httpClient { get { if (_httpClient == null) _httpClient = new HttpClient(); return _httpClient; } set { _httpClient = value; } } /// <summary> /// 餐厅ID /// </summary> public int RestaurantID { get; set; } public string RestaurantName { get; set; } public string RestaurantSessionKey { get; set; } #region --Get/Post/Put/Delete 方法操作 /// <summary> /// Get方式获取数据 /// </summary> /// <param name="httpUrl"></param> /// <returns></returns> public JsonData GetResult(string httpUrl) { HttpResponseMessage hrm = httpClient.GetAsync(httpUrl).Result; JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result; return data; } /// <summary> /// Get 异步方式获取数据 /// </summary> /// <param name="httpUrl"></param> /// <returns></returns> //public async Task<JsonData> GetAsyncResult(string httpUrl) //{ // HttpResponseMessage hrm = await httpClient.GetAsync(httpUrl).ConfigureAwait(false); // JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result; // return data; //} /// <summary> /// Post 方式传递数据 /// </summary> /// <param name="postModel"></param> /// <param name="httpUrl"></param> /// <returns></returns> public JsonData PostResult(string httpUrl, object postModel) { HttpResponseMessage hrm = httpClient.PostAsJsonAsync(httpUrl, postModel).Result; JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result; return data; } /// <summary> /// Post 异步方式传递数据 /// </summary> /// <param name="httpUrl"></param> /// <param name="postModel"></param> /// <returns></returns> //public async Task<JsonData> PostAsyncResult(string httpUrl, object postModel) //{ // var hrm = await httpClient.PostAsJsonAsync(httpUrl, postModel).ConfigureAwait(false); // JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result; // return data; //} /// <summary> /// Put 更新方式传递数据 /// </summary> /// <param name="postModel"></param> /// <param name="httpUrl"></param> /// <returns></returns> public JsonData PutResult(string httpUrl, object postModel) { HttpResponseMessage hrm = httpClient.PutAsJsonAsync(httpUrl, postModel).Result; JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result; return data; } /// <summary> /// Delete 删除方式传递数据 /// </summary> /// <param name="httpUrl"></param> /// <returns></returns> public JsonData DeleteResult(string httpUrl) { HttpResponseMessage hrm = httpClient.DeleteAsync(httpUrl).Result; JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result; return data; } #endregion #region 菜单操作 /// <summary> /// 获取该店铺所有菜单列表 /// </summary> /// <returns></returns> public List<WMFoodMenu> GetFoodMenuList(string httpUrl) { JsonData prod = OperateHttp.Instance().GetResult(httpUrl); var model = JsonConvert.DeserializeObject<List<WMFoodMenu>>(prod.data.ToString()); return model; } /// <summary> /// 获取菜单类别 /// </summary> /// <returns></returns> public List<WMFoodMenuCategory> GetFoodMenuCategory() { string httpUrl = HttpUrl.FoodMenuCategoryUrl + OperateHttp.Instance().RestaurantID; JsonData prod = OperateHttp.Instance().GetResult(httpUrl); var model = JsonConvert.DeserializeObject<List<WMFoodMenuCategory>>(prod.data.ToString()); return model; } /// <summary> /// 获取菜单单个信息 /// </summary> /// <returns></returns> public WMFoodMenu GetFoodMenu(int foodMenuID) { string httpUrl = HttpUrl.FoodMenuOneUrl + foodMenuID; JsonData prod = OperateHttp.Instance().GetResult(httpUrl); var model = JsonConvert.DeserializeObject<WMFoodMenu>(prod.data.ToString()); return model; } /// <summary> /// 更新菜单 /// </summary> /// <returns></returns> public bool PutFoodMenu(int foodMenuID, WMFoodMenu model) { string httpUrl = HttpUrl.FoodMenuUrl + OperateHttp.Instance().RestaurantID; JsonData data = OperateHttp.Instance().PutResult(httpUrl, model); if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg)) { return true; } else { return false; } } /// <summary> /// 更新菜单状态 /// </summary> /// <returns></returns> public bool PutIsSell(int foodMenuID, WMFoodMenuIsSell model) { string httpUrl = HttpUrl.FoodMenuOneUrl + foodMenuID.ToString(); JsonData data = OperateHttp.Instance().PutResult(httpUrl, model); if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg)) { return true; } else { return false; } } /// <summary> /// 删除菜单 /// </summary> /// <returns></returns> public bool DeleteFoodMenu(int foodMenuID) { string httpUrl = HttpUrl.FoodMenuOneUrl + foodMenuID.ToString(); JsonData data = OperateHttp.Instance().DeleteResult(httpUrl); if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg)) { return true; } else { return false; } } #endregion /// <summary> /// 获取订单列表 /// </summary> /// <param name="httpUrl"></param> /// <returns></returns> public List<WMOrder> GetOrder(string httpUrl) { JsonData prod = OperateHttp.Instance().GetResult(httpUrl); var model = JsonConvert.DeserializeObject<List<WMOrder>>(prod.data.ToString()); return model; } /// <summary> /// 获取订单列表 /// </summary> /// <param name="httpUrl"></param> /// <returns></returns> public WMOrderDetail GetOrderDetail(int orderID) { string httpUrl = HttpUrl.OrderDetailUrl + orderID.ToString(); JsonData prod = OperateHttp.Instance().GetResult(httpUrl); var model = JsonConvert.DeserializeObject<WMOrderDetail>(prod.data.ToString()); return model; } /// <summary> /// 取消订单 /// </summary> /// <param name="orderID">订单ID</param> /// <returns></returns> public bool DeleteOrder(int orderID) { string httpUrl = HttpUrl.OrderUrl + orderID.ToString(); JsonData data = OperateHttp.Instance().DeleteResult(httpUrl); if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg)) { return true; } else { return false; } } /// <summary> /// 订单打印 /// </summary> /// <param name="httpUrl"></param> /// <returns></returns> public string PutOrderPrint(int orderID) { string httpUrl = HttpUrl.OrderPrintUrl + orderID.ToString(); JsonData resultData = OperateHttp.Instance().PutResult(httpUrl, null); if (resultData.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(resultData.errorMsg)) { return "Success"; } else { return resultData.data.ToString(); } } } }