watch-input.vue 4.6 KB

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