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