watch-input.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="main-list oBorder">
  3. <!-- 文本框 -->
  4. <input class="main-input" :value="value" :type="_type" :maxlength="maxlength" :placeholder="placeholder" :password="type==='password'&&!showPassword"
  5. @input="$emit('input', $event.target.value)" @blur="$emit('blur', $event)" @focus="$emit('focus', $event)"
  6. @longpress="$emit('longpress', $event)" @confirm="$emit('confirm', $event)" @click="$emit('click', $event)" @longtap="$emit('longtap', $event)"
  7. @touchcancel="$emit('touchcancel', $event)" @touchend="$emit('touchend', $event)" @touchmove="$emit('touchmove', $event)"
  8. @touchstart="$emit('touchstart', $event)" />
  9. <!-- 是否可见密码 -->
  10. <image v-if="_isShowPass&&type==='password'&&!_isShowCode" class="img cuIcon" :class="showPassword?'cuIcon-attention':'cuIcon-attentionforbid'"
  11. @tap="showPass"></image>
  12. <!-- 倒计时 -->
  13. <view v-if="_isShowCode&&!_isShowPass" :class="['vercode',{'vercode-run': second>0}]" @click="setCode">{{ getVerCodeSecond }}</view>
  14. </view>
  15. </template>
  16. <script>
  17. var _this, countDown;
  18. export default {
  19. data() {
  20. return {
  21. showPassword: false, //是否显示明文
  22. second: 0, //倒计时
  23. isRunCode: false, //是否开始倒计时
  24. }
  25. },
  26. props: {
  27. type: String, //类型
  28. value: String, //值
  29. placeholder: String, //框内提示
  30. maxlength: {
  31. //最大长度
  32. type: [Number, String],
  33. default: 20,
  34. },
  35. isShowPass: {
  36. //是否显示密码图标(二选一)
  37. type: [Boolean, String],
  38. default: false,
  39. },
  40. isShowCode: {
  41. //是否显示获取验证码(二选一)
  42. type: [Boolean, String],
  43. default: false,
  44. },
  45. codeText: {
  46. type: String,
  47. default: "获取验证码",
  48. },
  49. setTime: {
  50. //倒计时时间设置
  51. type: [Number, String],
  52. default: 60,
  53. }
  54. },
  55. model: {
  56. prop: 'value',
  57. event: 'input'
  58. },
  59. mounted() {
  60. _this = this
  61. //准备触发
  62. this.$on('runCode', (val) => {
  63. this.runCode(val);
  64. });
  65. clearInterval(countDown); //先清理一次循环,避免缓存
  66. },
  67. methods: {
  68. showPass() {
  69. //是否显示密码
  70. this.showPassword = !this.showPassword
  71. },
  72. setCode() {
  73. //设置获取验证码的事件
  74. if (this.isRunCode) {
  75. //判断是否开始倒计时,避免重复点击
  76. return false;
  77. }
  78. this.$emit('setCode')
  79. },
  80. runCode(val) {
  81. //开始倒计时
  82. if (String(val) == "0") {
  83. //判断是否需要终止循环
  84. this.second = 0; //初始倒计时
  85. clearInterval(countDown); //清理循环
  86. this.isRunCode = false; //关闭循环状态
  87. return false;
  88. }
  89. if (this.isRunCode) {
  90. //判断是否开始倒计时,避免重复点击
  91. return false;
  92. }
  93. this.isRunCode = true
  94. this.second = this._setTime //倒数秒数
  95. let _this = this;
  96. countDown = setInterval(function() {
  97. _this.second--
  98. if (_this.second == 0) {
  99. _this.isRunCode = false
  100. clearInterval(countDown)
  101. }
  102. }, 1000)
  103. }
  104. },
  105. computed: {
  106. _type() {
  107. //处理值
  108. const type = this.type
  109. return type == 'password' ? 'text' : type
  110. },
  111. _isShowPass() {
  112. //处理值
  113. return String(this.isShowPass) !== 'false'
  114. },
  115. _isShowCode() {
  116. //处理值
  117. return String(this.isShowCode) !== 'false'
  118. },
  119. _setTime() {
  120. //处理值
  121. const setTime = Number(this.setTime)
  122. return setTime > 0 ? setTime : 60
  123. },
  124. getVerCodeSecond() {
  125. //验证码倒计时计算
  126. if (this.second <= 0) {
  127. return this.codeText;
  128. } else {
  129. if (this.second < 10) {
  130. return '0' + this.second;
  131. } else {
  132. return this.second;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style>
  140. @import url("./colorui/icon.css");
  141. .main-list {
  142. display: flex;
  143. flex-direction: row;
  144. justify-content: space-between;
  145. align-items: center;
  146. /* height: 36rpx; */
  147. /* Input 高度 */
  148. color: #333333;
  149. padding: 30rpx 32rpx;
  150. margin: 32rpx 0;
  151. background-color: #F5F6F8;
  152. border-radius: 10rpx;
  153. }
  154. .img {
  155. width: 32rpx;
  156. height: 32rpx;
  157. font-size: 32rpx;
  158. }
  159. .main-input {
  160. flex: 1;
  161. text-align: left;
  162. font-size: 28rpx;
  163. /* line-height: 100rpx; */
  164. padding-right: 10rpx;
  165. /* padding-left: 20rpx; */
  166. /* border-left: 2rpx solid #E0E0E0; */
  167. color: #999999;
  168. }
  169. .vercode {
  170. color: #999999;
  171. font-size: 24rpx;
  172. /* line-height: 100rpx; */
  173. }
  174. .vercode-run {
  175. color: #999999 !important;
  176. }
  177. .oBorder {
  178. /* border-bottom: 2rpx solid #E0E0E0; */
  179. /* border: 2rpx solid #E0E0E0; */
  180. }
  181. </style>