common.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. async function request($url, $data, $method = 'POST') {
  2. return await new Promise((resolve, reject) => {
  3. $.ajax({
  4. type: $method,
  5. url: $url,
  6. data: $data,
  7. success: (res) => {
  8. // if(res.code === 1012) {
  9. // window.location = 'http://localhost:8001/admin/login'
  10. // }
  11. resolve(res)
  12. },
  13. error: (err) => {
  14. reject(err.message)
  15. }
  16. })
  17. })
  18. }
  19. // 搜索开关
  20. function select_card_switch() {
  21. const element = $('#select_card_caviar')
  22. if (element.css('display') === 'none') {
  23. element.css('display', 'block');
  24. $("#caviar_search_btn").val("关闭搜索")
  25. } else {
  26. element.css('display', 'none');
  27. $("#caviar_search_btn").val("展开搜索")
  28. }
  29. }
  30. function check_params(p = []) {
  31. const index = p.findIndex(v => v !== undefined && v.toString().length > 0);
  32. return index >= 0;
  33. }
  34. function checkbox() {
  35. const isSelect = $("#check-all").is(":checked")
  36. isSelect ? $("input[class='checkbox_caviar']").each(function (i, n) {
  37. n.checked = true;
  38. }) : $("input[class='checkbox_caviar']").each(function (i, n) {
  39. n.checked = false;
  40. })
  41. }
  42. async function caviar_checkbox_delete(url,
  43. hints = ['Check at least one box', 'Succeed'],
  44. title = 'Warn',
  45. content = 'Are you sure you want to delete the selected data?',
  46. btn = ['Confirm'],
  47. ids_data = undefined
  48. ) {
  49. const ids = $('.checkbox_caviar:checkbox:checked').map((index, el) => {
  50. return $(el).val();
  51. }).get();
  52. if (ids.length === 0 && ids_data === undefined) {
  53. layer.msg(hints[0], {icon: 5})
  54. return false;
  55. }
  56. layer.open({
  57. title,
  58. content,
  59. btn,
  60. yes: async (index, layero) => {
  61. const response = await request(url, {ids: ids_data ? ids_data.join(',') : ids.join(',')});
  62. response.code === 200 ? layer.msg(hints[1], {icon: 1, time: 500}, () => {
  63. location.reload();
  64. }) : layer.msg(res.message, {icon: 5})
  65. layer.close(index);
  66. }
  67. });
  68. }
  69. function deletingASingle(url,
  70. id,
  71. hints = ['Succeed'],
  72. title = 'Warn',
  73. content = 'Are you sure you want to delete the selected data?',
  74. btn = ['Confirm']) {
  75. layer.open({
  76. title: title,
  77. content: content,
  78. btn: btn,
  79. yes: async (index, layero) => {
  80. const response = await request(url, {ids: id.toString()});
  81. response.code === 200 ? layer.msg(hints[0], {icon: 1, time: 500}, () => {
  82. location.reload();
  83. }) : layer.msg(res.message, {icon: 5})
  84. layer.close(index);
  85. }
  86. });
  87. }
  88. function add(url = '', title = "Info", area = ['60%', '80%']) {
  89. layui.use('layer', function () {
  90. var layer = layui.layer;
  91. layer.open({
  92. title: title,
  93. type: 2,
  94. content: url,
  95. area: area,
  96. });
  97. });
  98. }
  99. function view(url = '', title = "Info", area = ['80%', '80%']) {
  100. layui.use('layer', function () {
  101. var layer = layui.layer;
  102. layer.open({
  103. title: title,
  104. type: 2,
  105. content: url,
  106. area: area,
  107. });
  108. });
  109. }
  110. function image_magnify() {
  111. let jishu = 0;
  112. $(document).on('click', '.small-img', function (data) {
  113. var ImgUrl = $(this).attr('src');
  114. if (jishu === 0) {
  115. jishu = 1;
  116. layer.open({
  117. type: 1
  118. , title: false
  119. , closeBtn: 0
  120. , skin: 'layui-layer-nobg'
  121. , shadeClose: true
  122. , content: '<img style="width:100%;height:100%;" class="layui-upload-img" src="' + ImgUrl + '"/>'
  123. , scrollbar: false
  124. })
  125. } else {
  126. jishu = 0;
  127. layer.closeAll();
  128. }
  129. })
  130. }
  131. function deepTraversal(tree = []) {
  132. let nodes = [];
  133. const queues = tree.map(v => v);
  134. while (queues.length > 0) {
  135. const queue = queues.pop();
  136. if (queue.children !== undefined && queue.children.length > 0) {
  137. queue.children.forEach(child => {
  138. queues.push(child)
  139. })
  140. }
  141. nodes.push({
  142. id: queue.id,
  143. title: queue.title,
  144. field: queue.field,
  145. checked: true,
  146. })
  147. }
  148. return nodes;
  149. }
  150. function urlencode (str) {
  151. str = (str + '').toString();
  152. return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
  153. replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
  154. }