You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

228 lines
8.9 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["/wxbase64"] = WxUploadbase64;
  19. Post["/wxdelete"] = WxDeleteFile;
  20. Post["/wxuploadinsingle"] = WxUploadInSingle;
  21. }
  22. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  23. /// <summary>
  24. /// 获取附件列表
  25. /// </summary>
  26. /// <param name="_"></param>
  27. /// <returns></returns>
  28. public Response WxGetList(dynamic _)
  29. {
  30. var keyValue = this.GetReqData();
  31. var list = annexesFileIBLL.GetList(keyValue);
  32. return Success(list);
  33. }
  34. public Response WxUploadbase64(dynamic _)
  35. {
  36. var files = (List<HttpFile>)this.Context.Request.Files;
  37. //string folderId = Request.Form["folderId"];
  38. //string filePath = Config.GetValue("AnnexesFile");
  39. //string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  40. //string fileEextension = Path.GetExtension(files[0].Name);
  41. //string fileType = fileEextension.Replace(".", "");
  42. //string fileGuid = Guid.NewGuid().ToString();
  43. byte[] bytes = new byte[files[0].Value.Length];
  44. files[0].Value.Read(bytes, 0, bytes.Length);
  45. string src = Convert.ToBase64String(bytes);
  46. //string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, fileEextension);
  47. ////创建文件夹
  48. //string path = Path.GetDirectoryName(virtualPath);
  49. //Directory.CreateDirectory(path);
  50. //AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  51. //if (!System.IO.File.Exists(virtualPath))
  52. //{
  53. // byte[] bytes = new byte[files[0].Value.Length];
  54. // files[0].Value.Read(bytes, 0, bytes.Length);
  55. // //FileInfo file = new FileInfo(virtualPath);
  56. // //FileStream fs = file.Create();
  57. // //fs.Write(bytes, 0, bytes.Length);
  58. // //fs.Close();
  59. // string src = "data:image/png;base64," + Convert.ToBase64String(bytes);
  60. // //virtualPath
  61. //}
  62. return SuccessString(src);
  63. }
  64. /// <summary>
  65. /// 上传附件图片文件
  66. /// <summary>
  67. /// <returns></returns>
  68. public Response WxUpload(dynamic _)
  69. {
  70. var files = (List<HttpFile>)this.Context.Request.Files;
  71. string folderId = Request.Form["folderId"];
  72. string filePath = Config.GetValue("AnnexesFile");
  73. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  74. string fileEextension = Path.GetExtension(files[0].Name);
  75. string fileType = fileEextension.Replace(".", "");
  76. string fileGuid = Guid.NewGuid().ToString();
  77. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, fileEextension);
  78. //创建文件夹
  79. string path = Path.GetDirectoryName(virtualPath);
  80. Directory.CreateDirectory(path);
  81. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  82. if (!System.IO.File.Exists(virtualPath))
  83. {
  84. byte[] bytes = new byte[files[0].Value.Length];
  85. files[0].Value.Read(bytes, 0, bytes.Length);
  86. FileInfo file = new FileInfo(virtualPath);
  87. FileStream fs = file.Create();
  88. fs.Write(bytes, 0, bytes.Length);
  89. fs.Close();
  90. //文件信息写入数据库
  91. fileAnnexesEntity.F_Id = fileGuid;
  92. fileAnnexesEntity.F_FileName = files[0].Name;
  93. fileAnnexesEntity.F_FilePath = virtualPath;
  94. fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString();
  95. fileAnnexesEntity.F_FileExtensions = fileEextension;
  96. fileAnnexesEntity.F_FileType = fileType;
  97. fileAnnexesEntity.F_CreateUserId = userInfo.userId;
  98. fileAnnexesEntity.F_CreateUserName = userInfo.realName;
  99. annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity);
  100. }
  101. return SuccessString(fileGuid);
  102. }
  103. /// <summary>
  104. /// 获取文件信息
  105. /// </summary>
  106. /// <param name="_"></param>
  107. /// <returns></returns>
  108. public Response WxFileInfo(dynamic _)
  109. {
  110. var fileId = this.GetReqData();
  111. var fileEntity = annexesFileIBLL.GetEntity(fileId);
  112. return Success(fileEntity);
  113. }
  114. /// <summary>
  115. /// 删除文件
  116. /// </summary>
  117. /// <param name="_"></param>
  118. /// <returns></returns>
  119. public Response WxDeleteFile(dynamic _)
  120. {
  121. var fileId = this.GetReqData();
  122. AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
  123. annexesFileIBLL.DeleteEntity(fileId);
  124. //删除文件
  125. if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
  126. {
  127. System.IO.File.Delete(fileInfoEntity.F_FilePath);
  128. }
  129. return Success("删除成功");
  130. }
  131. /// <summary>
  132. /// 下载文件,微信小程序用
  133. ///
  134. /// 微信小程序可以预览图片、文档
  135. /// 支持的图片格式:.jpg .png .webp .gif
  136. /// 支持的文档格式:.doc(x) .xls(x) .ppt(x) .pdf
  137. ///
  138. /// 对于其他格式的文件,微信小程序官方未提供打开或预览的 API,文件对用户来说不可访问
  139. /// </summary>
  140. /// <param name="_"></param>
  141. /// <returns></returns>
  142. public Response WxDownload(dynamic _)
  143. {
  144. string name = this.GetReqData();
  145. string fileId = name.Split('.')[0];
  146. var fileEntity = annexesFileIBLL.GetEntity(fileId);
  147. string filepath = fileEntity.F_FilePath;
  148. if (!FileDownHelper.FileExists(filepath))
  149. {
  150. return 404;
  151. }
  152. FileDownHelper.DownLoadWx(filepath, fileEntity.F_FileType);
  153. return Success("");
  154. }
  155. /// <summary>
  156. /// 上传附件图片文件【移动端app2.0,单独页面中的附件图片上传使用方法】
  157. /// <summary>
  158. /// <returns></returns>
  159. public Response WxUploadInSingle(dynamic _)
  160. {
  161. var files = (List<HttpFile>)this.Context.Request.Files;
  162. var folderId = this.GetReq<FileModel>().folderId;
  163. var name = this.GetReq<FileModel>().name;
  164. string filePath = Config.GetValue("AnnexesFile");
  165. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  166. string FileEextension = Path.GetExtension(name);
  167. string fileGuid = Guid.NewGuid().ToString();
  168. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, FileEextension);
  169. //创建文件夹
  170. string path = Path.GetDirectoryName(virtualPath);
  171. Directory.CreateDirectory(path);
  172. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  173. if (!System.IO.File.Exists(virtualPath))
  174. {
  175. byte[] bytes = new byte[files[0].Value.Length];
  176. files[0].Value.Read(bytes, 0, bytes.Length);
  177. FileInfo file = new FileInfo(virtualPath);
  178. FileStream fs = file.Create();
  179. fs.Write(bytes, 0, bytes.Length);
  180. fs.Close();
  181. //文件信息写入数据库
  182. fileAnnexesEntity.F_Id = fileGuid;
  183. fileAnnexesEntity.F_FolderId = folderId;
  184. fileAnnexesEntity.F_FileName = name;
  185. fileAnnexesEntity.F_FilePath = virtualPath;
  186. fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString();
  187. fileAnnexesEntity.F_FileExtensions = FileEextension;
  188. fileAnnexesEntity.F_FileType = FileEextension.Replace(".", "");
  189. fileAnnexesEntity.F_CreateUserId = userInfo.userId;
  190. fileAnnexesEntity.F_CreateUserName = userInfo.realName;
  191. annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity);
  192. }
  193. return SuccessString(folderId);
  194. }
  195. public class FileModel
  196. {
  197. public string folderId { get; set; }
  198. public string name { get; set; }
  199. public string id { get; set; }
  200. }
  201. }
  202. }