Пример #1
0
        /// <summary>
        /// 获取商品相关图片(HeaderPics:轮播图,BodyPics:详情图片;图片数组0、1、2:大、中、小图)
        /// </summary>
        /// <param name="goodsClass">商品类别(0:单张,1:套餐)</param>
        /// <returns></returns>
        public string GetGoodsPics(GoodsClass goodsClass)
        {
            try
            {
                GoodsPic goodsPic = thisData.GetGoodsPics(goodsClass);
                return(new BaseResponseModel <GoodsPic>()
                {
                    StatusCode = Tools.ActionParams.code_ok, JsonData = goodsPic
                }.ToJson());
            }
            catch (Exception)
            {
                return(JsonResponseModel.ErrorJson);

                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// 保存商品图片
        /// </summary>
        /// <param name="goodsType"></param>
        /// <param name="picType"></param>
        /// <param name="files"></param>
        /// <param name="hostingEnvironment"></param>
        internal void SaveGoodsFiles(int goodsType, int picType, IFormFileCollection files, IHostingEnvironment hostingEnvironment)
        {
            long size = 0;
            foreach (var file in files)
            {
                var filename = ContentDispositionHeaderValue
                                .Parse(file.ContentDisposition)
                                .FileName
                                .Trim('"');
                string saveDir = $@"{ConstantProperty.BaseDir}{ConstantProperty.GoodsImagesDir}";
                string dbSaveDir = $@"{ConstantProperty.GoodsImagesDir}";
                if (!Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }
                string exString = filename.Substring(filename.LastIndexOf("."));
                string saveName = Guid.NewGuid().ToString("N");
                filename = $@"{saveDir}{saveName}{exString}";

                size += file.Length;
                FileModel<string[]> fileCard = new FileModel<string[]>();
                using (FileStream fs = System.IO.File.Create(filename))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                    string[] fileUrls = new string[] { $@"{dbSaveDir}{saveName}{exString}" };
                }

                var goodsPicCollection = mongo.GetMongoCollection<GoodsPic>();
                var goodsPic = goodsPicCollection.Find(x => x.GoodsClass == (GoodsClass)goodsType).FirstOrDefault();
                if (goodsPic == null)
                {
                    goodsPic = new GoodsPic() { GoodsClass = (GoodsClass)goodsType };

                    goodsPicCollection.InsertOne(goodsPic);
                }
                if (goodsPic.HeaderPics == null)
                {
                    goodsPicCollection.UpdateOne(x => x.GoodsPicID.Equals(goodsPic.GoodsPicID),
                        Builders<GoodsPic>.Update.Set(x => x.HeaderPics, new List<FileModel<string[]>>()));
                }
                if (goodsPic.BodyPics == null)
                {
                    goodsPicCollection.UpdateOne(x => x.GoodsPicID.Equals(goodsPic.GoodsPicID),
                        Builders<GoodsPic>.Update.Set(x => x.BodyPics, new List<FileModel<string[]>>()));
                }
                ParamsCreate3Img params3Img = new ParamsCreate3Img() { FileName = filename, FileDir = ConstantProperty.GoodsImagesDir };
                params3Img.OnFinish += fileModel =>
                {
                    fileModel.FileID = ObjectId.GenerateNewId();
                    if (picType == 0)
                    {
                        var update = Builders<GoodsPic>.Update.Push(x => x.HeaderPics, fileModel);
                        goodsPicCollection.UpdateOne(x => x.GoodsPicID.Equals(goodsPic.GoodsPicID), update);
                    }
                    else
                    {
                        var update = Builders<GoodsPic>.Update.Push(x => x.BodyPics, fileModel);
                        goodsPicCollection.UpdateOne(x => x.GoodsPicID.Equals(goodsPic.GoodsPicID), update);
                    }
                    mongo.GetMongoCollection<FileModel<string[]>>("FileModel").InsertOne(fileModel);
                };
                //ThreadPool.QueueUserWorkItem(new WaitCallback(ImageTool.Create3Img), params3Img);
                new Thread(new ParameterizedThreadStart(ImageTool.Create3Img)).Start(params3Img);
            }
        }