common.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. ) {
  48. const ids = $('.checkbox_caviar:checkbox:checked').map((index,el) => {
  49. return $(el).val();
  50. }).get();
  51. if(ids.length === 0) {
  52. layer.msg(hints[0],{icon: 5})
  53. return false;
  54. }
  55. layer.open({
  56. title,
  57. content,
  58. btn,
  59. yes: async (index, layero) => {
  60. const response = await request(url, {ids: ids.join(',')});
  61. response.code === 200 ? layer.msg(hints[1], {icon: 1,time:500},() => {
  62. location.reload();
  63. }) : layer.msg(res.message,{icon: 5})
  64. layer.close(index);
  65. }
  66. });
  67. }
  68. function deletingASingle(url,
  69. id,
  70. hints = ['Succeed'],
  71. title= 'Warn',
  72. content= 'Are you sure you want to delete the selected data?',
  73. btn = ['Confirm']) {
  74. layer.open({
  75. title: title,
  76. content: content,
  77. btn: btn,
  78. yes: async (index, layero) => {
  79. const response = await request(url, { ids: id.toString()} );
  80. response.code === 200 ? layer.msg(hints[0], {icon: 1,time:500},() => {
  81. location.reload();
  82. }) : layer.msg(res.message,{icon: 5})
  83. layer.close(index);
  84. }
  85. });
  86. }
  87. function add(url = '', title = "Info", area = ['60%', '80%']) {
  88. layui.use('layer', function(){
  89. var layer = layui.layer;
  90. layer.open({
  91. title: title,
  92. type: 2,
  93. content:url,
  94. area: area,
  95. });
  96. });
  97. }
  98. function view(url = '', title = "Info", area = ['80%', '80%']) {
  99. layui.use('layer', function(){
  100. var layer = layui.layer;
  101. layer.open({
  102. title: title,
  103. type: 2,
  104. content:url,
  105. area: area,
  106. });
  107. });
  108. }
  109. function image_magnify() {
  110. let jishu=0;
  111. $(document).on('click', '.small-img', function(data){
  112. var ImgUrl=$(this).attr('src');
  113. if(jishu===0) {
  114. jishu=1;
  115. layer.open({
  116. type:1
  117. ,title:false
  118. ,closeBtn:0
  119. ,skin:'layui-layer-nobg'
  120. ,shadeClose:true
  121. ,content:'<img style="width:100%;height:100%;" class="layui-upload-img" src="'+ ImgUrl+'"/>'
  122. ,scrollbar:false
  123. })
  124. }else{
  125. jishu=0;
  126. layer.closeAll();
  127. }
  128. })
  129. }