watch-input.vue 4.6 KB

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