2024-08-21

在JavaScript中实现常见的脱敏功能,可以通过自定义函数来进行。以下是实现手机号、邮箱、身份证号和姓名的简单脱敏方法:




// 手机号脱敏
function maskPhone(phone) {
  return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}
 
// 邮箱脱敏
function maskEmail(email) {
  return email.replace(/\w(?=\w{1,14}@)[^\.]+/g, function(match) {
    return match.replace(/./g, '*');
  });
}
 
// 身份证号脱敏
function maskId(id) {
  return id.replace(/^(\d{6})\d{8}(\d{4})$/, '$1******$2');
}
 
// 姓名脱敏
function maskName(name) {
  if (name.length === 2) {
    return name.charAt(0) + '*';
  } else if (name.length > 2) {
    return name.charAt(0) + '*' + name.charAt(name.length - 1);
  } else {
    return name;
  }
}
 
// 示例
console.log(maskPhone('13812345678')); // 输出: 138****5678
console.log(maskEmail('user@example.com')); // 输出: ****@example.com
console.log(maskId('123456789012345678')); // 输出: 123456******5678
console.log(maskName('张三')); // 输出: 张*
console.log(maskName('L')); // 输出: L

这些函数分别实现了手机号、邮箱、身份证号和姓名的简单脱敏处理。具体的脱敏规则可以根据实际需求进行调整。例如,邮箱脱敏可以只替换中间部分,或者根据邮件服务商的不同进行特定的处理。

2024-08-21

在Node.js中,后缀为.js.mjs.cjs的文件都被视为JavaScript文件。它们之间的区别在于如何导入模块以及如何处理模块的语法。

  1. .js:这是Node.js中默认的文件扩展名,没有特殊配置时,无论是import还是require都可以正常使用。
  2. .mjs:这是ECMAScript模块的标准扩展名。在Node.js中,要使.mjs文件正常工作,需要在package.json中添加"type": "module"声明,或者在命令行启动Node.js时使用--experimental-modules标志。
  3. .cjs:这是Node.js中的CommonJS扩展名,它是Node.js原生支持的模块系统。

例子代码:




// profile.js (CommonJS模块)
module.exports = {
  name: 'Alice',
  age: 25
};
 
// main.js (CommonJS模块)
const profile = require('./profile.cjs');
console.log(profile);
 
// 或者使用ES模块导入
import profile from './profile.mjs';
console.log(profile);

在上述代码中,profile.js是一个CommonJS模块,它使用module.exports导出数据。在main.js中,我们使用require来导入CommonJS模块。对于profile.mjs,它是一个ECMAScript模块,它使用export关键字导出数据,并且需要在package.json中声明"type": "module"或使用--experimental-modules标志。

注意:在实际开发中,为了保持代码的兼容性和清晰性,通常会选择一种模块系统进行使用,而不是混合多种。

2024-08-21

在Vue中结合Echarts实现水波图、水球图、水半球样式和圆形水进度条,可以通过Echarts的自定义系列(custom series)来实现。以下是一个简单的例子:

首先,确保已经安装了Echarts:




npm install echarts --save

然后,在Vue组件中使用Echarts创建图表:




<template>
  <div ref="waterChart" style="width: 400px; height: 400px;"></div>
</template>
 
<script>
import * as echarts from 'echarts/core';
import { WaterWaveSeries } from 'echarts-waterwave'; // 水波图
import { LiquidFillSeries } from 'echarts-liquidfill'; // 水球图
import 'echarts/theme/macarons'; // 主题
 
echarts.use([
  WaterWaveSeries,
  LiquidFillSeries
]);
 
export default {
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.waterChart, 'macarons');
 
      const option = {
        series: [
          {
            type: 'waterWave', // 水波图
            coordinateSystem: 'cartesian2d',
            shape: {
              type: 'circle'
            },
            data: [0.7]
            // 其他配置项...
          },
          {
            type: 'liquidFill', // 水球图
            data: [0.4],
            // 其他配置项...
          }
          // 更多系列...
        ]
      };
 
      chart.setOption(option);
    }
  }
};
</script>

在这个例子中,我们使用了echarts-waterwaveecharts-liquidfill两个Echarts的扩展库来提供水波图和水球图的系列。你可以根据需要添加更多的图表样式。记得在项目中安装这些扩展库:




npm install echarts-waterwave echarts-liquidfill

这样就可以在Vue组件中渲染出水波图、水球图等水样式的图表了。

2024-08-21



<template>
  <div class="table-container">
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :sortable="column.sortable"
      ></el-table-column>
    </el-table>
  </div>
</template>
 
<script>
export default {
  name: 'MyTable',
  props: {
    tableData: {
      type: Array,
      required: true
    },
    columns: {
      type: Array,
      required: true,
      validator: columns => columns.every(column => 'prop' in column && 'label' in column)
    }
  }
}
</script>
 
<style scoped>
.table-container {
  margin-top: 10px;
}
</style>

这个简单的Vue 3组件使用了Element UI的el-tableel-table-column组件来创建一个表格。组件接收tableDatacolumns作为props,其中tableData是表格数据数组,而columns定义了表格列的相关信息。这个组件可以被用来展示各种数据表,并且可以通过props灵活配置列信息。

2024-08-21



<template>
  <a-upload
    :action="uploadAction"
    :headers="uploadHeaders"
    :before-upload="beforeUpload"
    @change="handleChange">
    <a-button> <a-icon type="upload" /> 本地上传 </a-button>
  </a-upload>
  <video v-if="videoUrl" :src="videoUrl" controls style="margin-top: 10px;"></video>
</template>
 
<script>
export default {
  data() {
    return {
      uploadAction: '/jeecg-boot/sys/common/upload', // 上传的接口地址
      uploadHeaders: { authorization: 'Bearer ' + this.$ls.get('ACCESS_TOKEN') }, // 上传的请求头
      videoUrl: '' // 视频播放地址
    };
  },
  methods: {
    beforeUpload(file) {
      const isVideo = file.type === 'video/mp4';
      if (!isVideo) {
        this.$message.error('只能上传mp4格式的视频!');
      }
      return isVideo || Upload.abort;
    },
    handleChange({ file, fileList }) {
      if (file.status === 'done') {
        this.videoUrl = file.response.message; // 假设响应中包含视频地址
      }
    }
  }
};
</script>

这段代码使用了Ant Design Vue的<a-upload>组件来上传视频,并在上传成功后通过handleChange方法来处理响应,并更新视频播放地址。beforeUpload方法用于检查上传的文件是否为mp4格式,不是则阻止上传。在<video>标签中使用了v-if指令来控制视频的渲染,当videoUrl有值时,显示视频播放器。这个例子简洁明了,展示了如何在JeecgBoot项目中实现视频上传及播放的功能。

2024-08-21

在Vue项目中引入阿里妈妈(iconfont)的图标,你需要进行以下几个步骤:

  1. 在iconfont网站上选取所需图标,添加到项目,并下载到本地。
  2. 解压下载的文件,将iconfont.cssiconfont.js(如果有)复制到Vue项目的assetsstatic文件夹中。
  3. 在Vue组件中通过<style>标签引入iconfont.css,并通过<svg>标签使用图标。

以下是一个简单的示例:

首先,将iconfont.css放置在Vue项目的assets文件夹中,例如:/src/assets/iconfont.css

然后,在你的Vue组件中这样使用:




<template>
  <div>
    <!-- 使用图标,class名需要参照iconfont.css中定义的 -->
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-example"></use>
    </svg>
  </div>
</template>
 
<script>
export default {
  name: 'MyIconComponent'
}
</script>
 
<style>
/* 引入iconfont.css */
@import "~@/assets/iconfont.css";
 
.icon {
  width: 1em; height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>

确保你的Vue项目中的webpack配置能够处理svgxlink:href。如果你使用的是Vue CLI创建的项目,通常这个配置默认就有。

iconfont.css中,图标的类名通常是以icon-开头的,你需要根据实际情况替换#icon-example中的example为你需要的图标名称。

注意:如果你的项目中已经使用了vue-awesome-swiper或类似的Swiper插件,那么可能会出现图标不显示的问题。这是因为Swiper会改变svgdisplay属性。要解决这个问题,你可以在全局样式中添加以下代码:




.swiper-container svg {
  display: inline-block;
}

这样应该可以解决大部分的兼容性问题。

2024-08-21



// 引入Vue和VueBigScreenPlugin
import Vue from 'vue';
import VueBigScreenPlugin from 'vue-big-screen-plugin';
 
// 使用VueBigScreenPlugin插件
Vue.use(VueBigScreenPlugin);
 
// 在Vue组件中使用big-screen指令和small-screen指令
export default {
  methods: {
    enterFullscreen() {
      // 使用big-screen指令进入全屏模式
      this.$bigScreen.enter();
    },
    exitFullscreen() {
      // 使用small-screen指令退出全屏模式
      this.$bigScreen.exit();
    }
  }
}

这段代码展示了如何在Vue应用中引入并使用vue-big-screen-plugin插件。通过Vue.use()方法注册插件后,可以在Vue组件中使用big-screensmall-screen指令来控制全屏和退出全屏模式。这有助于开发者更方便地管理Web应用的全屏状态,并且可以根据全屏状态来调整界面布局和功能。

2024-08-21

在Ant Design Vue中,要设置a-input组件的输入类型为数字,可以使用type="number"属性。这样,输入框就只能接受数字输入,并且会带有一个小键盘,用于输入移动设备上的数字。

下面是一个简单的例子:




<template>
  <a-form-item label="数量">
    <a-input
      type="number"
      v-model="form.quantity"
      @change="handleQuantityChange"
    />
  </a-form-item>
</template>
 
<script>
export default {
  data() {
    return {
      form: {
        quantity: 0,
      },
    };
  },
  methods: {
    handleQuantityChange(value) {
      // 处理数量变化
      console.log('新的数量:', value);
    },
  },
};
</script>

在这个例子中,a-input组件被设置为数字类型,并通过v-model绑定到form.quantity数据模型上。当数量改变时,会触发handleQuantityChange方法。

2024-08-21

在Vue项目中,如果你需要使用代理配置来重写请求的路径,你可以使用pathRewriterewrite选项。以下是一个简单的例子,展示如何使用这两种方式来重写请求路径。




// vue.config.js
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://backend.example.com',
        pathRewrite: function (path, req) {
          // 使用正则表达式匹配到/api,并将其替换为空字符串
          return path.replace(/^\/api/, '');
        },
        changeOrigin: true
      },
      '/special-endpoint': {
        target: 'http://other-backend.example.com',
        rewrite: function (path) {
          // 直接重写路径到新的端点
          return '/new-endpoint';
        },
        changeOrigin: true
      }
    }
  }
};

在这个配置中:

  • /api 的代理配置使用了pathRewrite,它是一个函数,它接收当前请求的路径和请求对象作为参数,并返回重写后的路径。这个例子中,它使用正则表达式将路径开头的/api替换为空字符串。
  • /special-endpoint 的代理配置使用了rewrite,它也是一个函数,它接收当前请求的路径作为参数,并返回重写后的路径。这个例子中,它直接将请求重写到/new-endpoint

这样配置后,当你向/api/some/path发送请求时,它将被代理到http://backend.example.com/some/path;而向/special-endpoint发送请求时,它将被代理到http://other-backend.example.com/new-endpoint

2024-08-21



<template>
  <div class="lottery-container">
    <div class="lottery-button" @click="startLottery">点击抽奖</div>
    <div class="prize-list" :style="{ '--index': currentIndex }" @transitionend="onTransitionEnd">
      <div class="prize-item" v-for="(prize, index) in prizes" :key="index">
        {{ prize.name }}
      </div>
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      currentIndex: 0,
      prizes: [
        { name: '奖品一' },
        { name: '奖品二' },
        { name: '奖品三' },
        // ...更多奖品
      ],
      isAnimating: false,
    };
  },
  methods: {
    startLottery() {
      if (this.isAnimating) return;
      const nextIndex = this.getRandomIndex();
      this.animate(nextIndex);
    },
    animate(nextIndex) {
      this.isAnimating = true;
      this.currentIndex = nextIndex;
    },
    onTransitionEnd() {
      this.isAnimating = false;
      // 这里可以添加中奖后的处理逻辑,例如发放奖品
    },
    getRandomIndex() {
      return Math.floor(Math.random() * this.prizes.length);
    },
  },
};
</script>
 
<style scoped>
.lottery-container {
  position: relative;
  width: 300px;
  height: 150px;
  overflow: hidden;
}
 
.lottery-button {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  background-color: #f00;
  color: #fff;
  cursor: pointer;
}
 
.prize-list {
  display: flex;
  width: 300%;
  animation: slide 2s var(--ease-in-out) forwards;
  transform: translateX(-calc(var(--index) * 100%));
}
 
.prize-item {
  flex: 1 0 100%;
  text-align: center;
  line-height: 150px;
}
 
@keyframes slide {
  to {
    transform: translateX(-calc(var(--index) * 100%));
  }
}
 
/* 自定义缓动函数 */
.lottery-container {
  --ease-in-out: cubic-bezier(0.445, 0.05, 0.55, 0.95);
}
</style>

这个代码实例提供了一个基本的无缝横向滚动抽奖的Vue组件。用户点击"点击抽奖"按钮后,会随机滚动至一个奖品项。你可以在onTransitionEnd方法中添加中奖后的处理逻辑,例如发放奖品。这个例子使用了CSS动画和Vue的数据绑定来实现动态的滚动效果。