watch-input.vue 4.7 KB

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