using AllBase;
using AllBase.Model.Param.FissionManage;
using TemuKJD.KJDForms.BaseForms;
using TemuKJD.KJDForms.ToolForms;
using NaiBa.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work;
namespace TemuKJD.KJDForms.ProductForms
{
class FissionBrowserObject
{
public delegate void InformationReceivedHandler(string information);
public event InformationReceivedHandler InformationReceived;
///
/// 编辑图片后更新到服务器
///
///
///
public string updatePic(string str)
{
string requestData = "data=" + GetHtml.UrlEncodeUtf8(str);
string url = BaseClass.FissionPicUrl;
string result = GetHtml.PostUtf8(url, requestData);
return result.Replace("https:", "http:");
}
string addResult = "";
public string addLocalPic()
{
// 单线程单元(STA)模式运行文件对话框,否则会报错。
System.Threading.Thread InvokeThread = new System.Threading.Thread(new System.Threading.ThreadStart(addLocalPicInvokeMethod));
InvokeThread.SetApartmentState(System.Threading.ApartmentState.STA);
InvokeThread.Start();
InvokeThread.Join();
if (addResult.Equals("图片过大"))
{
BaseMsgForm.ShowInfo("本地图片大小不能超过500KB", "提示");
return "";
}
return addResult;
}
public string addLocalPic(bool isMultiselect, int maxSize = 1024)
{
addLocalPicInvokeMethod(isMultiselect, maxSize);
if (addResult.Equals("图片过大"))
{
BaseMsgForm.ShowInfo(("本地图片大小不能超过" + maxSize + "KB").Replace("1024KB", "1MB"), "提示");
return "";
}
return addResult;
}
private void addLocalPicInvokeMethod()
{
addLocalPicInvokeMethod(true);
}
private string addLocalPicInvokeMethod(bool isMultiselect, int maxSize = 500)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图片文件|*.png;*.jpg;*.jpeg;*.gif";
ofd.ValidateNames = true;
ofd.CheckPathExists = true;
ofd.CheckFileExists = true;
ofd.Multiselect = isMultiselect;
if (ofd.ShowDialog() == DialogResult.OK)
{
var result = "";
foreach (var fName in ofd.FileNames)
{
FileInfo file = new FileInfo(fName);
var size = file.Length;
// 图片大小限制500k;
if (size <= 1024 * maxSize)
{
string strFileName = fName;
var base64 = "data:image/" + System.IO.Path.GetExtension(fName).Replace(".", "").Replace("jpeg", "jpg") +
";base64," + Convert.ToBase64String(System.IO.File.ReadAllBytes(strFileName));
result += updatePic(base64) + "|";
}
else
{
result += "图片过大|";
}
}
addResult = result.TrimEnd('|');
}
return addResult;
}
///
/// 前端处理图片如果有跨域问题,将图片地址传到cs代码解决
///
/// 跨域的图片地址
/// 图片的base64数据
public string getHttpPicBase64(string picUrl)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(
System.Net.WebRequest.Create(picUrl).GetResponse().GetResponseStream());
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, image.RawFormat);
byte[] byteArray = ms.ToArray();
var str64 = "data:image/" + System.IO.Path.GetExtension(picUrl).Replace(".", "").Replace("jpeg", "jpg") +
";base64," + Convert.ToBase64String(byteArray);
ms.Close();
image.Dispose();
return str64;
}
///
/// 使用美图秀秀软件修图
///
/// 图片网址
/// 下载图片后是否打开美图
public string OpenMeitu(string src, bool bOpenMeitu = true)
{
string imgName = System.IO.Path.GetFileNameWithoutExtension(src) +
System.IO.Path.GetExtension(src).Replace(".jpeg", ".jpg");
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string saveFilePath = System.IO.Path.Combine(savePath, imgName);
System.Net.WebRequest imgRequst = System.Net.WebRequest.Create(src);
System.Drawing.Image downImage = System.Drawing.Image.FromStream(imgRequst.GetResponse().GetResponseStream());
downImage.Save(saveFilePath);
downImage.Dispose();
if (!bOpenMeitu) return "success";
if (System.IO.File.Exists(saveFilePath))
{
if (!System.IO.File.Exists(Fission.GetInstance().MeituPath))
{
//MessageBox.Show("请指定美图秀秀启动文件。");
if (MeituMessageDialog.ShowMessageDialog("请指定美图秀秀启动文件。\r\n如未安装美图秀秀,请下载。", "下载美图秀秀,指定启动文件") == "指定启动文件")
{
// 单线程单元(STA)模式运行文件对话框,否则会报错。
System.Threading.Thread InvokeThread = new System.Threading.Thread(new System.Threading.ThreadStart(SetMeituPath));
InvokeThread.SetApartmentState(System.Threading.ApartmentState.STA);
InvokeThread.Start();
InvokeThread.Join();
}
}
if (System.IO.File.Exists(Fission.GetInstance().MeituPath))
{
try
{
int i = 0;
while (i < 2500)
{
System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
if (processList.Any(p => p.ProcessName.ToLower() == "xiuxiu"))
{
System.Threading.Thread.Sleep(250);
i += 250;
continue;
}
break;
}
System.Diagnostics.Process.Start(Fission.GetInstance().MeituPath, saveFilePath);
return "success";
}
catch { }
}
}
return "failed";
}
private void SetMeituPath()
{
Fission.GetInstance().setMeituPath();
}
///
/// 将美图修好的本地文件回传服务器
///
public string PullBackPic(string src)
{
string imgName = Path.GetFileName(src);
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filePath = Path.Combine(savePath, imgName);
filePath = GetMeituHandledFilePath(filePath);
if (!File.Exists(filePath))
{
BaseMsgForm.ShowInfo("未找到修图文件,请确认已使用[美图秀秀]\r\n进行修图且保存路径为[覆盖原图]!", "提示");
return "";
}
var fileByte = System.IO.File.ReadAllBytes(filePath);
//using (FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read))
//{
// System.Drawing.Image img = Image.FromStream(fs);
// var width = img.Width;
// img.Dispose();
// if (width < 480)
// {
// BaseMsgForm.ShowError("详情图宽度不得小于480", "错误");
// return "";
// }
//}
if (fileByte.Length > 1024 * 500)
{
BaseMsgForm.ShowError("本地图片大小不能超过500KB", "错误");
return "";
}
var base64 = "data:image/" + System.IO.Path.GetExtension(filePath).Replace(".", "").Replace("jpeg", "jpg") +
";base64," + Convert.ToBase64String(fileByte);
var uploadResult = updatePic(base64);
return uploadResult;
}
void SendCmd(string information)
{
if (InformationReceived != null)
InformationReceived(information);
}
///
/// 显示大图
///
///
///
public string ShowBigImg(string imgSrc)
{
var f = Fission.GetInstance();
if (f != null && !string.IsNullOrEmpty(imgSrc))
{
string[] str = imgSrc.Split('|');
imgSrc = str[0];
string imgId = "";
if (str.Length > 1)
imgId = str[1];
return f.ShowBigImg(imgSrc, imgId);
}
return "error";
}
///
/// 编辑图片后加载
///
///
///
///
///
public string OnloadImg(string id, string src)
{
//src = Fission.RemovePrefix(src);
//if (src.Contains("deifei.com"))
//{
// var jdImg = Fission.jdTransparentImgList.Find(x => x.id == id);
// if (jdImg != null)
// {
// jdImg.OriginalImgUrl = src;
// return JsonHelper.SerializeObject(new { code = 1, src = "http://" + jdImg.OriginalImgUrl, msg = "原" });
// }
//}
return JsonHelper.SerializeObject(new { code = 0, src = src, msg = "" });
}
///
/// 获取美图秀秀处理过的图片路径
///
/// 初始图片路径
/// 处理后的图片路径
private string GetMeituHandledFilePath(string filePath)
{
string filePathWithoutExt = filePath.Substring(0, filePath.LastIndexOf('.'));
//jpg png bmp为美图秀秀指定的三种图片保存格式
if (File.Exists(filePathWithoutExt + ".jpg"))
{
return filePathWithoutExt + ".jpg";
}
if (File.Exists(filePathWithoutExt + ".png"))
{
return filePathWithoutExt + ".png";
}
if (File.Exists(filePathWithoutExt + ".bmp"))
{
return filePathWithoutExt + ".bmp";
}
return filePath;
}
///
/// 裂变页面 js回调添加入库数据
///
///
///
public string updateFessionDate(string jsonStr)
{
try
{
FissionDataModel fessionDataModel = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr);
fessionDataModel.prop_pic?.Clear();
var props = JsonHelper.DeserializeJsonToObject>(fessionDataModel.props);
foreach (var prop in props)
{
foreach (var value in prop.Values)
{
if (!string.IsNullOrEmpty(value.image))
{
var strImg = value.image;
if (!strImg.StartsWith("http"))
strImg = "http:" + strImg;
fessionDataModel.prop_pic.Add(strImg);
}
}
}
jsonStr = JsonHelper.SerializeObject(fessionDataModel);
// 调用php后台
string requestData = UserLogin.GetSignaturex() + "&act=add&text=" + GetHtml.UrlEncodeUtf8(jsonStr);
string url = BaseClass.BuildFissionObjString("fession_api");
string result = GetHtml.PostUtf8(url, requestData);
if (!result.Contains("\"code\":0"))
{
}
return result;
}
catch (Exception ex)
{
return "{\"code\":1}";
}
}
public string GetFissionPicCountPreLine()
{
var count = ""; // NaiBa.Util.Helper.ConfigHelper.LoadConfig("fission_pic_count_pre_line");
try { count = NaiBa.Util.Helper.FileHelper.ReadAllText("data\\config\\fission_pic_count_pre_line")?.Trim('\"'); } catch { }
if (string.IsNullOrEmpty(count))
return "3";
else
return count;
}
public string SaveFissionPicCountPreLine(string count)
{
try
{
NaiBa.Util.Helper.ConfigHelper.SaveConfig("fission_pic_count_pre_line", count);
return "success";
}
catch
{
}
return "failed";
}
}
}