Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

142 linhas
4.3 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Util;
  3. using Nancy;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace Learun.Application.WebApi.Modules
  8. {
  9. public class AnnexesApiWx : BaseApi
  10. {
  11. public AnnexesApiWx()
  12. : base("/learun/adms/annexes")
  13. {
  14. Get["/wxlist"] = WxGetList;
  15. Get["/wxdown"] = WxDownload;
  16. Get["/wxfileinfo"] = WxFileInfo;
  17. Post["/wxupload"] = WxUpload;
  18. Post["/wxdelete"] = WxDeleteFile;
  19. }
  20. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  21. /// <summary>
  22. /// 获取附件列表
  23. /// </summary>
  24. /// <param name="_"></param>
  25. /// <returns></returns>
  26. public Response WxGetList(dynamic _)
  27. {
  28. var keyValue = this.GetReqData();
  29. var list = annexesFileIBLL.GetList(keyValue);
  30. return Success(list);
  31. }
  32. /// <summary>
  33. /// 上传附件图片文件
  34. /// <summary>
  35. /// <returns></returns>
  36. public Response WxUpload(dynamic _)
  37. {
  38. var files = (List<HttpFile>)this.Context.Request.Files;
  39. var folderId = this.GetReqData();
  40. string filePath = Config.GetValue("AnnexesFile");
  41. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  42. string fileEextension = Path.GetExtension(files[0].Name);
  43. string fileType = fileEextension.Replace(".", "");
  44. string fileGuid = Guid.NewGuid().ToString();
  45. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, fileEextension);
  46. //创建文件夹
  47. string path = Path.GetDirectoryName(virtualPath);
  48. Directory.CreateDirectory(path);
  49. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  50. if (!System.IO.File.Exists(virtualPath))
  51. {
  52. byte[] bytes = new byte[files[0].Value.Length];
  53. files[0].Value.Read(bytes, 0, bytes.Length);
  54. FileInfo file = new FileInfo(virtualPath);
  55. FileStream fs = file.Create();
  56. fs.Write(bytes, 0, bytes.Length);
  57. fs.Close();
  58. //文件信息写入数据库
  59. fileAnnexesEntity.F_Id = fileGuid;
  60. fileAnnexesEntity.F_FileName = files[0].Name;
  61. fileAnnexesEntity.F_FilePath = virtualPath;
  62. fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString();
  63. fileAnnexesEntity.F_FileExtensions = fileEextension;
  64. fileAnnexesEntity.F_FileType = fileType;
  65. fileAnnexesEntity.F_CreateUserId = userInfo.userId;
  66. fileAnnexesEntity.F_CreateUserName = userInfo.realName;
  67. annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity);
  68. }
  69. return SuccessString(fileGuid);
  70. }
  71. /// <summary>
  72. /// 获取文件信息
  73. /// </summary>
  74. /// <param name="_"></param>
  75. /// <returns></returns>
  76. public Response WxFileInfo(dynamic _)
  77. {
  78. var fileId = this.GetReqData();
  79. var fileEntity = annexesFileIBLL.GetEntity(fileId);
  80. return Success(fileEntity);
  81. }
  82. /// <summary>
  83. /// 删除文件
  84. /// </summary>
  85. /// <param name="_"></param>
  86. /// <returns></returns>
  87. public Response WxDeleteFile(dynamic _)
  88. {
  89. var fileId = this.GetReqData();
  90. AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
  91. annexesFileIBLL.DeleteEntity(fileId);
  92. //删除文件
  93. if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
  94. {
  95. System.IO.File.Delete(fileInfoEntity.F_FilePath);
  96. }
  97. return Success("删除成功");
  98. }
  99. /// <summary>
  100. /// 下载文件,微信小程序用
  101. ///
  102. /// 微信小程序可以预览图片、文档
  103. /// 支持的图片格式:.jpg .png .webp .gif
  104. /// 支持的文档格式:.doc(x) .xls(x) .ppt(x) .pdf
  105. ///
  106. /// 对于其他格式的文件,微信小程序官方未提供打开或预览的 API,文件对用户来说不可访问
  107. /// </summary>
  108. /// <param name="_"></param>
  109. /// <returns></returns>
  110. public Response WxDownload(dynamic _)
  111. {
  112. string name = this.GetReqData();
  113. string fileId = name.Split('.')[0];
  114. var fileEntity = annexesFileIBLL.GetEntity(fileId);
  115. string filepath = fileEntity.F_FilePath;
  116. if (!FileDownHelper.FileExists(filepath))
  117. {
  118. return 404;
  119. }
  120. FileDownHelper.DownLoadWx(filepath, fileEntity.F_FileType);
  121. return Success("");
  122. }
  123. }
  124. }