common.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const layObj = {
  2. uploadImg(ele, temp = 'uploads.html') {
  3. layer.open({
  4. type: 2
  5. , area: ['600px', '700px']
  6. , title: '上传图片'
  7. , shade: 0.6 //遮罩透明度
  8. , maxmin: false //允许全屏最小化
  9. , anim: 1 //0-6的动画形式,-1不开启
  10. , content: temp
  11. , success: function (layero, index) {
  12. let body = layer.getChildFrame('body', index);//建立父子联系
  13. let html = `<input type="hidden" id="child-val" value="${ele}">`;
  14. body.append(html)
  15. }
  16. });
  17. },
  18. dialog(temp) {
  19. layer.open({
  20. type: 2
  21. , area: ['600px', '500px']
  22. , title: '添加分类'
  23. , shade: 0.6 //遮罩透明度
  24. , maxmin: false //允许全屏最小化
  25. , anim: 1 //0-6的动画形式,-1不开启
  26. , content: temp
  27. , success(layero, index) {
  28. console.log(layero, index)
  29. // let body = layer.getChildFrame('body', index);//建立父子联系
  30. // let html = `<input type="hidden" id="child-val" value="${ele}">`;
  31. // body.append(html)
  32. }
  33. });
  34. },
  35. msg(text,fn=()=>{}) {
  36. layer.msg(text,function () {
  37. fn()
  38. // window.location.reload();
  39. });
  40. },
  41. //询问框
  42. box(msg, func) {
  43. layer.confirm(msg, {
  44. title: '提示',
  45. btn: ['确定', '取消'] //按钮
  46. }, function () {
  47. func()
  48. })
  49. },
  50. get(url, func, err=()=>{}) {
  51. $.ajax({
  52. url,
  53. type: 'GET',
  54. success(res) {
  55. return func(res)
  56. },
  57. error(res) {
  58. return err(res)
  59. }
  60. })
  61. },
  62. post(url, data, func, err=()=>{}) {
  63. $.ajax({
  64. url,
  65. data,
  66. type: 'POST',
  67. dataType: "json",
  68. success(res) {
  69. return func(res)
  70. },
  71. error(res) {
  72. return err(res)
  73. }
  74. })
  75. }
  76. };
  77. function draw(show_num, ele = 'canvas') {
  78. var canvas_width = $('#' + ele).width();
  79. var canvas_height = $('#' + ele).height();
  80. var canvas = document.getElementById(ele);//获取到canvas的对象,演员
  81. var context = canvas.getContext("2d");//获取到canvas画图的环境,演员表演的舞台
  82. canvas.width = canvas_width;
  83. canvas.height = canvas_height;
  84. var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
  85. var aCode = sCode.split(",");
  86. var aLength = aCode.length;//获取到数组的长度
  87. for (var i = 0; i <= 3; i++) {
  88. var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
  89. var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
  90. var txt = aCode[j];//得到随机的一个内容
  91. show_num[i] = txt.toLowerCase();
  92. var x = 10 + i * 20;//文字在canvas上的x坐标
  93. var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
  94. context.font = "bold 23px 微软雅黑";
  95. context.translate(x, y);
  96. context.rotate(deg);
  97. context.fillStyle = randomColor();
  98. context.fillText(txt, 0, 0);
  99. context.rotate(-deg);
  100. context.translate(-x, -y);
  101. }
  102. for (var i = 0; i <= 5; i++) { //验证码上显示线条
  103. context.strokeStyle = randomColor();
  104. context.beginPath();
  105. context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
  106. context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
  107. context.stroke();
  108. }
  109. for (var i = 0; i <= 30; i++) { //验证码上显示小点
  110. context.strokeStyle = randomColor();
  111. context.beginPath();
  112. var x = Math.random() * canvas_width;
  113. var y = Math.random() * canvas_height;
  114. context.moveTo(x, y);
  115. context.lineTo(x + 1, y + 1);
  116. context.stroke();
  117. }
  118. }
  119. function randomColor() {//得到随机的颜色值
  120. var r = Math.floor(Math.random() * 256);
  121. var g = Math.floor(Math.random() * 256);
  122. var b = Math.floor(Math.random() * 256);
  123. return "rgb(" + r + "," + g + "," + b + ")";
  124. }
  125. function toTree(data) { // 生成递归树
  126. // 删除 所有 children,以防止多次调用
  127. data.forEach(function (item) {
  128. delete item.children;
  129. });
  130. // 将数据存储为 以 id 为 KEY 的 map 索引数据列
  131. var map = {};
  132. data.forEach(function (item) {
  133. map[item.id] = item;
  134. });
  135. var val = [];
  136. data.forEach(function (item) {
  137. // 以当前遍历项,的pid,去map对象中找到索引的id
  138. var parent = map[item.pid];
  139. // 好绕啊,如果找到索引,那么说明此项不在顶级当中,那么需要把此项添加到,他对应的父级中
  140. if (parent) {
  141. (parent.children || (parent.children = [])).push(item);
  142. } else {
  143. //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中,作为顶级
  144. val.push(item);
  145. }
  146. });
  147. return val;
  148. }