首页 > 其他分享 >前端输入框脱敏(vue)

前端输入框脱敏(vue)

时间:2024-01-18 17:11:57浏览次数:25  
标签:vue return isDesensitization 输入框 str returnStr thatValue type 脱敏

1.组件

<script lang="tsx">
import type { PropType } from 'vue';
import { type InputProps, Input } from 'ant-design-vue';
import { EyeInvisibleOutlined, EyeTwoTone } from '@ant-design/icons-vue';
import { isNil } from 'lodash-es';
// 通用脱敏
export function desensitizeString(str: string) {
  //姓名脱敏
  let returnStr = '';
  if (str && str != undefined && str != '') {
	str = `${str}`;
	if (str.length == 2) {
	  returnStr = str.substring(0, 1) + '*';
	}
	if (str.length > 2) {
	  returnStr += str.substring(0, 1);
	  for (let i = 0; i < str.length - 2; i++) {
		returnStr += '*';
	  }

	  returnStr += str.charAt(str.length - 1);
	}
	return returnStr;
  } else {
	return returnStr;
  }
}
//电话号码脱敏
export function desensitizePhoneNumber(phoneNumber: string) {
  if (isNil(phoneNumber)) {
	return phoneNumber;
  }
  return phoneNumber.toString().replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}

export default defineComponent({
  name: 'XDesensitizationInput',
  props: {
	inputProps: { type: Object as PropType<InputProps> },
	modelValue: {
	  type: String,
	  default: ''
	},
	type: {
	  type: String as PropType<'mobile' | 'default'>,
	  default: 'default'
	},
	//未添加逻辑
	desensitizationValue: {
	  type: String
	},
	//是否需要脱敏desensitization
	isShowDesensitization: { type: Boolean, default: true },
	isDesensitizationDefaultValue: { type: Boolean, default: true }
  },
  data() {
	const { isShowDesensitization } = this;
	let isDesensitization = true;
	if (isShowDesensitization) {
	  isDesensitization = this.isDesensitizationDefaultValue;
	} else {
	  isDesensitization = false;
	}
	return {
	  thatValue: '',
	  oldThatValue: '',
	  isDesensitization
	};
  },
  created() {
	this.setThatValue(this.modelValue);
  },
  emits: ['update:modelValue'],

  watch: {
	modelValue: {
	  handler(newValue) {
		if (this.isDesensitization) {
		  if (this.oldThatValue == newValue) {
			return;
		  }
		} else {
		  if (this.thatValue == newValue) {
			return;
		  }
		}

		this.setThatValue(newValue);
	  },
	  deep: true,
	  immediate: true
	}
  },
  computed: {
	trimmedThatValue: {
	  get() {
		return this.thatValue;
	  },
	  set(value: any) {
		this.thatValue = value.trim();
	  }
	}
  },

  methods: {
	handleEyeClick() {
	  this.isDesensitization = !this.isDesensitization;

	  if (!this.isDesensitization) {
		this.setThatValue(this.oldThatValue);
	  } else {
		this.setThatValue(this.thatValue);
	  }
	},
	handelInputChange() {
	  this.$emit('update:modelValue', this.thatValue);
	},
	setThatValue(value: string) {
	  if (this.isDesensitization) {
		this.oldThatValue = value;

		if (this.type == 'mobile') {
		  this.thatValue = desensitizePhoneNumber(this.oldThatValue);
		} else {
		  this.thatValue = desensitizeString(this.oldThatValue);
		}
	  } else {
		this.thatValue = value;
		this.oldThatValue = value;
	  }
	},

	renderEyeTwoTone() {
	  const { isDesensitization } = this.$data;
	  if (!this.isShowDesensitization) {
		return null;
	  }
	  if (!isDesensitization) {
		return <EyeTwoTone onClick={this.handleEyeClick} />;
	  } else {
		return <EyeInvisibleOutlined onClick={this.handleEyeClick} />;
	  }
	}
  },

  render() {
	const { inputProps } = this.$props;
	const { isDesensitization } = this.$data;

	return (
	  <>
		<Input
		  {...(inputProps || {})}
		  v-model:value={this.trimmedThatValue}
		  onChange={this.handelInputChange}
		  disabled={isDesensitization || inputProps?.disabled}
		>
		  {{ suffix: this.renderEyeTwoTone }}
		</Input>
	  </>
	);
  }
});
</script>

2.注册

import XDesensitizationInput from '@/components/xDesensitizationInput/index.vue';
app.component('XDesensitizationInput', XDesensitizationInput);

3.使用

 <a-form-item
    name="name"
    :rules="[{ required: isEdit ? true : false, message: '请输入姓名' }]"
    label="姓名"
  >
    <XDesensitizationInput
      :inputProps="{ maxlength: 20, placeholder: '请输入', disabled: false }"
      v-model="userInfo.name"
      :is-desensitization-default-value="true"
      :is-show-desensitization="true"
      v-if="isEdit"
    />
    <XDesensitizationInput
      :inputProps="{ placeholder: '请输入', disabled: true }"
      v-model="userInfo.name"
      :is-desensitization-default-value="true"
      :is-show-desensitization="true"
      v-else
    />
</a-form-item>

标签:vue,return,isDesensitization,输入框,str,returnStr,thatValue,type,脱敏
From: https://www.cnblogs.com/songkomei/p/17972938

相关文章

  • 一文掌握Vue3函数式组件中的confirm实现技巧!
    在做后台项目时候,使用声明式组件比较多,就是写一个.vue文件,在里面写template、script、style哪里需要,就在哪里导入。而对于前台项目而言,我们期望可以直接通过方法的形式调用,利用函数式组件,在封装时除了要写.vue,还要多一个手动渲染和卸载的步骤。我们可以通过h函数可以生成一个vno......
  • vue+antd-vue(自定义iconfont图标组件)
    1.方式一代码如下import{createFromIconfontCN}from'@ant-design/icons-vue';constIconFont=createFromIconfontCN({scriptUrl:newURL('./assets/font/iconfont.js',import.meta.url).href});app.component('IconFont',IconFont);......
  • vue2中使用v-selectpage插件 搜索并分页
    <v-selectpagedata="/api/intrusionevent/lists"v-model="temp.event_id"key-field="id"show-field="description"search-field="de......
  • 解决vue用axiso两次访问后台api,发现后台的sessionID不一致
    我用的是ASP.NETCore7.0做的后台api,在解决了跨域问题(这个问题在官网上就有答案https://learn.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-7.0)为了方便阅读,我再讲一下在里progam里面增加代码(黄色代码),代码格式我就把不变的放到一起了解决完这个之后,因为要......
  • 异步加载树形组件(antd-vue)
    1、html<a-directory-tree:tree-data="useDataSourceTreeList"v-model:selectedKeys="selectedKey"v-if="datasourceId"blockNodeclass="tree-card":showIco......
  • Vue3.0 路由动画(页面跳转)
    前言vue3.0的页面组件之前切换的动画效果,在移动端H5页面,交互体验比较好,就是带Vue3的Transition组件 之前的写法是 Transition的组件要包在routerView外面,但是3.0的语法就是要在在里面了,不然会黄色警告<divclass="animation"><RouterViewv-slot="{Component,......
  • 记一个vue2中使用路由时,在同一个页面跳转,但是url参数不同,不会重新渲染页面的问题
    vue2中使用路由时,页面自己跳转自己,但是携带的参数不一样预期想要的结果是:感冒2会跟随着url的参数进行变化,但是并没用 解决方法: 在App.vue这个页面中的router-view添加  :key="$route.fullPath"结果在自己跳转自己之后会刷新页面 达成:参考:https://blog.csdn.ne......
  • HBuilderX mac M1 打包 vite/vue3 报错处理办法(pnpm)
    项目运行h5的时候都没有问题,但是要运行到微信开发者工具的时候打包报11:40:54.480Specificallythe"esbuild-darwin-arm64"packageispresentbutthisplatform11:40:54.480needsthe"esbuild-darwin-64"packageinstead.Peopleoftengetintothis很好看去论......
  • vue+tsc+noEmit导致打包报TS类型错误问题及解决方法
    当我们新建vue3项目,package.json文件会自动给我添加一些配置选项,这写选项基本没有问题,但是在实际操作过程中,当项目越来越复杂就会出现问题。本文列举一个目前我遇到的一个问题:打包后报了一堆TS类型错误,怎么消除这些错误?项目环境:Vue3+Vite+TS当项目进行打包时候,突然发现终端......
  • vue3+lottie-web加载json格式动画
    项目中要用动画设计说gif会失真,用json格式动画吧。我虎躯一震,json格式动画什么鬼?lottie库什么鬼。。。。不废话,直接上重点环境:编辑器webstorm,前端技术栈vue3+vite+ts安装lottie-webyarnaddlottie-web引入lottie,引入json格式动画文件.importlottiefrom'lott......