u-code-input.vue 8.5 KB

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