vcode-input.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="vcode-input-body">
  3. <text class="vcode-input-item"
  4. :class="isBorderLine?'vcode-input-line':'vcode-input-border'"
  5. v-for="(v,index) in sum"
  6. :key="index"
  7. @tap.stop="setFocus"
  8. :style="{
  9. borderColor:text.length===index&&focus?borderActiveColor:(text.length>index?borderValueColor:borderColor),
  10. color:text.length>index?borderValueColor:borderColor}"
  11. >
  12. {{ text[index]?text[index]:'' }}
  13. </text>
  14. <view class="hidden-input">
  15. <input
  16. id="vcodeInput"
  17. ref="vcodeInput"
  18. type="number"
  19. :show-confirm-bar="false"
  20. auto-blur
  21. :focus="focus"
  22. :maxlength="sum"
  23. v-model="value"
  24. @blur="setBlur"
  25. :password="isPassword"
  26. placeholder="验证码"/>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. name:'VcodeInput',
  33. props: {
  34. sum:{
  35. type: Number,
  36. default: 6
  37. },
  38. isBorderLine:{
  39. type:Boolean,
  40. default:false
  41. },
  42. borderColor:{
  43. type:String,
  44. default:'#e8e8e8'
  45. },
  46. borderValueColor:{
  47. type:String,
  48. default:'#424456'
  49. },
  50. borderActiveColor:{
  51. type:String,
  52. default:'#FF6B00'
  53. },
  54. isAutoComplete:{
  55. type: Boolean,
  56. default: true
  57. },
  58. isPassword:{
  59. type: Boolean,
  60. default: false
  61. }
  62. },
  63. data() {
  64. return {
  65. focus:false,
  66. text:[],
  67. value:''
  68. };
  69. },
  70. watch:{
  71. value(value,oldVal){
  72. if(this.isAutoComplete){
  73. if(value.length>=this.sum){
  74. this.focus=false;
  75. this.$emit('vcodeInput', value);
  76. }
  77. }else{
  78. this.$emit('vcodeInput', value);
  79. }
  80. if(this.isPassword){
  81. let val='';
  82. for (let i = 0; i < value.length; i++) {
  83. val+='●';
  84. }
  85. this.text=val;
  86. }else{
  87. this.text=value.split("");
  88. }
  89. }
  90. },
  91. mounted() {
  92. this.$nextTick(() => {
  93. this.initInput()
  94. })
  95. },
  96. methods:{
  97. initInput(){
  98. this.focus=true;
  99. // #ifdef H5
  100. this.$refs.vcodeInput.$refs.input.setAttribute('type','number');
  101. this.$refs.vcodeInput.$refs.input.setAttribute('pattern','[0-9]*')
  102. // #endif
  103. },
  104. setBlur(){
  105. this.$nextTick(() => {
  106. this.focus=false;
  107. })
  108. },
  109. setFocus(){
  110. this.focus= !this.focus;
  111. },
  112. clearValue(){
  113. this.value='';
  114. }
  115. }
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .vcode-input-body{
  120. margin-left: -36rpx;
  121. margin-right: -36rpx;
  122. position: relative;
  123. overflow: hidden;
  124. /* #ifndef APP-NVUE */
  125. display: flex;
  126. /* #endif */
  127. flex-direction: row;
  128. justify-content: center;
  129. align-items: center;
  130. }
  131. .vcode-input-item{
  132. width: 76rpx;
  133. height: 76rpx;
  134. margin-left: 12rpx;
  135. margin-right: 12rpx;
  136. line-height: 76rpx;
  137. text-align: center;
  138. font-weight: 500;
  139. color: #c4761f !important;
  140. border-radius: 4rpx;
  141. }
  142. .vcode-input-border{
  143. border-style: solid;
  144. border-width: 2rpx;
  145. border-color: $uni-border-color;
  146. border-radius: 4rpx;
  147. }
  148. .vcode-input-line{
  149. border-bottom-style: solid;
  150. border-bottom-width: 2rpx;
  151. border-color: $uni-border-color;
  152. }
  153. .hidden-input{
  154. width: 0px;
  155. height: 0px;
  156. position: absolute;
  157. left: -1px;
  158. top: -1px;
  159. overflow: hidden;
  160. }
  161. </style>