Vue组件递归

  1. <a name="Zmtxw"></a>
  2. # Echarts封装Vue组件
  3. - 文章:[https://juejin.cn/post/6844903581259137031](https://juejin.cn/post/6844903581259137031)
  4. ```vue
  5. <template>
  6. <div :id="id" :style="style"></div>
  7. </template>
  8. <script>
  9. import echarts from "echarts";
  10. import "@/util/echartsColor.js";
  11. export default {
  12. name: "Chart",
  13. data() {
  14. return {
  15. // echarts实例
  16. chart: "",
  17. };
  18. },
  19. props: {
  20. id: {
  21. type: String,
  22. required: true,
  23. },
  24. width: {
  25. type: String,
  26. default: "100%",
  27. },
  28. height: {
  29. type: String,
  30. default: "100%",
  31. },
  32. option: {
  33. type: Object,
  34. default: () => {
  35. return {
  36. title: {
  37. text: "ECharts 入门示例",
  38. },
  39. tooltip: {},
  40. legend: {
  41. data: ["销量"],
  42. },
  43. xAxis: {
  44. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
  45. },
  46. yAxis: {},
  47. series: [
  48. {
  49. name: "销量",
  50. type: "bar",
  51. data: [5, 20, 36, 10, 10, 20],
  52. },
  53. ],
  54. };
  55. },
  56. },
  57. },
  58. watch: {
  59. // 观察option的变化
  60. option: {
  61. handler(newVal, oldVal) {
  62. if (this.chart) {
  63. if (newVal) {
  64. this.chart.setOption(newVal);
  65. } else {
  66. this.chart.setOption(oldVal);
  67. }
  68. } else {
  69. this.init();
  70. }
  71. },
  72. deep: true,
  73. },
  74. },
  75. computed: {
  76. style() {
  77. return {
  78. width: this.width,
  79. height: this.height,
  80. };
  81. },
  82. },
  83. mounted() {
  84. this.init();
  85. },
  86. methods: {
  87. // 让图表跟随屏幕自适应
  88. selfAdaption(chart) {
  89. window.addEventListener("resize", function () {
  90. chart.resize();
  91. });
  92. },
  93. init() {
  94. // 基于准备好的dom,初始化echarts实例
  95. this.chart = echarts.init(document.getElementById(this.id), "shine");
  96. // 指定图表的配置项和数据
  97. // 使用刚指定的配置项和数据显示图表。
  98. this.chart.setOption(this.option);
  99. // 让图表跟随屏幕自适应
  100. this.selfAdaption(this.myChart);
  101. },
  102. },
  103. };
  104. </script>
  105. <style scoped lang="scss"></style>

禁止查看网页源码

  1. // 禁止查看源码
  2. (function () {
  3. // 设置 body 中的 html
  4. function setBodyHtml(html) {
  5. document.getElementsByTagName("body")[0].innerHTML = html;
  6. }
  7. window.onload = function () {
  8. //屏蔽键盘事件
  9. document.onkeydown = function () {
  10. var e = window.event || arguments[0];
  11. //F12
  12. if (e.keyCode == 123) {
  13. setBodyHtml("<h1>禁止查看源码?</h1>");
  14. return false;
  15. //Ctrl+Shift+I
  16. } else if (e.ctrlKey && e.shiftKey && e.keyCode == 73) {
  17. setBodyHtml("<h1>禁止查看源码?</h1>");
  18. return false;
  19. //Shift+F10
  20. } else if (e.shiftKey && e.keyCode == 121) {
  21. setBodyHtml("<h1>禁止查看源码?</h1>");
  22. return false;
  23. //Ctrl+U
  24. } else if (e.ctrlKey && e.keyCode == 85) {
  25. setBodyHtml("<h1>禁止查看源码?</h1>");
  26. return false;
  27. }
  28. };
  29. //屏蔽鼠标右键
  30. document.oncontextmenu = function () {
  31. return false;
  32. };
  33. };
  34. });

前端分页

  1. // 分页
  2. paging(page = 1, limit = 10) {
  3. page = Number(page);
  4. limit = Number(limit);
  5. // 默认从第1页开始,显示10条
  6. // 每页默认显示10条
  7. this.tableData = this.tableDataCopy.slice(
  8. page * limit - limit + 0,
  9. page * limit - limit + limit
  10. );
  11. },

前端实现html转成pdf并下载

  1. var contentWidth = canvas.width;
  2. var contentHeight = canvas.height;
  3. //一页pdf显示html页面生成的canvas高度;
  4. var pageHeight = contentWidth / 592.28 * 841.89;
  5. //未生成pdf的html页面高度
  6. var leftHeight = contentHeight;
  7. //页面偏移
  8. var position = 0;
  9. //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
  10. var imgWidth = 595.28;
  11. var imgHeight = 592.28/contentWidth * contentHeight;
  12. var pageData = canvas.toDataURL('image/jpeg', 1.0);
  13. var pdf = new jsPDF('', 'pt', 'a4');
  14. //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
  15. //当内容未超过pdf一页显示的范围,无需分页
  16. if (leftHeight < pageHeight) {
  17. pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight );
  18. } else {
  19. while(leftHeight > 0) {
  20. pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
  21. leftHeight -= pageHeight;
  22. position -= 841.89;
  23. //避免添加空白页
  24. if(leftHeight > 0) {
  25. pdf.addPage();
  26. }
  27. }
  28. }
  29. pdf.save('content.pdf');

网址:https://blog.csdn.net/weixin_43720095/article/details/87358705

设置图片验证码

  1. // 显示验证码图片
  2. createMiniQrcode(blob) {
  3. let img = document.createElement("img");
  4. img.onload = function () {
  5. // 元素的onload 事件触发后将销毁URL对象, 释放内存。
  6. window.URL.revokeObjectURL(img.src);
  7. };
  8. // 浏览器允许使用URL.createObjectURL()方法,针对 Blob 对象生成一个临时 URL。
  9. // 这个 URL 以blob://开头,表明对应一个 Blob 对象。
  10. // img.src = window.URL.createObjectURL(blob);
  11. img.src = blob;
  12. document.querySelector("#imgQrCode").innerHTML = "";
  13. document.querySelector("#imgQrCode").appendChild(img);
  14. document.querySelector("#imgQrCode img").style.width = "100%";
  15. document.querySelector("#imgQrCode img").style.height = "100%";
  16. }
  17. /* 开始对按钮进行倒计时 */
  18. startCountdownTimer() {
  19. if(typeof this.countdownTime != 'number'){
  20. this.countdownTime = 60;
  21. this.countdownTimer = setInterval(() => {
  22. if (this.countdownTime <= 1) {
  23. clearInterval(this.countdownTimer);
  24. this.countdownTimer = null;
  25. }
  26. this.countdownTime--;
  27. }, 1000);
  28. }
  29. }

Vue登录、退出、路由守卫、headers设置token

  1. // 存储token
  2. localStorage.setItem('access_token', res.data.data.access_token)
  3. // 清空token
  4. localStorage.setItem("access_token", "");
  5. // 导航守卫
  6. router.beforeEach((to, from, next) => {
  7. const token = localStorage.getItem('access_token')
  8. if (to.fullPath != '/login') {
  9. if (token) {
  10. next()
  11. } else {
  12. next('/login')
  13. }
  14. } else {
  15. if (token) {
  16. next('/')
  17. } else {
  18. next()
  19. }
  20. }
  21. })

h5复制文本(原生代码)

  1. function copyWx(wx) {
  2. var input = document.createElement("input");
  3. input.value = wx;
  4. input.id = "input";
  5. document.body.append(input);
  6. input.select(); // 选中文本
  7. document.execCommand("copy"); // 执行浏览器复制命令
  8. input.style.display = "none";
  9. document.getElementById("wxBgBox").style.display = 'block';
  10. }

h5调起微信(应用)

  1. function openWx() {
  2. window.location.href = "weixin://";
  3. }