watch-input.vue 4.8 KB

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