database.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {extend name="common/base" /}
  2. <!-- 主体 -->
  3. {block name="body"}
  4. <div class="p-3">
  5. <table class="layui-hide" id="backup" lay-filter="backup"></table>
  6. </div>
  7. <script type="text/html" id="toolbarDemo">
  8. <div class="layui-btn-group">
  9. <span class="layui-btn layui-btn-sm layui-btn-normal" lay-event="backup">数据备份</span><span class="layui-btn layui-btn-sm" lay-event="optimize">数据表优化</span><span class="layui-btn layui-btn-danger layui-btn-sm" lay-event="repair">数据表修复</span>
  10. </div>
  11. <span id="dataTips" style="font-size:12px; margin-left:10px"></span>
  12. </script>
  13. {/block}
  14. <!-- /主体 -->
  15. <!-- 脚本 -->
  16. {block name="script"}
  17. <script>
  18. function gouguInit() {
  19. var table = layui.table, form = layui.form;
  20. var tableIns = table.render({
  21. elem: '#backup',
  22. title: '数据备份',
  23. toolbar: '#toolbarDemo',
  24. url: "/admin/database/database", //数据接口
  25. page: false,
  26. cols: [
  27. [ //表头
  28. { type: 'checkbox' },
  29. {
  30. field: 'name',
  31. title: '数据表',
  32. width: 200
  33. },
  34. {
  35. field: 'status',
  36. title: '状态',
  37. width: 100,
  38. templet: function (d) {
  39. return '<span id="table_' + d.name + '">-</span>';
  40. }
  41. },
  42. {
  43. field: 'engine',
  44. title: '存储引擎',
  45. align: 'center',
  46. width: 80
  47. }, {
  48. field: 'row_format',
  49. title: '行格式',
  50. align: 'center',
  51. width: 80
  52. }, {
  53. field: 'rows',
  54. title: '行数',
  55. align: 'center',
  56. width: 60,
  57. }, {
  58. field: 'data_size',
  59. title: '字节数',
  60. align: 'center',
  61. width: 80
  62. }, {
  63. field: 'data_length',
  64. title: '数据大小',
  65. align: 'center',
  66. width: 80
  67. }, {
  68. field: 'comment',
  69. title: '数据表注释'
  70. }, {
  71. field: 'create_time',
  72. title: '创建时间',
  73. width: 160,
  74. align: 'center'
  75. }
  76. ]
  77. ],
  78. done: function (res, curr, count) {
  79. $('#dataTips').html(res.msg);
  80. }
  81. });
  82. //递归备份表
  83. function backup(tab, status, table) {
  84. layer.closeAll();
  85. layer.msg("备份中...,请勿关闭本页面", { time: 200000 });
  86. $.get("/admin/database/backup", tab, function (data) {
  87. if (data.code == 0) {
  88. showmsg(data.data.tab.table, data.msg);
  89. if (data.data.tab.start == 'ok') {
  90. layer.msg('备份完成');
  91. window.onbeforeunload = function () { return null }
  92. return;
  93. }
  94. backup(data.data.tab, tab.id != data.data.tab.id);
  95. } else {
  96. layer.msg('立即备份');
  97. }
  98. }, "json");
  99. }
  100. //修改备份状态
  101. function showmsg(table, msg) {
  102. console.log(table);
  103. $("#table_" + table).addClass('span-color-2').html(msg);
  104. }
  105. //监听行工具事件
  106. table.on('toolbar(backup)', function (obj) {
  107. var checkData = table.checkStatus(obj.config.id).data;
  108. var len = checkData.length;
  109. var tables = [];
  110. if (len == 0) {
  111. layer.msg('请先选择表');
  112. return false;
  113. }
  114. for (var i = 0; i < len; i++) {
  115. tables.push(checkData[i].name);
  116. }
  117. if (obj.event === 'backup') {
  118. layer.confirm('确认要备份选中的' + len + '个数据表吗?', {
  119. icon: 3,
  120. title: '提示'
  121. }, function (index) {
  122. $.ajax({
  123. url: "/admin/database/backup",
  124. type: 'post',
  125. data: { 'tables': tables },
  126. success: function (res) {
  127. if (res.code == 0) {
  128. layer.msg("开始备份,请不要关闭本页面!");
  129. window.onbeforeunload = function () { return "正在备份数据库,请不要关闭!" }
  130. backup(res.data.tab);
  131. }
  132. if (res.code == 2) {
  133. layer.confirm('检测到有一个备份任务未完成,请先清除未完成的备份', {
  134. icon: 3,
  135. title: '提示'
  136. }, function (index) {
  137. $.ajax({
  138. url: "/admin/database/del",
  139. data: { 'lock': 1 },
  140. success: function (res) {
  141. layer.msg(res.msg);
  142. }
  143. })
  144. })
  145. }
  146. else {
  147. layer.msg(res.msg);
  148. }
  149. }
  150. })
  151. layer.close(index);
  152. });
  153. } else if (obj.event === 'optimize') {
  154. layer.confirm('确认要优化选中的' + len + '个数据表吗?', {
  155. icon: 3,
  156. title: '提示'
  157. }, function (index) {
  158. $.ajax({
  159. url: "/admin/database/optimize",
  160. type: 'post',
  161. data: { 'tables': tables },
  162. success: function (res) {
  163. layer.msg(res.msg);
  164. if (res.code == 0) {
  165. setTimeout(function () {
  166. location.reload();
  167. }, 2000)
  168. }
  169. }
  170. })
  171. layer.close(index);
  172. });
  173. } else if (obj.event === 'repair') {
  174. layer.confirm('确认要修复选中的' + len + '个数据表吗?', {
  175. icon: 3,
  176. title: '提示'
  177. }, function (index) {
  178. $.ajax({
  179. url: "/admin/database/repair",
  180. type: 'post',
  181. data: { 'tables': tables },
  182. success: function (res) {
  183. layer.msg(res.msg);
  184. if (res.code == 0) {
  185. setTimeout(function () {
  186. location.reload();
  187. }, 2000)
  188. }
  189. }
  190. })
  191. layer.close(index);
  192. });
  193. }
  194. });
  195. }
  196. </script>
  197. {/block}
  198. <!-- /脚本 -->