msg_add.ejs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <div class="panel-content">
  2. <div class="panel-title fluid">
  3. <div class="title-main"><h2>消息通知</h2></div>
  4. </div>
  5. <div class="content-wrap">
  6. <div class="sjs-height-0">
  7. <div class="container py-5">
  8. <form id="save-form" class="row" action="/dashboard/msg/set/<%= msgInfo.id === undefined ? 0 : msgInfo.id %>" method="post">
  9. <div class="card col-9">
  10. <div class="card-body">
  11. <div class="msg-height-content">
  12. <input-text name="title" id="title" placeholder="请输入标题" required="true" label="标题"
  13. value="<%= msgInfo.title %>"></input-text>
  14. <div class="form-group form-check">
  15. <input type="checkbox" class="form-check-input" id="exampleCheck1" name="istop" value="1" <% if (msgInfo.istop !== undefined && msgInfo.istop !== '0') { %>checked<% } %>>
  16. <label class="form-check-label" for="exampleCheck1">重要通知(标红置顶)</label>
  17. </div>
  18. <div class="form-group">
  19. <label>内容</label>
  20. <div>
  21. <textarea id="content" rows="15" name="content" class="form-control form-control-sm"><%- msgInfo.content %></textarea>
  22. </div>
  23. </div>
  24. <div class="form-group" id="msg-file">
  25. <label>附件</label>
  26. <input type="hidden" value="<%= msgInfo.id === undefined ? 0 : msgInfo.id %>" id="mid">
  27. <div class="form-group upload-permission">
  28. <label for="formGroupExampleInput">单个文件大小限制:30MB,支持<span data-toggle="tooltip" data-placement="bottom" title="doc,docx,xls,xlsx,ppt,pptx,pdf">office等文档格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="jpg,png,bmp">图片格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="rar,zip">压缩包格式</span></label>
  29. <br>
  30. <input type="file" class="" multiple>
  31. </div>
  32. <div class="sp-wrap" style="overflow: auto">
  33. <table class="table table-sm table-bordered" id="file-table">
  34. <thead>
  35. <tr class="text-center">
  36. <th width="5%">序号</th><th>名称</th><th width="15%">上传人</th><th width="15%">上传时间</th><th width="15%">操作</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. </tbody>
  41. </table>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. <div class="col-3 bd-toc">
  48. <div class="card">
  49. <div class="card-body">
  50. <input type="hidden" name="_csrf_j" value="<%= ctx.csrf %>">
  51. <% if (msgInfo.id === undefined) { %>
  52. <button type="submit" class="btn btn-primary">确认添加</button>
  53. <% } else { %>
  54. <button type="submit" class="btn btn-primary">确认修改</button>
  55. <a href="#del-modal" data-toggle="modal" data-target="#del-modal" class="btn btn-danger">删除</a>
  56. <% } %>
  57. </div>
  58. </div>
  59. </div>
  60. </form>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. <%- jsValidator %>
  66. <script src="/public/js/moment/moment.min.js"></script>
  67. <script type="text/javascript">
  68. new Vue({
  69. el: '#save-form',
  70. });
  71. const user_id = <%- ctx.session.sessionUser.accountId %>;
  72. const files = JSON.parse(unescape('<%- escape(JSON.stringify(files)) %>'));
  73. const whiteList = JSON.parse('<%- JSON.stringify(whiteList) %>');
  74. $(function () {
  75. $('#btn_click').click(function () {
  76. $('#btn_type').val(2);
  77. });
  78. setFiles(files);
  79. function setFiles(files, _this = '#file-table tbody') {
  80. let filesHtml = '';
  81. const newFiles = files.map(file => {
  82. let showDel = false;
  83. if (file.uid === user_id) {
  84. showDel = true
  85. }
  86. return {...file, showDel}
  87. })
  88. newFiles.forEach((file, idx) => {
  89. filesHtml += `<tr class="text-center">
  90. <td>${idx + 1}</td><td class="text-left"><a href="${file.filepath}" target="_blank">${file.filename}</a></td><td>${file.username}</td><td>${moment(file.upload_time).format('YYYY-MM-DD HH:mm:ss')}</td>
  91. <td>
  92. <div class="btn-group-table">
  93. ${file.viewpath ? `<a href="${file.viewpath}" target="_blank" class="mr-1"><i class="fa fa-eye fa-fw"></i></a>` : ''}
  94. <a href="/wap/message/download/file/${file.id}" class="mr-1"><i class="fa fa-download fa-fw"></i></a>
  95. ${file.showDel ? `<a href="javascript: void(0);" class="text-danger file-del mr-1" data-id="${file.id}"><i class="fa fa-trash-o fa-fw text-danger"></i></a>` : ''}
  96. </div>
  97. </td>
  98. </tr>`;
  99. });
  100. $(_this).html(filesHtml);
  101. }
  102. $('#msg-file input[type="file"]').change(function () {
  103. const files = Array.from(this.files);
  104. console.log(files);
  105. const valiData = files.map(v => {
  106. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  107. return {
  108. size: v.size,
  109. ext
  110. }
  111. })
  112. const mid = parseInt($('#mid').val());
  113. if (validateFiles(valiData)) {
  114. if (files.length) {
  115. const formData = new FormData()
  116. files.forEach(file => {
  117. formData.append('name', file.name)
  118. formData.append('size', file.size)
  119. formData.append('file', file)
  120. })
  121. postDataWithFile(`/dashboard/msg/${mid}/file/upload`, formData, function (result) {
  122. setFiles(result);
  123. });
  124. }
  125. }
  126. $('#msg-file input[type="file"]').val('');
  127. });
  128. $('body').on('click', '#msg-file .file-del', function () {
  129. const fid = $(this).data('id');
  130. const mid = parseInt($('#mid').val());
  131. deleteAfterHint(function () {
  132. postData(`/dashboard/msg/${mid}/file/del`, { id: fid }, function (result) {
  133. setFiles(result);
  134. });
  135. }, '确认删除该文件?');
  136. });
  137. })
  138. /**
  139. * 校验文件大小、格式
  140. * @param {Array} files 文件数组
  141. */
  142. function validateFiles(files) {
  143. if (files.length > 10) {
  144. toastr.error('至多同时上传10个文件');
  145. return false
  146. }
  147. return files.every(file => {
  148. if (file.size > 1024 * 1024 * 30) {
  149. toastr.error('文件大小限制为30MB');
  150. return false
  151. }
  152. if (whiteList.indexOf('.' + file.ext.toLowerCase()) === -1) {
  153. toastr.error('请上传正确的格式文件');
  154. return false
  155. }
  156. return true
  157. })
  158. }
  159. </script>