frmOssTask.cs 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. using LYFZ.BLL.OssCloudDiskSDK;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace LYFZ.Software.MainBusiness.OssTask
  12. {
  13. public class frmOssTask : LYFZ.Software.OssTask.frmOssTask
  14. {
  15. public frmOssTask()
  16. {
  17. MyfrmOssTask = this;
  18. this.Load += FrmOssTask_Load;
  19. this.Shown += FrmOssTask_Shown;
  20. // 按钮事件;
  21. this.btn_refresh.Click += Btn_refresh_Click;
  22. // 窗口关闭事件;
  23. this.FormClosed += FrmOssTask_FormClosed;
  24. // 双击DataGridView;
  25. this.dgv.CellDoubleClick += Dgv_CellDoubleClick;
  26. }
  27. private void Dgv_CellDoubleClick( object sender, DataGridViewCellEventArgs e )
  28. {
  29. if ( e.ColumnIndex == 12 )
  30. {
  31. DataGridViewRow row = this.dgv.Rows[e.RowIndex];
  32. JsonOssTask ossTask = (JsonOssTask)row.Tag;
  33. if ( ossTask != null )
  34. {
  35. // 检查手机合法性;
  36. string okPhones = "";
  37. LYFZ.WinAPI.CustomPublicMethod.ValidationPhones( row.Cells[5].Value.ToString().Replace( '/', ',' ), ref okPhones );
  38. if ( ossTask.OsType == 2 )
  39. {// 手动百度云分享;
  40. if ( string.IsNullOrEmpty( ossTask.BaiduShareUrl ) )
  41. {
  42. OssTask.frmBaiduShareUrl dlg = new frmBaiduShareUrl();
  43. dlg.ShowDialog();
  44. if ( dlg.DialogResult == DialogResult.OK )
  45. {
  46. ossTask.BaiduExtrCode = dlg.tx_code.Text;
  47. ossTask.BaiduShareUrl = dlg.tx_url.Text;
  48. // 发送到web服务器;
  49. ossTask.FinishedTime = DateTime.Now.ToString( "yyyy-MM-dd 00:00:00" );
  50. ossTask.DownloadExpireTime = DateTime.Now.ToString( "yyyy-MM-dd 00:00:00" );
  51. string strResult = LYFZ.WinAPI.CustomPublicMethod.HttpContentPOST( "http://p.lyfz.net/api/ChargeDataControl.ashx/SetChargeDetail", JsonConvert.SerializeObject( ossTask ), Encoding.UTF8 );
  52. ResultCode result = JsonConvert.DeserializeObject<ResultCode>( strResult );
  53. if ( result.code == 200 && result.data.Status )
  54. {
  55. MessageBoxCustom.Show( "推送成功" );
  56. row.Cells[12].Value = ossTask.BaiduShareUrl;
  57. // 更新到服务器;
  58. string strJson = JsonConvert.SerializeObject( ossTask );
  59. LYFZ.Network.PlatinumClientProcess.SendOssTaskJson2Svr( strJson, ossTask.TaskID, true );
  60. }
  61. else
  62. {
  63. MessageBoxCustom.Show( result.info + ". " + result.data.StatusMsg );
  64. }
  65. }
  66. }
  67. else
  68. {
  69. LYFZ.Software.MainBusiness.SMSManagement.SmsSend.FrmSendSms sendSMSDlg = new SMSManagement.SmsSend.FrmSendSms();
  70. sendSMSDlg.Phone = okPhones.Trim().Trim( ',' );
  71. sendSMSDlg.SetDefaultSmsText( string.Format( "{0} 您好, 您的相片已上传到百度云,请前行小程序查看下载地址,提取码:{1}!", row.Cells[3].Value.ToString(), ossTask.BaiduExtrCode ) );
  72. sendSMSDlg.ShowDialog();
  73. }
  74. }
  75. else if ( row.Cells[12].Value != null && !string.IsNullOrEmpty( row.Cells[12].Value.ToString() ) )
  76. {// 阿里或腾讯;
  77. LYFZ.Software.MainBusiness.SMSManagement.SmsSend.FrmSendSms sendSMSDlg = new SMSManagement.SmsSend.FrmSendSms();
  78. sendSMSDlg.Phone = okPhones.Trim().Trim( ',' );
  79. sendSMSDlg.SetDefaultSmsText( string.Format( "{0} 您好, 您的相片已上传完成,请前行小程序查看下载地址!", row.Cells[3].Value.ToString() ) );
  80. sendSMSDlg.ShowDialog();
  81. }
  82. }
  83. }
  84. }
  85. private void FrmOssTask_Shown( object sender, EventArgs e )
  86. {
  87. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod( delegate ( object obj, BackgroundWorker backgroundWorker )
  88. {
  89. backgroundWorker.ReportProgress( 0, "正在加载中,请稍等..." );
  90. // 从服务器加载文件数据;
  91. List<string> listJson;
  92. if ( LYFZ.Network.PlatinumClientProcess.GetOssTaskInfoFromSvr( out listJson ) )
  93. {
  94. LoadOssTask2( listJson );
  95. PaddingData();
  96. }
  97. } );
  98. }
  99. /// <summary>
  100. /// 窗口关闭事件;
  101. /// </summary>
  102. /// <param name="sender"></param>
  103. /// <param name="e"></param>
  104. private void FrmOssTask_FormClosed( object sender, FormClosedEventArgs e )
  105. {
  106. MyfrmOssTask = null;
  107. }
  108. /// <summary>
  109. /// 窗口加载事件;
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. private void FrmOssTask_Load( object sender, EventArgs e )
  114. {
  115. }
  116. /// <summary>
  117. /// 刷新按钮;
  118. /// </summary>
  119. /// <param name="sender"></param>
  120. /// <param name="e"></param>
  121. private void Btn_refresh_Click( object sender, EventArgs e )
  122. {
  123. if ( !CanUploadImage( _uploadOss.AppInterface.AppEnId ) )
  124. {
  125. MessageBoxCustom.Show( "无上传权限,请充值流量!" );
  126. return;
  127. }
  128. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod( delegate ( object obj, BackgroundWorker backgroundWorker )
  129. {
  130. backgroundWorker.ReportProgress( 0, "正在加载中,请稍等..." );
  131. // 清空datagridview;
  132. dgv.Rows.Clear();
  133. // 从服务器加载文件数据;
  134. List<string> listJson;
  135. if ( LYFZ.Network.PlatinumClientProcess.GetOssTaskInfoFromSvr( out listJson ) )
  136. {
  137. LoadOssTask2( listJson );
  138. PaddingData();
  139. }
  140. } );
  141. }
  142. /// <summary>
  143. /// 填充数据;
  144. /// </summary>
  145. private void PaddingData()
  146. {
  147. this.dgv.Rows.Clear();
  148. if ( _listOssTask == null ) return;
  149. DataGridViewRow data = null;
  150. foreach ( JsonOssTask item in _listOssTask )
  151. {
  152. data = new DataGridViewRow();
  153. data.Tag = item;
  154. data.CreateCells( this.dgv );
  155. data.Cells[0].Value = item.TaskID;
  156. data.Cells[1].Value = item.OrderNumber;
  157. data.Cells[2].Value = item.OrderType;
  158. data.Cells[3].Value = item.CusNames;
  159. data.Cells[4].Value = item.CusGenders;
  160. data.Cells[5].Value = item.CusPhones;
  161. data.Cells[6].Value = item.PhotosType;
  162. data.Cells[7].Value = item.ShootName;
  163. data.Cells[8].Value = item.DownloadBranchIDs;
  164. // 上传完成的数量;
  165. int nCompletedCount = 0;
  166. if ( item.ArrayPhotos != null )
  167. {
  168. foreach ( PhotoArray array in item.ArrayPhotos )
  169. {
  170. if ( array.ListPhotos == null )
  171. continue;
  172. foreach ( PhotoItem phitem in array.ListPhotos )
  173. {
  174. if ( !string.IsNullOrEmpty( phitem.PhotoUrl ) )
  175. nCompletedCount++;
  176. }
  177. }
  178. // 上传进度;
  179. data.Cells[9].Value = string.Format( "{0}/{1}", nCompletedCount, item.PhotoCount );
  180. }
  181. data.Cells[10].Value = item.FinishedTime;
  182. data.Cells[11].Value = item.DownloadExpireTime;
  183. // 如果已经完成,生成连接;
  184. PaddingUrl( item, data );
  185. this.dgv.Rows.Add( data );
  186. }
  187. }
  188. /// <summary>
  189. /// 填充url到DataGridView中;
  190. /// </summary>
  191. /// <param name="ossTask"></param>
  192. /// <param name="row"></param>
  193. private void PaddingUrl( JsonOssTask ossTask, DataGridViewRow row )
  194. {
  195. // check arg;
  196. if ( ossTask == null || row == null )
  197. return;
  198. // 如果已经完成,生成连接;
  199. if ( !string.IsNullOrEmpty( ossTask.FinishedTime ) )
  200. {
  201. if ( ossTask.OsType == 2 )
  202. row.Cells[12].Value = ossTask.BaiduShareUrl;
  203. else
  204. row.Cells[12].Value = GenerateUrl( ossTask.TaskID );
  205. }
  206. }
  207. /// <summary>
  208. /// 生成客户云相片下载地址;
  209. /// </summary>
  210. /// <param name="strTaskId"></param>
  211. /// <returns></returns>
  212. private static string GenerateUrl( string strTaskId )
  213. {
  214. return string.Format( "http://p.lyfz.net/ImgDownload.aspx?id={0}", strTaskId );
  215. }
  216. #region 成员变量或静态变量;
  217. public static frmOssTask MyfrmOssTask { get; set; } = null;
  218. // 是否加载过任务文件;
  219. public static bool IsTaskloaded { get; set; } = false;
  220. // 安全锁;
  221. private static object _lockJson = new object();
  222. // 安全锁;
  223. private static object _lockTask = new object();
  224. // 上传下载oss的执行对象;
  225. private static BLL.OssCloudDiskSDK.UploadOssCloudDisk _uploadOss = new UploadOssCloudDisk();
  226. // 任务Json数据队列;
  227. private static List<JsonOssTask> _listOssTask = null;
  228. // 任务线程对象;
  229. private static System.Threading.Thread _thread = null;
  230. // 任务Task对象队列;
  231. private static List<System.Threading.Tasks.Task> _listTask = null;
  232. private static string _host_url = "";
  233. #endregion
  234. #region 静态方法
  235. public static void SendBaiduShare2WebSrv( JsonOssTask ossTask )
  236. {
  237. // 发送到web服务器;
  238. ossTask.FinishedTime = DateTime.Now.ToString( "yyyy-MM-dd 00:00:00" );
  239. ossTask.DownloadExpireTime = DateTime.Now.ToString( "yyyy-MM-dd 00:00:00" );
  240. string strResult = LYFZ.WinAPI.CustomPublicMethod.HttpContentPOST( "http://p.lyfz.net/api/ChargeDataControl.ashx/SetChargeDetail", JsonConvert.SerializeObject( ossTask ), Encoding.UTF8 );
  241. ResultCode result = JsonConvert.DeserializeObject<ResultCode>( strResult );
  242. if ( result.code == 200 && result.data.Status )
  243. {
  244. MessageBoxCustom.Show( "推送成功" );
  245. // 更新到服务器;
  246. string strJson = JsonConvert.SerializeObject( ossTask );
  247. LYFZ.Network.PlatinumClientProcess.SendOssTaskJson2Svr( strJson, ossTask.TaskID, true );
  248. }
  249. else
  250. {
  251. MessageBoxCustom.Show( result.info + ". " + result.data.StatusMsg );
  252. }
  253. }
  254. /// <summary>
  255. /// task list is null or empty;
  256. /// </summary>
  257. /// <returns></returns>
  258. public static bool IsTaskListNullOrEmpty()
  259. {
  260. if ( _listOssTask == null || _listOssTask.Count == 0 )
  261. return true;
  262. return false;
  263. }
  264. /// <summary>
  265. /// 是否具有企业ID;
  266. /// </summary>
  267. /// <returns></returns>
  268. public static bool IsExistsEnterpriseID()
  269. {
  270. if ( _uploadOss.AppInterface == null || string.IsNullOrEmpty( _uploadOss.AppInterface.AppEnId ) )
  271. return false;
  272. return true;
  273. }
  274. public static bool CanUploadImage()
  275. {
  276. string url = string.Format( "http://p.lyfz.net/api/ChargeDataControl.ashx/CheckChargeDetailData?EntID={0}", _uploadOss.AppInterface.AppEnId );
  277. string strResult = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET( url, Encoding.UTF8 );
  278. ResultCode result = JsonConvert.DeserializeObject<ResultCode>( strResult );
  279. if ( result.code == 200 && result.data.Status )
  280. {
  281. return true;
  282. }
  283. return false;
  284. }
  285. public static bool CanUploadImageEx()
  286. {
  287. if ( _uploadOss.AppInterface == null || string.IsNullOrEmpty( _uploadOss.AppInterface.AppEnId ) )
  288. return false;
  289. string url = string.Format( "http://p.lyfz.net/api/ChargeDataControl.ashx/CheckChargeDetailData?EntID={0}", _uploadOss.AppInterface.AppEnId );
  290. string strResult = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET( url, Encoding.UTF8 );
  291. ResultCode result = JsonConvert.DeserializeObject<ResultCode>( strResult );
  292. if ( result.code == 200 && result.data.Status )
  293. {
  294. return true;
  295. }
  296. return false;
  297. }
  298. public static bool CanUploadImage( string entId )
  299. {
  300. string url = string.Format( "http://p.lyfz.net/api/ChargeDataControl.ashx/CheckChargeDetailData?EntID={0}", entId );
  301. string strResult = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET( url, Encoding.UTF8 );
  302. ResultCode result = JsonConvert.DeserializeObject<ResultCode>( strResult );
  303. if ( result.code == 200 && result.data.Status )
  304. {
  305. return true;
  306. }
  307. return false;
  308. }
  309. /// <summary>
  310. /// 生成客户云链接的上传任务;
  311. /// </summary>
  312. /// <param name="order">订单号</param>
  313. /// <param name="cus_name">客户姓名</param>
  314. /// <param name="cus_gender">客户性别</param>
  315. /// <param name="cus_tel">客户电话</param>
  316. /// <param name="photoType">相片类型:原片, 初修, 精修, 设计, 选好的片</param>
  317. /// <param name="shootName">拍摄名称</param>
  318. /// <param name="listImages">要上传的相片路径, 如果为null则要手动获取相片路径</param>
  319. public static JsonOssTask GenerateOssTask(
  320. int osType,
  321. string downloadExpire,
  322. int downloadTiems,
  323. string order,
  324. string viceOrder,
  325. string order_type,
  326. string cus_name,
  327. string cus_gender,
  328. string cus_tel,
  329. string photoType,
  330. string shootName, List<string> listImagesPath = null,
  331. string download_branchs = "" )
  332. {
  333. if ( string.IsNullOrEmpty( order ) )
  334. return null;
  335. if ( string.IsNullOrEmpty( _uploadOss.AppInterface.AppEnId ) )
  336. {
  337. MessageBoxCustom.Show( "未注册云账号,请联系服务商注册" );
  338. return null;
  339. }
  340. // 创建任务;
  341. JsonOssTask jsonOssTask = new JsonOssTask();
  342. // 企业编号;
  343. jsonOssTask.EnterpriseID = _uploadOss.AppInterface.AppEnId;
  344. // 本店分店ID(上传分店ID)
  345. jsonOssTask.UploadBranchID = LYFZ.BLL.BLL_ErpCompanyInfo.CurrentCompanyInfoID;
  346. // 下载的分店ID串, ID值前后使用'#'分隔;
  347. jsonOssTask.DownloadBranchIDs = download_branchs;
  348. jsonOssTask.OrderNumber = order;
  349. jsonOssTask.OrderViceNumber = viceOrder;
  350. jsonOssTask.OrderType = order_type;
  351. jsonOssTask.CusNames = cus_name;
  352. jsonOssTask.CusGenders = cus_gender;
  353. jsonOssTask.CusPhones = cus_tel;
  354. jsonOssTask.ShootName = shootName;
  355. jsonOssTask.PhotosType = photoType;
  356. // 创建时,默认完成时间空;
  357. jsonOssTask.FinishedTime = "";
  358. jsonOssTask.listPhotos = new List<PhotoItem>();
  359. jsonOssTask.OsType = osType;
  360. if ( osType == -1 )
  361. {// 阿里或腾讯云;
  362. jsonOssTask.DownloadExpireTime = downloadExpire;
  363. jsonOssTask.CustomizeDownloadTimes = downloadTiems;
  364. }
  365. // 任务id;
  366. jsonOssTask.TaskID = LYFZ.WinAPI.SDKSecurity.MD5Encrypt( string.Format( "{0}{1}{2}{3}{4}{5}",
  367. jsonOssTask.OsType, jsonOssTask.EnterpriseID, jsonOssTask.UploadBranchID, order, viceOrder, shootName ) );
  368. if ( listImagesPath == null )
  369. return jsonOssTask;
  370. ulong nPhotoSize = 0;
  371. foreach ( string file in listImagesPath )
  372. {
  373. // 文件是否存在;
  374. if ( !System.IO.File.Exists( file ) || _uploadOss == null )
  375. continue;
  376. // 文件大小;
  377. System.IO.FileInfo fileInfo = new System.IO.FileInfo( file );
  378. nPhotoSize += (ulong)fileInfo.Length;
  379. PhotoItem photoItem = new PhotoItem( System.IO.Path.GetFileName( file ).Remove( 0, 9 /*'Original_'*/), file, fileInfo.Length, ""/*创建时,默认url空*/ );
  380. // 添加到list中;
  381. jsonOssTask.listPhotos.Add( photoItem );
  382. }
  383. jsonOssTask.PhotoCount = jsonOssTask.listPhotos.Count;
  384. jsonOssTask.PhotoSize = nPhotoSize;
  385. return jsonOssTask;
  386. }
  387. /// <summary>
  388. /// 生成任务Json, 并发送给服务器;
  389. /// </summary>
  390. /// <param name="order">订单号</param>
  391. /// <param name="order_type">订单类型,可能用于统计使用</param>
  392. /// <param name="cus_name">用户姓名,用于短信发送</param>
  393. /// <param name="cus_gender">用户性别,用于短信发送</param>
  394. /// <param name="cus_tel">用户电话,用于短信发送</param>
  395. /// <param name="photoType">上传的相片类型, 用于统计查看,类型有:原片,初修, 选好的片,精修,设计</param>
  396. /// <param name="shootName">拍摄名称,可能用于统计查看</param>
  397. /// <param name="listImagesPath">要上传的相片绝对路径集合</param>
  398. /// <param name="download_branchs">用于分店传片,指定能下载相片的分店, 如果该值为空,则没有分店要下载</param>
  399. /// <returns></returns>
  400. public static bool GenerateOssTaskEx(
  401. int osType,
  402. string downloadExpire,
  403. int downloadTiems,
  404. string order,
  405. string order_type,
  406. string cus_name,
  407. string cus_gender,
  408. string cus_tel,
  409. string photoType,
  410. string shootName, List<string> listImagesPath = null,
  411. string download_branchs = "" )
  412. {
  413. if ( string.IsNullOrEmpty( order ) )
  414. return false;
  415. if ( string.IsNullOrEmpty( _uploadOss.AppInterface.AppEnId ) )
  416. {
  417. MessageBoxCustom.Show( "未注册云账号,请联系服务商注册" );
  418. return false;
  419. }
  420. if ( listImagesPath == null )
  421. {// 需要重新获取相片;
  422. listImagesPath = new List<string>();
  423. // 暂时不处理这块,默认返回false;
  424. return false;
  425. }
  426. else
  427. {
  428. // 创建任务;
  429. JsonOssTask jsonOssTask = new JsonOssTask();
  430. // 企业编号;
  431. jsonOssTask.EnterpriseID = _uploadOss.AppInterface.AppEnId;
  432. // 本店分店ID(上传分店ID)
  433. jsonOssTask.UploadBranchID = LYFZ.BLL.BLL_ErpCompanyInfo.CurrentCompanyInfoID;
  434. // 下载的分店ID串, ID值前后使用'#'分隔;
  435. jsonOssTask.DownloadBranchIDs = download_branchs;
  436. jsonOssTask.OrderNumber = order;
  437. jsonOssTask.OrderType = order_type;
  438. jsonOssTask.CusNames = cus_name;
  439. jsonOssTask.CusGenders = cus_gender;
  440. jsonOssTask.CusPhones = cus_tel;
  441. jsonOssTask.ShootName = shootName;
  442. jsonOssTask.PhotosType = photoType;
  443. // 创建时,默认完成时间空;
  444. jsonOssTask.FinishedTime = "";
  445. jsonOssTask.listPhotos = new List<PhotoItem>();
  446. jsonOssTask.OsType = osType;
  447. if ( osType == -1 )
  448. {// 阿里或腾讯云;
  449. jsonOssTask.DownloadExpireTime = downloadExpire;
  450. jsonOssTask.CustomizeDownloadTimes = downloadTiems;
  451. }
  452. // 任务id;
  453. jsonOssTask.TaskID = LYFZ.WinAPI.SDKSecurity.MD5Encrypt( string.Format( "{0}{1}{2}{3}{4}{5}{6}", jsonOssTask.OsType, jsonOssTask.EnterpriseID, jsonOssTask.UploadBranchID, order, order_type, photoType, shootName ) );
  454. ulong nPhotoSize = 0;
  455. foreach ( string file in listImagesPath )
  456. {
  457. // 文件是否存在;
  458. if ( !System.IO.File.Exists( file ) || _uploadOss == null )
  459. continue;
  460. // 文件大小;
  461. System.IO.FileInfo fileInfo = new System.IO.FileInfo( file );
  462. nPhotoSize += (ulong)fileInfo.Length;
  463. PhotoItem photoItem = new PhotoItem( System.IO.Path.GetFileName( file ).Remove( 0, 9 /*'Original_'*/), file, fileInfo.Length, ""/*创建时,默认url空*/ );
  464. // 添加到list中;
  465. jsonOssTask.listPhotos.Add( photoItem );
  466. }
  467. jsonOssTask.PhotoCount = jsonOssTask.listPhotos.Count;
  468. jsonOssTask.PhotoSize = nPhotoSize;
  469. // 将对象转储为Json格式的文件;
  470. if ( jsonOssTask != null )
  471. {
  472. try
  473. {
  474. string strJson = JsonConvert.SerializeObject( jsonOssTask );
  475. return LYFZ.Network.PlatinumClientProcess.SendOssTaskJson2Svr( strJson, jsonOssTask.TaskID );
  476. }
  477. catch ( Exception e )
  478. {
  479. return false;
  480. }
  481. }
  482. }
  483. return false;
  484. }
  485. public static void AddPhoto2TaskObj( JsonOssTask jsonOssTask, string strPhotoType, List<string> listPhotos )
  486. {
  487. if ( listPhotos == null || jsonOssTask == null )
  488. return;
  489. OssTask.PhotoArray photoArray = null;
  490. foreach ( PhotoArray item in jsonOssTask.ArrayPhotos )
  491. {
  492. if ( item.PhotosType.ToUpper() == strPhotoType.ToUpper() )
  493. {
  494. photoArray = item;
  495. if ( photoArray.ListPhotos == null )
  496. photoArray.ListPhotos = new List<PhotoItem>();
  497. break;
  498. }
  499. }
  500. if ( photoArray == null )
  501. {
  502. photoArray = new PhotoArray();
  503. photoArray.PhotosType = strPhotoType;
  504. if ( photoArray.ListPhotos == null )
  505. photoArray.ListPhotos = new List<PhotoItem>();
  506. jsonOssTask.ArrayPhotos.Add( photoArray );
  507. }
  508. ulong nPhotoSize = 0;
  509. foreach ( string file in listPhotos )
  510. {
  511. // 文件是否存在;
  512. if ( !System.IO.File.Exists( file ) || _uploadOss == null )
  513. continue;
  514. // 文件大小;
  515. System.IO.FileInfo fileInfo = new System.IO.FileInfo( file );
  516. nPhotoSize += (ulong)fileInfo.Length;
  517. OssTask.PhotoItem photoItem = new OssTask.PhotoItem( System.IO.Path.GetFileName( file ).Remove( 0, 9 /*'Original_'*/), file, fileInfo.Length, ""/*创建时,默认url空*/ );
  518. // 添加到list中;
  519. photoArray.ListPhotos.Add( photoItem );
  520. }
  521. jsonOssTask.PhotoCount += listPhotos.Count;
  522. jsonOssTask.PhotoSize += nPhotoSize;
  523. }
  524. public static bool SendOssTask2Svr( JsonOssTask jsonOssTask )
  525. {
  526. if ( jsonOssTask != null )
  527. {
  528. try
  529. {
  530. string strJson = JsonConvert.SerializeObject( jsonOssTask );
  531. return LYFZ.Network.PlatinumClientProcess.SendOssTaskJson2Svr( strJson, jsonOssTask.TaskID );
  532. }
  533. catch ( Exception e )
  534. {
  535. return false;
  536. }
  537. }
  538. return false;
  539. }
  540. /// <summary>
  541. /// 获取已选好的相片;
  542. /// </summary>
  543. /// <param name="order"></param>
  544. /// <param name="viceOrder"></param>
  545. /// <returns></returns>
  546. public static List<string> GetSelectedPhotos( string order, string viceOrder, string strScenery )
  547. {
  548. LYFZ.BLL.BLL_ErpOrderDigital bLL_ErpOrderDigital = new BLL.BLL_ErpOrderDigital();
  549. LYFZ.Model.Model_ErpOrderDigital model = bLL_ErpOrderDigital.GetModel( "Ordv_ViceNumber", viceOrder );
  550. try
  551. {
  552. // 将字符串转成枚举;
  553. Model.Model_ErpOrder.DirectoryType directoryType = (Model.Model_ErpOrder.DirectoryType)Enum.Parse( typeof( Model.Model_ErpOrder.DirectoryType ), model.Ordv_SelectedPhotoType );
  554. // 相片类型;
  555. string strPhotosType = (directoryType == Model.Model_ErpOrder.DirectoryType.EarlyRetouch) ? "初修" : "原片";
  556. // 相片串,解析出相片名;
  557. string strImages = model.Ordv_SelectionFilm;
  558. if ( string.IsNullOrEmpty( strImages ) )
  559. {
  560. //MessageBoxCustom.Show( "该订单未有选片记录,不可生成任务" );
  561. return null;
  562. }
  563. int nStart = 0/*冒号位置*/, nEndof = 0/*星号位置*/;
  564. string strResult; // 选片结果;
  565. List<string> listGoodsImage = strImages.Split( '|' ).ToList();
  566. List<string> listImages = new List<string>();
  567. foreach ( string item in listGoodsImage )
  568. {
  569. nStart = item.IndexOf( ':' ) + 1;
  570. nEndof = item.IndexOf( '*' );
  571. if ( nEndof != -1 && nStart >= nEndof )
  572. continue;
  573. strResult = (nEndof == -1 ? item.Substring( nStart ) : item.Substring( nStart, nEndof - nStart ));
  574. listImages = strResult.Split( '?' ).ToList().Union( listImages ).ToList();
  575. //listImages = item.Substring( item.IndexOf( ':' ) + 1 ).Split( '?' ).ToList().Union( listImages ).ToList();
  576. }
  577. // 实际相片的目录;
  578. BLL.SelectPhotoHandling photoHandling = new BLL.SelectPhotoHandling( new BLL.BLL_ErpOrder().GetModel( "Ord_Number", order ), bLL_ErpOrderDigital.GetModel( "Ordv_ViceNumber", viceOrder ) );
  579. List<string> listPaths = BLL.SelectPhotoHandling.GetFromServerSpecifyDirectoryList( directoryType );
  580. List<string> listFiles = photoHandling.GetSpecifyTypePhotoList( directoryType, BLL.SelectPhotoHandling.PhotoType.Original, strScenery );
  581. List<string> listPhotos = new List<string>();
  582. foreach ( string item in listImages )
  583. {
  584. for ( int i = 0; i < listFiles.Count; i++ )
  585. {
  586. // 文件名;
  587. string name = System.IO.Path.GetFileName( listFiles[i] ).Remove( 0, 9 /*'Original_'*/);
  588. if ( item.ToUpper() == name.ToUpper() )
  589. {
  590. listPhotos.Add( listFiles[i] );
  591. listFiles.RemoveAt( i );
  592. break;
  593. }
  594. }
  595. }
  596. return listPhotos;
  597. }
  598. catch
  599. {
  600. // 可能字符串转枚举时出错了;
  601. }
  602. return null;
  603. }
  604. public static List<string> GetSelectedPhotos( string order, string viceOrder, string strScenery, Model.Model_ErpOrder.DirectoryType directoryType )
  605. {
  606. LYFZ.BLL.BLL_ErpOrderDigital bLL_ErpOrderDigital = new BLL.BLL_ErpOrderDigital();
  607. LYFZ.Model.Model_ErpOrderDigital model = bLL_ErpOrderDigital.GetModel( "Ordv_ViceNumber", viceOrder );
  608. try
  609. {
  610. // 将字符串转成枚举;
  611. //Model.Model_ErpOrder.DirectoryType directoryType = (Model.Model_ErpOrder.DirectoryType)Enum.Parse( typeof( Model.Model_ErpOrder.DirectoryType ), model.Ordv_SelectedPhotoType );
  612. // 相片类型;
  613. //string strPhotosType = (directoryType == Model.Model_ErpOrder.DirectoryType.EarlyRetouch) ? "初修" : "原片";
  614. // 相片串,解析出相片名;
  615. string strImages = model.Ordv_SelectionFilm;
  616. if ( string.IsNullOrEmpty( strImages ) )
  617. {
  618. //MessageBoxCustom.Show( "该订单未有选片记录,不可生成任务" );
  619. return null;
  620. }
  621. int nStart = 0/*冒号位置*/, nEndof = 0/*星号位置*/;
  622. string strResult; // 选片结果;
  623. List<string> listGoodsImage = strImages.Split( '|' ).ToList();
  624. List<string> listImages = new List<string>();
  625. foreach ( string item in listGoodsImage )
  626. {
  627. nStart = item.IndexOf( ':' ) + 1;
  628. nEndof = item.IndexOf( '*' );
  629. if ( nEndof != -1 && nStart >= nEndof )
  630. continue;
  631. strResult = (nEndof == -1 ? item.Substring( nStart ) : item.Substring( nStart, nEndof - nStart ));
  632. listImages = strResult.Split( '?' ).ToList().Union( listImages ).ToList();
  633. //listImages = item.Substring( item.IndexOf( ':' ) + 1 ).Split( '?' ).ToList().Union( listImages ).ToList();
  634. }
  635. // 实际相片的目录;
  636. BLL.SelectPhotoHandling photoHandling = new BLL.SelectPhotoHandling( new BLL.BLL_ErpOrder().GetModel( "Ord_Number", order ), bLL_ErpOrderDigital.GetModel( "Ordv_ViceNumber", viceOrder ) );
  637. List<string> listPaths = BLL.SelectPhotoHandling.GetFromServerSpecifyDirectoryList( directoryType );
  638. List<string> listFiles = photoHandling.GetSpecifyTypePhotoList( directoryType, BLL.SelectPhotoHandling.PhotoType.Original, strScenery );
  639. List<string> listPhotos = new List<string>();
  640. foreach ( string item in listImages )
  641. {
  642. for ( int i = 0; i < listFiles.Count; i++ )
  643. {
  644. // 文件名;
  645. string name = System.IO.Path.GetFileName( listFiles[i] ).Remove( 0, 9 /*'Original_'*/);
  646. if ( item.ToUpper() == name.ToUpper() )
  647. {
  648. listPhotos.Add( listFiles[i] );
  649. listFiles.RemoveAt( i );
  650. break;
  651. }
  652. }
  653. }
  654. return listPhotos;
  655. }
  656. catch
  657. {
  658. // 可能字符串转枚举时出错了;
  659. }
  660. return null;
  661. }
  662. /// <summary>
  663. /// 从目录中加载任务文件;
  664. /// </summary>
  665. public static void LoadOssTask()
  666. {
  667. if ( !System.IO.Directory.Exists( JsonOssTask.TaskJsonPath ) )
  668. {
  669. System.IO.Directory.CreateDirectory( JsonOssTask.TaskJsonPath );
  670. return;
  671. }
  672. // 加载任务文件;
  673. string strJson = null;
  674. var files = System.IO.Directory.GetFiles( JsonOssTask.TaskJsonPath, "*.Json" );
  675. foreach ( var file in files )
  676. {
  677. strJson = System.IO.File.ReadAllText( file );
  678. try
  679. {
  680. if ( !IsTaskExists( System.IO.Path.GetFileNameWithoutExtension( file ) ) )
  681. {
  682. JsonOssTask jsonOssTask = JsonConvert.DeserializeObject<JsonOssTask>( strJson );
  683. PushTask( jsonOssTask );
  684. }
  685. }
  686. catch { }
  687. }
  688. // 标识加载过;
  689. IsTaskloaded = true;
  690. }
  691. public static void LoadOssTask2( List<string> listJson )
  692. {
  693. // 加载前,先移除之前的;
  694. if ( _listOssTask != null )
  695. _listOssTask.Clear();
  696. foreach ( var strJson in listJson )
  697. {
  698. try
  699. {
  700. JsonOssTask jsonOssTask = JsonConvert.DeserializeObject<JsonOssTask>( strJson );
  701. if ( !IsTaskExists( jsonOssTask.TaskID ) )
  702. {
  703. PushTask( jsonOssTask );
  704. }
  705. }
  706. catch ( Exception e )
  707. {
  708. System.Diagnostics.Debug.Write( e.Message );
  709. }
  710. }
  711. // 标识加载过;
  712. IsTaskloaded = true;
  713. }
  714. /// <summary>
  715. /// 任务是否存在;
  716. /// </summary>
  717. /// <param name="jsonOssTask"></param>
  718. /// <returns></returns>
  719. public static bool IsTaskExists( JsonOssTask jsonOssTask )
  720. {
  721. if ( _listOssTask == null || _listOssTask.Count == 0 ) return false;
  722. bool exists = false;
  723. foreach ( JsonOssTask task in _listOssTask )
  724. {
  725. if ( task.TaskID.ToUpper() == jsonOssTask.TaskID.ToUpper() )
  726. {
  727. exists = true;
  728. break;
  729. }
  730. }
  731. return exists;
  732. }
  733. public static bool IsTaskExists( string strTaskID )
  734. {
  735. if ( _listOssTask == null || _listOssTask.Count == 0 ) return false;
  736. bool exists = false;
  737. foreach ( JsonOssTask task in _listOssTask )
  738. {
  739. if ( task.TaskID.ToUpper() == strTaskID.ToUpper() )
  740. {
  741. exists = true;
  742. break;
  743. }
  744. }
  745. return exists;
  746. }
  747. /// <summary>
  748. /// 将新任务压入任务队列;
  749. /// </summary>
  750. /// <param name="jsonOssTask"></param>
  751. /// <returns></returns>
  752. public static void PushTask( JsonOssTask jsonOssTask )
  753. {
  754. if ( jsonOssTask == null ) return;
  755. lock ( _lockJson )
  756. {
  757. if ( _listOssTask == null )
  758. _listOssTask = new List<JsonOssTask>();
  759. if ( !IsTaskExists( jsonOssTask ) )
  760. {
  761. jsonOssTask.IsWorking = false;
  762. _listOssTask.Add( jsonOssTask );
  763. }
  764. }
  765. }
  766. /// <summary>
  767. /// 取出任务,先进先出方式;
  768. /// </summary>
  769. /// <returns></returns>
  770. public static JsonOssTask PopTask()
  771. {
  772. if ( _listOssTask == null || _listOssTask.Count == 0 ) return null;
  773. lock ( _lockJson )
  774. {
  775. /*
  776. // 先进先出;
  777. JsonOssTask jsonOssTask = _listOssTask.Last();
  778. // 从任务队列中移除;
  779. _listOssTask.RemoveAt( _listOssTask.Count - 1 );
  780. */
  781. // 先进先出,找到可以开始的任务;
  782. JsonOssTask jsonOssTask = null;
  783. for ( int i = _listOssTask.Count - 1; i > -1; i-- )
  784. {
  785. if ( !_listOssTask[i].IsWorking // 正在运行的任务;
  786. && _listOssTask[i].MyTask != null // 被开始的任务;
  787. && string.IsNullOrEmpty( _listOssTask[i].FinishedTime ) // 未完成的任务;
  788. )
  789. {
  790. jsonOssTask = _listOssTask[i];
  791. break;
  792. }
  793. }
  794. return jsonOssTask;
  795. }
  796. }
  797. /// <summary>
  798. /// 删除一个任务(删除前,如果任务在进行,必须停止任务);
  799. /// </summary>
  800. /// <param name="taskMd5">订单号+相片类型+拍摄景点的MD5值, 也是文件名</param>
  801. public static void RemoveTask( string taskID )
  802. {
  803. if ( _listOssTask == null || _listOssTask.Count == 0 ) return;
  804. lock ( _lockJson )
  805. {
  806. JsonOssTask jsonOssTask = null;
  807. for ( int i = _listOssTask.Count - 1; i > -1; i-- )
  808. {
  809. jsonOssTask = _listOssTask[i];
  810. if ( taskID.ToUpper() == jsonOssTask.TaskID.ToUpper() )
  811. {
  812. _listOssTask.RemoveAt( i );
  813. break;
  814. }
  815. }
  816. }
  817. }
  818. public static void RemoveTask( JsonOssTask ossTask )
  819. {
  820. if ( _listOssTask == null || _listOssTask.Count == 0 ) return;
  821. lock ( _lockJson )
  822. {
  823. JsonOssTask jsonOssTask = null;
  824. for ( int i = _listOssTask.Count - 1; i > -1; i-- )
  825. {
  826. jsonOssTask = _listOssTask[i];
  827. if ( ossTask == jsonOssTask )
  828. {
  829. _listOssTask.RemoveAt( i );
  830. // 并且移除任务文件;
  831. DeleteTaskFile( jsonOssTask.TaskID );
  832. break;
  833. }
  834. }
  835. }
  836. }
  837. /// <summary>
  838. /// 删除任务文件;
  839. /// </summary>
  840. /// <param name="strTaskID"></param>
  841. public static void DeleteTaskFile( string strTaskID )
  842. {
  843. try
  844. {
  845. string taskfile = string.Format( "{0}{1}.Json", JsonOssTask.TaskJsonPath, strTaskID );
  846. if ( System.IO.File.Exists( taskfile ) )
  847. System.IO.File.Delete( taskfile );
  848. }
  849. catch
  850. {
  851. // 可能没有权限删除,或都文件被占用;
  852. }
  853. }
  854. /// <summary>
  855. /// 启动线程;
  856. /// </summary>
  857. public static void StartThread()
  858. {
  859. if ( _thread == null )
  860. {
  861. _thread = new System.Threading.Thread( TaskThread );
  862. _thread.Start();
  863. }
  864. }
  865. /// <summary>
  866. /// 任务线程;
  867. /// </summary>
  868. private static void TaskThread()
  869. {
  870. JsonOssTask ossTask = null;
  871. while ( true )
  872. {
  873. if ( _listOssTask == null || _listOssTask.Count == 0 )
  874. {
  875. Thread.Sleep( 150 );
  876. continue;
  877. }
  878. // 弹出一个未执行的任务;
  879. ossTask = PopTask();
  880. if ( ossTask != null && string.IsNullOrEmpty( ossTask.FinishedTime ) && !ossTask.IsWorking )
  881. {
  882. ossTask.StartTask();
  883. try
  884. {
  885. while ( !ossTask.MyTask.IsCompleted )
  886. {// 等待任务完成;
  887. Thread.Sleep( 150 );
  888. }
  889. }
  890. catch
  891. {
  892. // 异常时,表示任务被取消;
  893. }
  894. // 如果任务未完成,再次入队列中;
  895. if ( string.IsNullOrEmpty( ossTask.FinishedTime ) )
  896. {
  897. PushTask( ossTask );
  898. }
  899. }
  900. }
  901. }
  902. public delegate void SetRowTextDelegate( string md5, string pro, string time );
  903. /// <summary>
  904. /// 设置datagridview行文本;
  905. /// </summary>
  906. /// <param name="pro">进度文本</param>
  907. /// <param name="time">完成时间</param>
  908. public void SetRowText( string md5, string pro, string time )
  909. {
  910. if ( this.dgv.InvokeRequired )
  911. {
  912. SetRowTextDelegate setRowText = new SetRowTextDelegate( SetRowText );
  913. this.dgv.Invoke( setRowText, md5, pro, time );
  914. }
  915. else
  916. {
  917. JsonOssTask jsonOssTask = null;
  918. foreach ( DataGridViewRow item in this.dgv.Rows )
  919. {
  920. jsonOssTask = (JsonOssTask)item.Tag;
  921. if ( jsonOssTask != null && jsonOssTask.TaskID.ToUpper() == md5.ToUpper() )
  922. {
  923. item.Cells[7].Value = pro;
  924. item.Cells[8].Value = time;
  925. // 如果已经完成,生成连接;
  926. PaddingUrl( jsonOssTask, item );
  927. }
  928. }
  929. }
  930. }
  931. #endregion
  932. }
  933. [Serializable]
  934. /// <summary>
  935. /// Json格式的OSS任务;
  936. /// </summary>
  937. public class JsonOssTask
  938. {
  939. /// <summary>
  940. /// 任务是否运行;
  941. /// </summary>
  942. [JsonIgnore]
  943. public bool IsWorking { get; set; } = false;
  944. [JsonIgnore]
  945. public Task MyTask { get; set; } = null;
  946. [JsonIgnore]
  947. public CancellationTokenSource MyCancellationTokenSource { get; set; } = null;
  948. [JsonIgnore]
  949. // 上传下载oss的执行对象;
  950. private UploadOssCloudDisk _uploadOss = new UploadOssCloudDisk();
  951. [JsonIgnore]
  952. public static string TaskJsonPath { get; } = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath( LYFZ.WinAPI.CustomPublicMethod.BasePath ) + "Temp\\.OssTask\\";
  953. /// <summary>
  954. /// 任务ID值(由企业ID+分店ID+订单号+相片类型+拍摄名称计算);
  955. /// </summary>
  956. public string TaskID { get; set; } = "";
  957. /// <summary>
  958. /// 对象存储类型: 0 = 阿里云(默认), 1 = 腾讯云, 2 = 手动百度云分享;;
  959. /// </summary>
  960. public int OsType { get; set; } = 0;
  961. /// <summary>
  962. /// 百度云分享链接;
  963. /// </summary>
  964. public string BaiduShareUrl { get; set; } = "";
  965. /// <summary>
  966. /// 百度提取码;
  967. /// </summary>
  968. public string BaiduExtrCode { get; set; } = "";
  969. /// <summary>
  970. /// 可下载到期日期;
  971. /// </summary>
  972. public string DownloadExpireTime { get; set; } = "";
  973. /// <summary>
  974. /// 自定义下载次数;
  975. /// </summary>
  976. public int CustomizeDownloadTimes { get; set; } = 1;
  977. /// <summary>
  978. /// 企业ID;
  979. /// </summary>
  980. public string EnterpriseID { get; set; } = "";
  981. /// <summary>
  982. /// 上传分店ID,即本店ID;
  983. /// </summary>
  984. public string UploadBranchID { get; set; } = "";
  985. /// <summary>
  986. /// 下载分店串,前后都必须使用#分隔,如:"#SSN001#SSN002#SSN003#"
  987. /// </summary>
  988. public string DownloadBranchIDs { get; set; } = "";
  989. /// <summary>
  990. /// 订单号;
  991. /// </summary>
  992. public string OrderNumber { get; set; } = "";
  993. /// <summary>
  994. /// 子订单号;
  995. /// </summary>
  996. public string OrderViceNumber { get; set; } = "";
  997. /// <summary>
  998. /// 订单类型;
  999. /// </summary>
  1000. public string OrderType { get; set; } = "";
  1001. /// <summary>
  1002. /// 顾客姓名;
  1003. /// </summary>
  1004. public string CusNames { get; set; } = "";
  1005. /// <summary>
  1006. /// 顾客性别;
  1007. /// </summary>
  1008. public string CusGenders { get; set; } = "";
  1009. /// <summary>
  1010. /// 顾客性别;
  1011. /// </summary>
  1012. public string CusPhones { get; set; } = "";
  1013. /// <summary>
  1014. /// 拍摄名称;
  1015. /// </summary>
  1016. public string ShootName { get; set; } = "";
  1017. /// <summary>
  1018. /// 像片类型 <固定以下字符串:"原片,初修,精修,设计,已选">;
  1019. /// </summary>
  1020. public string PhotosType { get; set; } = "";
  1021. /// <summary>
  1022. /// 已上传的相片数量;
  1023. /// </summary>
  1024. public int PhotoCount { get; set; } = 0;
  1025. /// <summary>
  1026. /// 已上传的相片总大小;
  1027. /// </summary>
  1028. public ulong PhotoSize { get; set; } = 0;
  1029. /// <summary>
  1030. /// 上传的大小(不包含已存在的,即Status=1);
  1031. /// </summary>
  1032. public ulong UploadedSize { get; set; } = 0;
  1033. /// <summary>
  1034. /// 上传完成时间(可以作为完成状态使用, 只有全部相片上传完了,才能设置时间);
  1035. /// </summary>
  1036. public string FinishedTime { get; set; } = "";
  1037. [JsonIgnore]
  1038. /// <summary>
  1039. /// 客户要上传至云的相片集合;
  1040. /// </summary>
  1041. public List<PhotoItem> listPhotos { get; set; } = null;
  1042. public List<PhotoArray> ArrayPhotos { get; set; } = null;
  1043. /// <summary>
  1044. /// 开始任务;
  1045. /// </summary>
  1046. public void StartTask()
  1047. {
  1048. if ( !MarkTask() )
  1049. {
  1050. MessageBoxCustom.Show( "仍有相片在上传,重新开始失败!" );
  1051. return;
  1052. }
  1053. MyTask.Start();
  1054. IsWorking = true;
  1055. }
  1056. /// <summary>
  1057. /// 标记任务为可执行状态,等待执行;
  1058. /// </summary>
  1059. public bool MarkTask()
  1060. {
  1061. if ( MyTask == null || MyTask.Status == TaskStatus.RanToCompletion )
  1062. {
  1063. MyCancellationTokenSource = new CancellationTokenSource();
  1064. MyTask = new Task( () => TaskWorking( this, MyCancellationTokenSource.Token ), MyCancellationTokenSource.Token );
  1065. return true;
  1066. }
  1067. return false;
  1068. }
  1069. /// <summary>
  1070. /// 停止任务;
  1071. /// </summary>
  1072. public void StopTask()
  1073. {
  1074. if ( MyTask != null && MyCancellationTokenSource != null )
  1075. {
  1076. IsWorking = false;
  1077. MyCancellationTokenSource.Cancel();
  1078. }
  1079. }
  1080. /// <summary>
  1081. /// 任务函数;
  1082. /// </summary>
  1083. /// <param name="ossTask"></param>
  1084. /// <param name="ct"></param>
  1085. public static void TaskWorking( JsonOssTask ossTask, CancellationToken ct )
  1086. {
  1087. // 参数校验;
  1088. if ( ossTask.listPhotos == null || ossTask.listPhotos.Count == 0 )
  1089. {
  1090. ossTask.IsWorking = false;
  1091. return;
  1092. }
  1093. // 开始上传;
  1094. Int64 nCompleteSize = 0;
  1095. int nCompleteCount = 0;
  1096. PhotoItem photoItem = null;
  1097. for ( int i = ossTask.listPhotos.Count - 1; i > -1 && !ct.IsCancellationRequested; i-- )
  1098. {
  1099. photoItem = ossTask.listPhotos[i];
  1100. if ( string.IsNullOrEmpty( photoItem.PhotoUrl ) )
  1101. {
  1102. OSSUpdateReturnData returnData = ossTask._uploadOss.UpLoadFile( photoItem.PhotoName, OssCloudDiskFileType.PhotoCloudBackup, null );
  1103. if ( returnData.State )
  1104. {
  1105. photoItem.PhotoUrl = returnData.OssFileWebUrl;
  1106. // 将文件路径替换成key值;
  1107. //photoItem.PhotoName = returnData.OssFileKey;
  1108. // 将文件路径替换成文件名;
  1109. photoItem.PhotoName = System.IO.Path.GetFileNameWithoutExtension( photoItem.PhotoName ).Replace( BLL.SelectPhotoHandling.PhotoType.Original.ToString() + "_", "" );
  1110. ++nCompleteCount;
  1111. nCompleteSize += photoItem.PhotoSize;
  1112. }
  1113. }
  1114. else
  1115. {
  1116. ++nCompleteCount;
  1117. nCompleteSize += photoItem.PhotoSize;
  1118. }
  1119. // 更新界面的进度;
  1120. RefreshUI( ossTask, nCompleteCount );
  1121. }
  1122. // 如果全部上传完成,生成完成时间,并且保存到文件中;
  1123. if ( nCompleteCount == ossTask.listPhotos.Count && string.IsNullOrEmpty( ossTask.FinishedTime ) )
  1124. {
  1125. try
  1126. {
  1127. ossTask.FinishedTime = DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" );
  1128. string strJson = JsonConvert.SerializeObject( ossTask.listPhotos );
  1129. // 向服务器发送Json数据,Post发送;
  1130. string url = "http://xiangpianxiazai.lyfz.net:8090/Upload.aspx";
  1131. //string url = "http://localhost:60336/Upload.aspx";
  1132. string post_date = string.Format( "EntID={0}&OrderNumber={1}&ShootName={2}&PhotoType={3}&PhotoCount={4}&PhotoSize={5}&TaskMD5={6}&TaskJson={7}",
  1133. ossTask.EnterpriseID,
  1134. ossTask.OrderNumber,
  1135. ossTask.ShootName,
  1136. ossTask.PhotosType,
  1137. nCompleteCount,
  1138. nCompleteSize,
  1139. ossTask.TaskID,
  1140. strJson );
  1141. string strResult = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestPOST( url, post_date, Encoding.UTF8 );
  1142. try
  1143. {
  1144. ResultCode resultCode = JsonConvert.DeserializeObject<ResultCode>( strResult );
  1145. //if ( resultCode.Status == 200 )
  1146. if ( true )
  1147. {
  1148. // 写入文件;
  1149. if ( !System.IO.Directory.Exists( JsonOssTask.TaskJsonPath ) )
  1150. System.IO.Directory.CreateDirectory( JsonOssTask.TaskJsonPath );
  1151. strJson = JsonConvert.SerializeObject( ossTask );
  1152. System.IO.File.WriteAllText( JsonOssTask.TaskJsonPath + ossTask.TaskID + ".Json", strJson );
  1153. }
  1154. else
  1155. {
  1156. // 上传失败;
  1157. ossTask.FinishedTime = "";
  1158. }
  1159. }
  1160. catch
  1161. {
  1162. // 上传失败;
  1163. ossTask.FinishedTime = "";
  1164. }
  1165. }
  1166. catch { }
  1167. // 更新界面的进度;
  1168. RefreshUI( ossTask, nCompleteCount );
  1169. // 任务置为false;
  1170. ossTask.IsWorking = false;
  1171. }
  1172. }
  1173. /// <summary>
  1174. /// 刷新UI;
  1175. /// </summary>
  1176. /// <param name="ossTask"></param>
  1177. /// <param name="nCompleteCount"></param>
  1178. public static void RefreshUI( JsonOssTask ossTask, int nCompleteCount )
  1179. {
  1180. try
  1181. {
  1182. if ( frmOssTask.MyfrmOssTask != null )
  1183. {
  1184. frmOssTask.MyfrmOssTask.SetRowText( ossTask.TaskID, string.Format( "{0}/{1}", nCompleteCount, ossTask.listPhotos.Count ), ossTask.FinishedTime );
  1185. }
  1186. }
  1187. catch
  1188. {
  1189. // 可能界面被关闭后, SetRowText函数未执行完, 导致异常发生;
  1190. }
  1191. }
  1192. }
  1193. [Serializable]
  1194. public class PhotoArray
  1195. {
  1196. /// <summary>
  1197. /// 相片类型:选好的原片, 选好的初修, 原片, 初修, 精修, 设计;
  1198. /// </summary>
  1199. public string PhotosType { get; set; } = "";
  1200. /// <summary>
  1201. /// 相片集合;
  1202. /// </summary>
  1203. public List<PhotoItem> ListPhotos { get; set; } = null;
  1204. }
  1205. [Serializable]
  1206. /// <summary>
  1207. /// 嵌套类,作为子项;
  1208. /// </summary>
  1209. public class PhotoItem
  1210. {
  1211. public PhotoItem()
  1212. {
  1213. }
  1214. public PhotoItem( string path, long size, string url )
  1215. {
  1216. PhotoName = path;
  1217. PhotoSize = size;
  1218. PhotoUrl = url;
  1219. DownloadTimes = 0;
  1220. }
  1221. public PhotoItem( string name, string path, long size, string url )
  1222. {
  1223. PhotoName = name;
  1224. PhotoSize = size;
  1225. PhotoUrl = url;
  1226. DownloadTimes = 0;
  1227. PhotoPath = path;
  1228. }
  1229. /// <summary>
  1230. /// 上传状态: -1,无状态, 0, 不存在, 1,已存在, 2,已上传;
  1231. /// </summary>
  1232. //public int Status { get; set; } = -1;
  1233. /// <summary>
  1234. /// 相片路径;
  1235. /// </summary>
  1236. public string PhotoName { get; set; } = "";
  1237. /// <summary>
  1238. /// 相片的文件路径, 用于分店下载时,保存到指定的路径;
  1239. /// </summary>
  1240. public string PhotoPath { get; set; } = "";
  1241. /// <summary>
  1242. /// 相片大小;
  1243. /// </summary>
  1244. public long PhotoSize { get; set; } = 0;
  1245. /// <summary>
  1246. /// 相片上传完成后的Oss Key值;
  1247. /// </summary>
  1248. public string PhotoKey { get; set; } = ""; // url有限制有限期;
  1249. /// <summary>
  1250. /// 上传成功后,记录下来的oss链接地址(可以作为上传状态使用, 只有上传成的相片,才有url地址);
  1251. /// </summary>
  1252. public string PhotoUrl { get; set; } = "";
  1253. /// <summary>
  1254. /// 该相片被下载的次数;
  1255. /// </summary>
  1256. public int DownloadTimes { get; set; } = 0;
  1257. }
  1258. [Serializable]
  1259. public class BaiduInfo
  1260. {
  1261. public string BaiduShareUrl { get; set; } = "";
  1262. public string BaiduExtrCode { get; set; } = "";
  1263. }
  1264. public class ResultCode
  1265. {
  1266. /// <summary>
  1267. /// 状态,200为成功;
  1268. /// </summary>
  1269. public int code { get; set; } = 201;
  1270. /// <summary>
  1271. /// 描述;
  1272. /// </summary>
  1273. public string info { get; set; } = "";
  1274. public class ServeData
  1275. {
  1276. public int StatusCode { get; set; } = 0;
  1277. public bool Status { get; set; } = false;
  1278. public string StatusMsg { get; set; } = "";
  1279. }
  1280. public ServeData data { get; set; } = new ServeData();
  1281. }
  1282. }