Files
KJD/TEMUKJD/WXForms/AdminForms/WXAdminQualificationImageForm.cs

466 lines
20 KiB
C#

using AllBase;
using AllBase.Model.DBModel;
using AllBase.Model.Param.TemuProductManage;
using AllBase.Model.Result;
using NaiBa.Util;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemuKJD.BaseForms;
using TemuKJD.BLL;
using TemuKJD.Extension;
using TemuKJD.WXForms.ShopForms;
using Work;
using Work.BLL.Tools;
namespace TemuKJD.WXForms.AdminForms
{
public partial class WXAdminQualificationImageForm : BaseGeneralForm
{
static WXAdminQualificationImageForm inst = null;
public static WXAdminQualificationImageForm Instance
{
get { if (inst == null) inst = new WXAdminQualificationImageForm(); return inst; }
set { inst = value; }
}
public static ConcurrentList<wx_qualification_image_info> lstWxQualificationImageInfo { get; set; } = new ConcurrentList<wx_qualification_image_info>();
public WXAdminQualificationImageForm()
{
InitializeComponent();
Instance = this;
}
private void AdminShopForm_Load(object sender, EventArgs e)
{
this.ucSplitPage1.PageChange += UcSplitPage1_PageChange;
this.dgvShop.AddSelectorColumn("colSelect");
}
bool firstLoad = true;
private void AdminShopForm_VisibleChanged(object sender, EventArgs e)
{
if (this.Visible == true && firstLoad)
{
firstLoad = false;
this.pbLoading.Visible = true;
SearchImg(this.ucSplitPage1.CurrentPage);
}
}
private void UcSplitPage1_PageChange(object obj)
{
SearchImg(this.ucSplitPage1.CurrentPage);
}
public void SearchImg(int page)
{
List<string> shopIdList = GetCheckedShopIdList();
if (InvokeRequired)
{
Invoke(new Action(() =>
{
this.pbLoading.Visible = true;
this.dgvShop.Rows.Clear();
lstWxQualificationImageInfo.Clear();
}));
}
else
{
this.pbLoading.Visible = true;
this.dgvShop.Rows.Clear();
lstWxQualificationImageInfo.Clear();
}
Task.Run(() =>
{
int pageCount;
decimal seconds = 0;
var catName = "";
Invoke(new Action(() => { catName = txtCatName.Text.Trim(); }));
var list = AdminGetServerQualificationImageLst(out pageCount, out seconds, "", "", page, ucSplitPage1.PageSize);
lstWxQualificationImageInfo.AddRange(list);
this.ucSplitPage1.TotalPage = pageCount;
foreach (var image in lstWxQualificationImageInfo)
{
Invoke(new Action(() =>
{
int rowIndex = this.dgvShop.Rows.Add(
false,
image.cat_name,
image.img_md5,
Base64ChangeImgUrl.Base64ToImage(image.img_thumb_base64),
image.txt_margin,
"编辑",
"删除");
var row = dgvShop.Rows[rowIndex];
row.Height = 60;
}));
}
Invoke(new Action(() =>
{
this.pbLoading.Visible = false;
this.statusBar.lblTaskStatus.Text = "店铺查询完毕";
#if DEBUG
this.statusBar.lblTaskStatus.Text += ",耗时:" + seconds;
#endif
}));
});
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (this.ucSplitPage1.CurrentPage != 1)
this.ucSplitPage1.CurrentPage = 1;
else
SearchImg(1);
}
private void pnlEmpty_Click(object sender, EventArgs e)
{
this.txtCatName.Text = "";
}
bool IsOperate { get; set; } = false;
private void shopLoginForm_SaveComplete(object obj)
{
SearchImg(this.ucSplitPage1.CurrentPage);
}
private void dgvShop_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
string headerText = dgvShop.Columns[e.ColumnIndex].HeaderText;
var row = dgvShop.Rows[e.RowIndex];
switch (headerText)
{
case "删除":
if (BaseMsgForm.ShowConfirm("确定删除吗?删除后不可恢复!").DialogResult == DialogResult.Cancel)
return;
var url = BaseClass.BuildObjString("product/wx_del_qualification_image");
var data = UserLogin.GetSignaturex();
data += "&img_md5=" + row.Cells["colImgMd5"].Value.ParseToString();
var html = GetHtml.PostUtf8(url, data);
if (!html.Contains("\"code\":200"))
BaseMsgForm.ShowError(BaseRegex.GetStringJsonByRegs(ref html, html, "msg"));
SearchImg(ucSplitPage1.CurrentPage);
break;
case "编辑图片":
var image = lstWxQualificationImageInfo.FirstOrDefault(i => i.img_md5 == row.Cells["colImgMd5"].Value.ParseToString());
if (image != null)
{
if (image.img_base64.IsNullOrEmpty())
{
int pageCount;
decimal seconds = 0;
image.img_base64 = AdminGetServerQualificationImageLst(out pageCount, out seconds, "", image.img_md5).FirstOrDefault()?.img_base64;
}
try
{
if (image.txt_margin.ParseToString().Split(',').Select(m => m.ParseToInt()).ToList() is List<int> marginValueList && marginValueList.Count == 4)
{
txtLeft.Text = marginValueList[0].ToString();
txtTop.Text = marginValueList[1].ToString();
txtRight.Text = marginValueList[2].ToString();
txtBottom.Text = marginValueList[3].ToString();
}
var imageSource = Base64ChangeImgUrl.Base64ToImage(image.img_base64);
LoadImg(imageSource);
}
catch (Exception ex)
{
BaseMsgForm.ShowError("加载图片失败" + ex.Message);
}
}
break;
case "":
dgvShop.Rows[e.RowIndex].Cells["colSelect"].Value = !(bool)dgvShop.Rows[e.RowIndex].Cells["colSelect"].Value;
break;
default:
break;
}
}
private List<string> GetCheckedShopIdList()
{
List<string> shopNameList = new List<string>();
for (int i = 0; i < dgvShop.Rows.Count; i++)
{
if (dgvShop.Rows[i].Cells["colSelect"].Value.ParseToBool())
{
string shopName = dgvShop.Rows[i].Cells["colShopId"].Value.ParseToString();
shopName += dgvShop.Rows[i].Cells["colUser"].Value.ParseToString();
shopNameList.Add(shopName);
}
}
return shopNameList;
}
public static List<wx_qualification_image_info> AdminGetServerQualificationImageLst(out int pageCount, out decimal seconds, string cat_name="", string img_md5 = "", int page = 1, int pageSize = 200)
{
List<wx_qualification_image_info> list = new List<wx_qualification_image_info>();
pageCount = 1;
seconds = 0;
var url = BaseClass.BuildObjString("product/wx_get_qualification_image");
var data = UserLogin.GetSignaturex() +
"&page=" + page +
"&page_size=" + pageSize;
if (!string.IsNullOrEmpty(img_md5))
{
data += "&img_md5=" + img_md5;
data += "&is_detail=true";
}
if (!cat_name.IsNullOrEmpty())
{
data += "&cat_name=" + cat_name;
}
var html = GetHtml.PostUtf8(url, data);
if (!html.Contains("\"code\":200"))
return list;
int count = BaseRegex.GetIntJson(ref html, "count");
pageCount = Math.Ceiling(1.0 * count / pageSize).ParseToInt();
pageCount = Math.Max(pageCount, 1);
seconds = BaseRegex.GetDecimalJson(ref html, "seconds");
JObject jObj = JsonHelper.JObjectParse(html);
return JsonHelper.DeserializeJsonToList<wx_qualification_image_info>(jObj["data"].ParseToString());
}
private void dgvShop_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
}
public static Image EmptyImage = null;
private void btnUploadImg_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
// 设置过滤条件,只显示文本文件
openFileDialog.Filter = "图片文件 (*.jpeg;*.jpg;*.png)|*.jpeg;*.jpg;*.png|All files (*.*)|*.*";
// 显示对话框并获取结果
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
string filePath = openFileDialog.FileName;
Image imageSource = Image.FromFile(filePath);
LoadImg(imageSource);
}
catch (Exception ex)
{
BaseMsgForm.ShowError("打开自定义sku图片失败 " + ex.Message);
return;
}
}
}
string imgBase64 = "";
public void LoadImg(Image imageSource)
{
EmptyImage = new Bitmap(900, 1200);
using (Graphics g = Graphics.FromImage(EmptyImage))
{
g.DrawImage(imageSource, new Rectangle(0, 0, EmptyImage.Width, EmptyImage.Height), 0, 0, imageSource.Width, imageSource.Height, GraphicsUnit.Pixel);
}
using (MemoryStream memoryStream = new MemoryStream())
{
EmptyImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = memoryStream.ToArray();
imgBase64 = Convert.ToBase64String(imageBytes);
}
this.pictureBox1.ImageLocation = "";
Image image = new Bitmap(900, 1200);
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(EmptyImage, new Rectangle(0, 0, image.Width, image.Height), 0, 0, EmptyImage.Width, EmptyImage.Height, GraphicsUnit.Pixel);
}
DrawText(image, txtLeft.Text.ParseToInt(), txtTop.Text.ParseToInt(), txtRight.Text.ParseToInt(), txtBottom.Text.ParseToInt());
this.pictureBox1.Image = image;
}
public static void DrawText(Image img, int _txtLeft, int _txtTop, int _txtRight, int _txtBottom)
{
if (img == null)
return;
string bookName = "行走中国 : 人文风情和自然生态发现之旅";
string author = "雷东军";
string publisher = "中国工人出版社";
string isbn = "978-7-500-87761-5";
string binding = "装  帧:平装";
string edition = "版  次:1";
string publishTime = "2021-12-01";
string bookPrice = "37.39";
var bookImg = img;
var imgH = bookImg.Height;
var imgW = bookImg.Width;
using (Graphics g = Graphics.FromImage(bookImg))
{
Brush textBrush = Brushes.Black;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
float txtTop = _txtTop;
float txtBottom = _txtBottom;
float txtLeft = _txtLeft;
float txtRight = _txtRight;
int txtWidth = (int)imgW - (int)txtLeft - (int)txtRight; // 370;
// 书名
Font font = new Font("宋体", 22, FontStyle.Bold);
RectangleF rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop - 80);
g.DrawString(bookName.Replace("图书名称:", ""), font, textBrush, rectF);
// 测量文本的高度 决定接下来文字绘制位置
var textSize = g.MeasureString(bookName.Replace("图书名称:", ""), font, txtWidth);
txtTop += 20 + textSize.Height;
font = new Font("宋体", 15, FontStyle.Bold);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop - 80);
g.DrawString(author.Replace("作  者:", "").TrimEnd('著', ' ') + " 著", font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(author.Replace("作  者:", "").TrimEnd('著', ' ') + " 著", font, txtWidth);
txtTop += 20 + textSize.Height;
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop - 80);
g.DrawString(publisher.Replace("出版发行:", ""), font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(publisher.Replace("出版发行:", ""), font, txtWidth);
txtTop += 20 + textSize.Height;
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop - 80);
g.DrawString(isbn.Replace("ISBN/CN刊号:", "ISBN "), font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(isbn.Replace("ISBN/CN刊号:", "ISBN "), font, txtWidth);
txtTop += 20 + textSize.Height;
// 以下从下往上写
txtTop = imgH - txtBottom;
// 测量文本的高度
textSize = g.MeasureString(binding, font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(binding, font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(edition, font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(edition, font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(publishTime, font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(publishTime, font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(author, font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(author, font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(bookPrice + "元", font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(bookPrice + "元", font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(bookName, font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(bookName, font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(isbn.Replace("ISBN/CN刊号:", "书  号:ISBN "), font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(isbn.Replace("ISBN/CN刊号:", "书  号:ISBN "), font, textBrush, rectF);
// 测量文本的高度
textSize = g.MeasureString(publisher, font, txtWidth);
txtTop -= (10 + textSize.Height);
rectF = new RectangleF(txtLeft, txtTop, txtWidth, imgH - txtTop);
g.DrawString(publisher, font, textBrush, rectF);
}
}
private void txt_TextChanged(object sender, EventArgs e)
{
if (EmptyImage == null)
return;
Image image = new Bitmap(900, 1200);
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(EmptyImage, new Rectangle(0, 0, image.Width, image.Height), 0, 0, EmptyImage.Width, EmptyImage.Height, GraphicsUnit.Pixel);
}
DrawText(image, txtLeft.Text.ParseToInt(), txtTop.Text.ParseToInt(), txtRight.Text.ParseToInt(), txtBottom.Text.ParseToInt());
this.pictureBox1.Image = image;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (imgBase64.IsNullOrEmpty())
return;
var thumbImage = new Bitmap(90, 120);
using (Graphics g = Graphics.FromImage(thumbImage))
{
g.DrawImage(EmptyImage, new Rectangle(0, 0, thumbImage.Width, thumbImage.Height), 0, 0, EmptyImage.Width, EmptyImage.Height, GraphicsUnit.Pixel);
}
string thumbBase64 = "";
using (MemoryStream memoryStream = new MemoryStream())
{
thumbImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = memoryStream.ToArray();
thumbBase64 = Convert.ToBase64String(imageBytes);
}
var url = BaseClass.BuildWXObjString("product/wx_add_mdy_qualification_image");
var data = "";
Invoke(new Action(() =>
{
data = UserLogin.GetSignaturex();
data += "&img_md5=" + BaseClass.GetMd5(imgBase64);
data += "&img_base64=" + imgBase64;
data += "&txt_margin=" + txtLeft.Text.ParseToInt() + "," + txtTop.Text.ParseToInt() + "," + txtRight.Text.ParseToInt() + "," + txtBottom.Text.ParseToInt();
data += "&img_thumb_base64=" + thumbBase64;
}));
var html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
html = GetHtml.PostUtf8(url, data);
SearchImg(ucSplitPage1.CurrentPage);
}
}
}