u-code-input.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view class="u-code-input">
  3. <view
  4. class="u-code-input__item"
  5. :style="[itemStyle(index)]"
  6. v-for="(item, index) in codeLength"
  7. :key="index"
  8. >
  9. <view
  10. class="u-code-input__item__dot"
  11. v-if="dot && codeArray.length > index"
  12. ></view>
  13. <text
  14. v-else
  15. :style="{
  16. fontSize: addUnit(fontSize),
  17. fontWeight: bold ? 'bold' : 'normal',
  18. color: color
  19. }"
  20. >{{codeArray[index]}}</text>
  21. <view
  22. class="u-code-input__item__line"
  23. v-if="mode === 'line'"
  24. :style="[lineStyle]"
  25. ></view>
  26. <!-- #ifndef APP-NVUE -->
  27. <view v-if="isFocus && codeArray.length === index"
  28. :style="{backgroundColor: color}" class="u-code-input__item__cursor"></view>
  29. <!-- #endif -->
  30. <!-- #ifdef APP-NVUE -->
  31. <view v-if="isFocus && codeArray.length === index"
  32. :style="{backgroundColor: color, opacity: opacity}" class="u-code-input__item__cursor"></view>
  33. <!-- #endif -->
  34. </view>
  35. <input
  36. :disabled="disabledKeyboard"
  37. type="number"
  38. :focus="focus"
  39. :value="inputValue"
  40. :maxlength="maxlength"
  41. :adjustPosition="adjustPosition"
  42. class="u-code-input__input"
  43. @input="inputHandler"
  44. :style="{
  45. height: addUnit(size)
  46. }"
  47. @focus="isFocus = true"
  48. @blur="isFocus = false"
  49. />
  50. </view>
  51. </template>
  52. <script>
  53. import { props } from './props';
  54. import { mpMixin } from '../../libs/mixin/mpMixin';
  55. import { mixin } from '../../libs/mixin/mixin';
  56. import { addUnit, getPx } from '../../libs/function/index';
  57. /**
  58. * CodeInput 验证码输入
  59. * @description 该组件一般用于验证用户短信验证码的场景,也可以结合uview-plus的键盘组件使用
  60. * @tutorial https://ijry.github.io/uview-plus/components/codeInput.html
  61. * @property {String | Number} maxlength 最大输入长度 (默认 6 )
  62. * @property {Boolean} dot 是否用圆点填充 (默认 false )
  63. * @property {String} mode 显示模式,box-盒子模式,line-底部横线模式 (默认 'box' )
  64. * @property {Boolean} hairline 是否细边框 (默认 false )
  65. * @property {String | Number} space 字符间的距离 (默认 10 )
  66. * @property {String | Number} value 预置值
  67. * @property {Boolean} focus 是否自动获取焦点 (默认 false )
  68. * @property {Boolean} bold 字体和输入横线是否加粗 (默认 false )
  69. * @property {String} color 字体颜色 (默认 '#606266' )
  70. * @property {String | Number} fontSize 字体大小,单位px (默认 18 )
  71. * @property {String | Number} size 输入框的大小,宽等于高 (默认 35 )
  72. * @property {Boolean} disabledKeyboard 是否隐藏原生键盘,如果想用自定义键盘的话,需设置此参数为true (默认 false )
  73. * @property {String} borderColor 边框和线条颜色 (默认 '#c9cacc' )
  74. * @property {Boolean} disabledDot 是否禁止输入"."符号 (默认 true )
  75. *
  76. * @event {Function} change 输入内容发生改变时触发,具体见上方说明 value:当前输入的值
  77. * @event {Function} finish 输入字符个数达maxlength值时触发,见上方说明 value:当前输入的值
  78. * @example <u-code-input v-model="value4" :focus="true"></u-code-input>
  79. */
  80. export default {
  81. name: 'u-code-input',
  82. mixins: [mpMixin, mixin, props],
  83. data() {
  84. return {
  85. inputValue: '',
  86. isFocus: this.focus,
  87. timer: null,
  88. opacity: 1
  89. }
  90. },
  91. watch: {
  92. // #ifdef VUE2
  93. value: {
  94. immediate: true,
  95. handler(val) {
  96. // 转为字符串,超出部分截掉
  97. this.inputValue = String(val).substring(0, this.maxlength)
  98. }
  99. },
  100. // #endif
  101. // #ifdef VUE3
  102. modelValue: {
  103. immediate: true,
  104. handler(val) {
  105. // 转为字符串,超出部分截掉
  106. this.inputValue = String(val).substring(0, this.maxlength)
  107. }
  108. },
  109. // #endif
  110. isFocus: {
  111. handler(val) {
  112. // #ifdef APP-NVUE
  113. if (val) {
  114. this.timer = setInterval(() => {
  115. this.opacity = Math.abs(this.opacity - 1)
  116. }, 600)
  117. } else {
  118. clearInterval(this.timer)
  119. }
  120. // #endif
  121. }
  122. }
  123. },
  124. created() {
  125. },
  126. beforeUnmount() {
  127. // #ifdef APP-NVUE
  128. clearInterval(this.timer)
  129. // #endif
  130. },
  131. computed: {
  132. // 根据长度,循环输入框的个数,因为头条小程序数值不能用于v-for
  133. codeLength() {
  134. return new Array(Number(this.maxlength))
  135. },
  136. // 循环item的样式
  137. itemStyle() {
  138. return index => {
  139. const style = {
  140. width: addUnit(this.size),
  141. height: addUnit(this.size)
  142. }
  143. // 盒子模式下,需要额外进行处理
  144. if (this.mode === 'box') {
  145. // 设置盒子的边框,如果是细边框,则设置为0.5px宽度
  146. style.border = `${this.hairline ? 0.5 : 1}px solid ${this.borderColor}`
  147. // 如果盒子间距为0的话
  148. if (getPx(this.space) === 0) {
  149. // 给第一和最后一个盒子设置圆角
  150. if (index === 0) {
  151. style.borderTopLeftRadius = '3px'
  152. style.borderBottomLeftRadius = '3px'
  153. }
  154. if (index === this.codeLength.length - 1) {
  155. style.borderTopRightRadius = '3px'
  156. style.borderBottomRightRadius = '3px'
  157. }
  158. // 最后一个盒子的右边框需要保留
  159. if (index !== this.codeLength.length - 1) {
  160. style.borderRight = 'none'
  161. }
  162. }
  163. }
  164. if (index !== this.codeLength.length - 1) {
  165. // 设置验证码字符之间的距离,通过margin-right设置,最后一个字符,无需右边框
  166. style.marginRight = addUnit(this.space)
  167. } else {
  168. // 最后一个盒子的有边框需要保留
  169. style.marginRight = 0
  170. }
  171. return style
  172. }
  173. },
  174. // 将输入的值,转为数组,给item历遍时,根据当前的索引显示数组的元素
  175. codeArray() {
  176. return String(this.inputValue).split('')
  177. },
  178. // 下划线模式下,横线的样式
  179. lineStyle() {
  180. const style = {}
  181. style.height = this.hairline ? '2px' : '4px'
  182. style.width = addUnit(this.size)
  183. // 线条模式下,背景色即为边框颜色
  184. style.backgroundColor = this.borderColor
  185. return style
  186. }
  187. },
  188. emits: ["change", 'finish', "update:modelValue"],
  189. methods: {
  190. addUnit,
  191. // 监听输入框的值发生变化
  192. inputHandler(e) {
  193. const value = e.detail.value
  194. this.inputValue = value
  195. // 是否允许输入“.”符号
  196. if(this.disabledDot) {
  197. this.$nextTick(() => {
  198. this.inputValue = value.replace('.', '')
  199. })
  200. }
  201. // 未达到maxlength之前,发送change事件,达到后发送finish事件
  202. this.$emit('change', value)
  203. // 修改通过v-model双向绑定的值
  204. // #ifdef VUE3
  205. this.$emit("update:modelValue", value);
  206. // #endif
  207. // #ifdef VUE2
  208. this.$emit("input", value);
  209. // #endif
  210. // 达到用户指定输入长度时,发出完成事件
  211. if (String(value).length >= Number(this.maxlength)) {
  212. this.$emit('finish', value)
  213. }
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="scss" scoped>
  219. @import "../../libs/css/components.scss";
  220. $u-code-input-cursor-width: 1px;
  221. $u-code-input-cursor-height: 20px;
  222. $u-code-input-cursor-animation-duration: 1s;
  223. $u-code-input-cursor-animation-name: u-cursor-flicker;
  224. .u-code-input {
  225. @include flex;
  226. position: relative;
  227. overflow: hidden;
  228. &__item {
  229. @include flex;
  230. justify-content: center;
  231. align-items: center;
  232. position: relative;
  233. &__text {
  234. font-size: 15px;
  235. color: $u-content-color;
  236. }
  237. &__dot {
  238. width: 7px;
  239. height: 7px;
  240. border-radius: 100px;
  241. background-color: $u-content-color;
  242. }
  243. &__line {
  244. position: absolute;
  245. bottom: 0;
  246. height: 4px;
  247. border-radius: 100px;
  248. width: 40px;
  249. background-color: $u-content-color;
  250. }
  251. &__cursor {
  252. position: absolute;
  253. /* #ifndef APP-NVUE */
  254. top: 50%;
  255. left: 50%;
  256. opacity: 1;
  257. transform: translate(-50%,-50%);
  258. /* #endif */
  259. width: $u-code-input-cursor-width;
  260. height: $u-code-input-cursor-height;
  261. animation: $u-code-input-cursor-animation-duration u-cursor-flicker infinite;
  262. }
  263. }
  264. &__input {
  265. // 之所以需要input输入框,是因为有它才能唤起键盘
  266. // 这里将它设置为两倍的屏幕宽度,再将左边的一半移出屏幕,为了不让用户看到输入的内容
  267. position: absolute;
  268. left: -750rpx;
  269. width: 1500rpx;
  270. top: 0;
  271. background-color: transparent;
  272. text-align: left;
  273. }
  274. }
  275. /* #ifndef APP-NVUE */
  276. @keyframes u-cursor-flicker {
  277. 0% {
  278. opacity: 0;
  279. }
  280. 50% {
  281. opacity: 1;
  282. }
  283. 100% {
  284. opacity: 0;
  285. }
  286. }
  287. /* #endif */
  288. </style>