uni-popup-captcha.uvue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <uni-popup ref="popup" @clickMask="cancel">
  3. <view class="popup-captcha">
  4. <view class="content">
  5. <text class="title">{{title}}</text>
  6. <uni-captcha ref="captcha" :focus="focus" :scene="scene" v-model="val" :cursorSpacing="150"></uni-captcha>
  7. </view>
  8. <view class="button-box">
  9. <text @click="cancel" class="btn cancel">取消</text>
  10. <text @click="confirm" class="btn confirm">确认</text>
  11. </view>
  12. </view>
  13. </uni-popup>
  14. </template>
  15. <script>
  16. let confirmCallBack = ():void=>console.log('未传入回调函数')
  17. export default {
  18. emits:["modelValue","confirm","cancel"],
  19. data() {
  20. return {
  21. focus: false,
  22. val:""
  23. }
  24. },
  25. props: {
  26. modelValue: {
  27. type: String,
  28. default: ""
  29. },
  30. value: {
  31. type: String,
  32. default: ""
  33. },
  34. scene: {
  35. type: String,
  36. default: ""
  37. },
  38. title: {
  39. type: String,
  40. default: "默认标题"
  41. }
  42. },
  43. watch: {
  44. val(val:string) {
  45. // console.log(val);
  46. // TODO 兼容 vue2
  47. // #ifdef VUE2
  48. this.$emit('input', val);
  49. // #endif
  50. // TODO 兼容 vue3
  51. // #ifdef VUE3
  52. this.$emit('update:modelValue', val)
  53. // #endif
  54. if(val.length == 4){
  55. this.confirm()
  56. }
  57. }
  58. },
  59. mounted() {},
  60. methods: {
  61. open(callback: () => void) {
  62. // console.log('callback',callback);
  63. confirmCallBack = callback;
  64. this.focus = true
  65. this.val = "";
  66. (this.$refs['popup'] as ComponentPublicInstance).$callMethod("open");
  67. this.$nextTick(()=>{
  68. (this.$refs['captcha'] as ComponentPublicInstance).$callMethod("getImageCaptcha",true);
  69. })
  70. },
  71. close() {
  72. this.focus = false;
  73. (this.$refs['popup'] as ComponentPublicInstance).$callMethod("close");
  74. },
  75. cancel(){
  76. this.close()
  77. this.$emit("cancel")
  78. },
  79. confirm() {
  80. if (this.val.length != 4) {
  81. return uni.showToast({
  82. title: '请填写验证码',
  83. icon: 'none'
  84. });
  85. }
  86. this.close()
  87. this.$emit('confirm')
  88. confirmCallBack()
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. .popup-captcha {
  95. background-color: #fff;
  96. flex-direction: column;
  97. width: 600rpx;
  98. padding:10px 15px;
  99. border-radius: 10px;
  100. }
  101. .popup-captcha .title {
  102. text-align: center;
  103. font-weight: 700;
  104. margin: 5px 0;
  105. }
  106. .popup-captcha .button-box {
  107. flex-direction: row;
  108. justify-content: space-around;
  109. margin-top: 5px;
  110. }
  111. .popup-captcha .button-box .btn {
  112. flex: 1;
  113. height: 35px;
  114. line-height: 35px;
  115. text-align: center;
  116. }
  117. .popup-captcha .button-box .cancel {
  118. border: 1px solid #eee;
  119. color: #666;
  120. }
  121. .confirm {
  122. background-color: #0070ff;
  123. color: #fff;
  124. margin-left: 5px;
  125. }
  126. </style>