index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <view>
  3. <nut-popup
  4. :style="{
  5. padding: '32px 24px 28px 24px',
  6. borderRadius: '12px',
  7. textAlign: 'center'
  8. }"
  9. v-model:visible="show"
  10. :closeable="true"
  11. @click-close-icon="closeIcon"
  12. :close-on-click-overlay="closeOnClickOverlay"
  13. @click-overlay="close"
  14. >
  15. <view class="nut-shortpsd-title">{{ title }}</view>
  16. <view class="nut-shortpsd-subtitle">{{ desc }}</view>
  17. <view class="nut-input-w">
  18. <input
  19. ref="realpwd"
  20. class="nut-input-real"
  21. type="number"
  22. maxlength="6"
  23. v-model="realInput"
  24. @input="changeValue"
  25. />
  26. <view class="nut-shortpsd-fake" @click="focus">
  27. <view
  28. class="nut-shortpsd-li"
  29. v-for="(sublen, index) in new Array(comLen)"
  30. v-bind:key="index"
  31. >
  32. <view
  33. class="nut-shortpsd-icon"
  34. v-if="realInput.length > index"
  35. ></view>
  36. </view>
  37. </view>
  38. </view>
  39. <view class="nut-shortpsd-message">
  40. <view class="nut-shortpsd-error">{{ errorMsg }}</view>
  41. <view class="nut-shortpsd-forget" v-if="tips">
  42. <nut-icon class="icon" size="11px" name="tips"></nut-icon>
  43. <view @click="onTips">{{ tips }}</view>
  44. </view>
  45. </view>
  46. <view v-if="!noButton" class="nut-shortpsd-footer">
  47. <view class="nut-shortpsd-cancle" @click="close">取消</view>
  48. <view class="nut-shortpsd-sure" @click="sureClick">确认</view>
  49. </view>
  50. </nut-popup>
  51. </view>
  52. </template>
  53. <script lang="ts">
  54. import { ref, computed, watch } from 'vue';
  55. import { createComponent } from '@/utils/create';
  56. const { create } = createComponent('shortpassword');
  57. export default create({
  58. props: {
  59. title: {
  60. type: String,
  61. default: '请输入密码'
  62. },
  63. desc: {
  64. type: String,
  65. default: '您使用了虚拟资产,请进行验证'
  66. },
  67. tips: {
  68. type: String,
  69. default: '忘记密码'
  70. },
  71. visible: {
  72. type: Boolean,
  73. default: false
  74. },
  75. value: {
  76. type: String,
  77. default: ''
  78. },
  79. errorMsg: {
  80. type: String,
  81. default: ''
  82. },
  83. noButton: {
  84. type: Boolean,
  85. default: true
  86. },
  87. closeOnClickOverlay: {
  88. type: Boolean,
  89. default: true
  90. },
  91. length: {
  92. type: [String, Number], //4~6
  93. default: 6
  94. }
  95. },
  96. emits: [
  97. 'update:value',
  98. 'update:visible',
  99. 'complete',
  100. 'change',
  101. 'ok',
  102. 'tips',
  103. 'close',
  104. 'cancel'
  105. ],
  106. setup(props, { emit }) {
  107. const realInput = ref(props.value);
  108. const realpwd = ref();
  109. const comLen = computed(() => range(Number(props.length)));
  110. const show = ref(props.visible);
  111. // 方法
  112. function sureClick() {
  113. emit('ok', realInput.value);
  114. }
  115. function focus() {
  116. realpwd.value.focus();
  117. }
  118. watch(
  119. () => props.visible,
  120. value => {
  121. show.value = value;
  122. }
  123. );
  124. function changeValue(e: Event) {
  125. const input = e.target as HTMLInputElement;
  126. let val = input.value;
  127. if (val.length > comLen.value) {
  128. val = val.slice(0, comLen.value);
  129. realInput.value = val;
  130. }
  131. if (realInput.value.length === comLen.value) {
  132. emit('complete', val);
  133. }
  134. emit('change', val);
  135. emit('update:value', val);
  136. }
  137. function close() {
  138. emit('update:visible', false);
  139. emit('cancel');
  140. }
  141. function closeIcon() {
  142. emit('update:visible', false);
  143. emit('close');
  144. }
  145. function range(val: number) {
  146. return Math.min(Math.max(4, val), 6);
  147. }
  148. function onTips() {
  149. emit('tips');
  150. }
  151. return {
  152. comLen,
  153. sureClick,
  154. realInput,
  155. realpwd,
  156. focus,
  157. range,
  158. changeValue,
  159. close,
  160. onTips,
  161. show,
  162. closeIcon
  163. };
  164. }
  165. });
  166. </script>
  167. <style scoped lang="scss">
  168. @import 'index.scss';
  169. </style>