Esempio n. 1
0
        public int DeleteShop(int ShopID, int ShopImageID)
        {
            ShopImageModel model = new ShopImageModel();

            if (ShopImageID != 0)
            {
                var response = client.GetAsync(string.Format("{0}/api/ShopImages/{1}", WebAPIUrl, ShopImageID)).Result;
                if (response.IsSuccessStatusCode)
                {
                    var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                    string code           = responseString.SelectToken("code").ToString();
                    if (code == "0")
                    {
                        model.ImageName = (string)responseString["data"]["ImageName"];
                        model.ImagePath = (string)responseString["data"]["ImagePath"];
                    }
                }
            }
            string fullPath = Request.MapPath("~/Areas/Images/" + model.ImageName);

            if (System.IO.File.Exists(fullPath))
            {
                System.IO.File.Delete(fullPath);
            }

            response = client.DeleteAsync(string.Format("{0}/api/ShopImages/{1}", WebAPIUrl, ShopImageID)).Result;
            if (response.IsSuccessStatusCode)
            {
                var    result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                string code   = result.SelectToken("code").ToString();
                if (code == "0")
                {
                    response = client.DeleteAsync(string.Format("{0}/api/Shops/{1}", WebAPIUrl, ShopID)).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                        code   = result.SelectToken("code").ToString();
                        if (code == "0")
                        {
                            return(0);
                        }
                    }
                    return(0);
                }
            }
            return(-1);
        }
Esempio n. 2
0
        public ActionResult Create(ShopAndImageModel model, HttpPostedFileBase postedFile)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (model.ID > 0)
                    {
                        model.Active      = model.Active;
                        model.Created     = System.DateTime.Now;
                        model.CreatedBy   = 1;
                        model.Updated     = System.DateTime.Now;
                        model.UpdatedBy   = 1;
                        model.StateID     = model.StateID;
                        model.CityID      = model.CityID;
                        model.CategoryID  = model.CategoryID;
                        model.UserID      = 1;
                        model.ShopImageID = model.ShopImageID;

                        string data     = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                        var    content  = new StringContent(data, Encoding.UTF8, "application/json");
                        var    response = client.PutAsync(string.Format("{0}/api/Shops", WebAPIUrl), content).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                            string code           = responseString.SelectToken("code").ToString();
                            int    ShopID         = Convert.ToInt32(responseString.SelectToken("id"));
                            if (code == "0")
                            {
                                //Image Upload Code Start
                                if (postedFile != null)
                                {
                                    //Extract Image File Name.
                                    string ImageName = System.IO.Path.GetFileName(postedFile.FileName);

                                    //Set the Image File Path.
                                    string ImagePath = "~/Areas/Images/" + ImageName;

                                    //Save the Image File in Folder.
                                    postedFile.SaveAs(Server.MapPath(ImagePath));

                                    ShopImageModel ShopImageModel = new ShopImageModel();
                                    ShopImageModel.ID        = (int)model.ShopImageID;
                                    ShopImageModel.ImageName = ImageName;
                                    ShopImageModel.ImagePath = ImagePath;
                                    ShopImageModel.ShopID    = ShopID;
                                    ShopImageModel.Active    = true;
                                    ShopImageModel.Created   = System.DateTime.Now;
                                    ShopImageModel.CreatedBy = 1;
                                    ShopImageModel.Updated   = System.DateTime.Now;
                                    ShopImageModel.UpdatedBy = 1;

                                    string dataImage     = Newtonsoft.Json.JsonConvert.SerializeObject(ShopImageModel);
                                    var    contentImage  = new StringContent(dataImage, Encoding.UTF8, "application/json");
                                    var    responseImage = client.PutAsync(string.Format("{0}/api/ShopImages", WebAPIUrl), contentImage).Result;
                                    if (responseImage.IsSuccessStatusCode)
                                    {
                                        var    responseStringImage = JObject.Parse(responseImage.Content.ReadAsStringAsync().Result);
                                        string codeImage           = responseStringImage.SelectToken("code").ToString();
                                        if (codeImage == "0")
                                        {
                                            return(RedirectToAction("Index", "Shop"));
                                        }
                                    }
                                    else
                                    {
                                        ViewBag.Message = "Shop Image Insertion failed. " + responseImage.ToString();
                                        return(View(model));
                                    }
                                }
                                //Image Upload Code End

                                return(RedirectToAction("Index", "Shop"));
                            }
                            else
                            {
                                ViewBag.Message = "State not found";
                                return(View(model));
                            }
                        }
                        else
                        {
                            ViewBag.Message = "Update failed.";
                            return(View(model));
                        }
                    }
                    else
                    {
                        model.Active    = true;
                        model.Created   = System.DateTime.Now;
                        model.CreatedBy = 1;
                        model.Updated   = System.DateTime.Now;
                        model.UpdatedBy = 1;
                        model.StateID   = model.StateID;
                        model.CityID    = model.CityID;
                        model.UserID    = 1;

                        string data     = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                        var    content  = new StringContent(data, Encoding.UTF8, "application/json");
                        var    response = client.PostAsync(string.Format("{0}/api/Shops", WebAPIUrl), content).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                            string code           = responseString.SelectToken("code").ToString();
                            int    ShopID         = Convert.ToInt32(responseString.SelectToken("id"));
                            if (code == "0")
                            {
                                //Image Upload Code Start
                                if (postedFile != null)
                                {
                                    //Extract Image File Name.
                                    string ImageName = System.IO.Path.GetFileName(postedFile.FileName);

                                    //Set the Image File Path.
                                    string ImagePath = "~/Areas/Images/" + ImageName;

                                    //Save the Image File in Folder.
                                    postedFile.SaveAs(Server.MapPath(ImagePath));

                                    ShopImageModel ShopImageModel = new ShopImageModel();
                                    ShopImageModel.ImageName = ImageName;
                                    ShopImageModel.ImagePath = ImagePath;
                                    ShopImageModel.ShopID    = ShopID;
                                    ShopImageModel.Active    = true;
                                    ShopImageModel.Created   = System.DateTime.Now;
                                    ShopImageModel.CreatedBy = 1;
                                    ShopImageModel.Updated   = System.DateTime.Now;
                                    ShopImageModel.UpdatedBy = 1;

                                    string dataImage     = Newtonsoft.Json.JsonConvert.SerializeObject(ShopImageModel);
                                    var    contentImage  = new StringContent(dataImage, Encoding.UTF8, "application/json");
                                    var    responseImage = client.PostAsync(string.Format("{0}/api/ShopImages", WebAPIUrl), contentImage).Result;
                                    if (responseImage.IsSuccessStatusCode)
                                    {
                                        var    responseStringImage = JObject.Parse(responseImage.Content.ReadAsStringAsync().Result);
                                        string codeImage           = responseStringImage.SelectToken("code").ToString();
                                        if (codeImage == "0")
                                        {
                                            return(RedirectToAction("Index", "Shop"));
                                        }
                                    }
                                    else
                                    {
                                        ViewBag.Message = "Shop Image Insertion failed. " + responseImage.ToString();
                                        return(View(model));
                                    }
                                }
                                //Image Upload Code End

                                return(RedirectToAction("Index", "Shop"));
                            }
                            else
                            {
                                ViewBag.Message = "Shop already exists.";
                                return(View(model));
                            }
                        }
                        else
                        {
                            ViewBag.Message = "Insert failed. " + response.ToString();
                            return(View(model));
                        }
                    }
                }
                return(View(model));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        public ApiResultModel UploadShopImage(ShopImageModel model)
        {
            //string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "YST.txt";
            //StreamWriter sw = new StreamWriter(filePath, true);
            //sw.Write("测试开始。。。");
            //sw.Close();
            ApiResultModel resultModel = new ApiResultModel();

            try
            {
                //获取要设置基本信息的物业用户
                IShopUserBLL userBll = BLLFactory <IShopUserBLL> .GetBLL("ShopUserBLL");

                T_ShopUser user = userBll.GetEntity(u => u.Id == model.UserId && u.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);
                if (user != null)
                {
                    //如果验证Token不通过或已过期
                    if (DateTime.Now > user.TokenInvalidTime || model.Token != user.Token)
                    {
                        resultModel.Msg = APIMessage.TOKEN_INVALID;
                        return(resultModel);
                    }
                    //更新最近登录时间和Token失效时间
                    user.LatelyLoginTime  = DateTime.Now;
                    user.TokenInvalidTime = DateTime.Now.AddDays(Convert.ToInt32(PropertyUtils.GetConfigParamValue("TokenInvalid")));

                    userBll.Update(user);

                    //调用商家BLL层获取商家信息
                    IShopBLL shopBll = BLLFactory <IShopBLL> .GetBLL("ShopBLL");

                    var shop = shopBll.GetEntity(s => s.Id == model.ShopId);

                    if (shop != null)
                    {
                        //设定头像路径及文件名
                        string dir = HttpContext.Current.Server.MapPath(ConstantParam.SHOP_IMG_DIR);

                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        //设置商家封面
                        if (!string.IsNullOrEmpty(model.ShopImage))
                        {
                            var    fileName = DateTime.Now.ToFileTime().ToString() + ".zip";
                            string filepath = Path.Combine(dir, fileName);

                            using (FileStream fs = new FileStream(filepath, FileMode.Create))
                            {
                                using (BinaryWriter bw = new BinaryWriter(fs))
                                {
                                    byte[] datas = Convert.FromBase64String(model.ShopImage);
                                    bw.Write(datas);
                                    bw.Close();
                                }
                            }

                            //封面路径保存
                            shop.ImgPath = PropertyUtils.UnZip(filepath, dir, ConstantParam.SHOP_IMG_DIR);

                            StringBuilder imgsSB = new StringBuilder();
                            //生成缩略图保存
                            foreach (var path in shop.ImgPath.Split(';'))
                            {
                                string thumpFile = DateTime.Now.ToFileTime() + ".jpg";
                                string thumpPath = Path.Combine(HttpContext.Current.Server.MapPath(ConstantParam.SHOP_THUM_IMG_DIR), thumpFile);
                                PropertyUtils.getThumImage(Path.Combine(HttpContext.Current.Server.MapPath(path)), 18, 3, thumpPath);
                                imgsSB.Append(ConstantParam.SHOP_THUM_IMG_DIR + thumpFile + ";");
                            }

                            shop.ImgThumbnail = imgsSB.ToString();
                            shop.ImgThumbnail = shop.ImgThumbnail.Substring(0, shop.ImgThumbnail.Length - 1);
                        }

                        shopBll.Update(shop);
                    }
                }
                else
                {
                    resultModel.Msg = APIMessage.NO_USER;
                }
            }
            catch
            {
                resultModel.Msg = APIMessage.REQUEST_EXCEPTION;
            }

            return(resultModel);
        }