watch-input.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. v-if="_isShowGet"
  28. class="vercode"
  29. @click="setNumberCode"
  30. >官方邀请码</view>
  31. </view>
  32. </template>
  33. <script>
  34. var _this, countDown;
  35. export default{
  36. data(){
  37. return{
  38. showPassword: false, //是否显示明文
  39. second: 0, //倒计时
  40. isRunCode: false, //是否开始倒计时
  41. }
  42. },
  43. props:{
  44. type: String, //类型
  45. value: String, //值
  46. placeholder: String, //框内提示
  47. maxlength: {
  48. //最大长度
  49. type: [Number,String],
  50. default: 20,
  51. },
  52. isShowPass:{
  53. //是否显示密码图标(二选一)
  54. type: [Boolean,String],
  55. default: false,
  56. },
  57. isShowCode:{
  58. //是否显示获取验证码(二选一)
  59. type: [Boolean,String],
  60. default: false,
  61. },
  62. isShowGet:{
  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. onInput(e) {
  95. //传出值
  96. this.$emit('input', e.target.value)
  97. },
  98. setCode(){
  99. //设置获取验证码的事件
  100. if(this.isRunCode){
  101. //判断是否开始倒计时,避免重复点击
  102. return false;
  103. }
  104. this.$emit('setCode')
  105. },
  106. setNumberCode(){
  107. this.$emit('setNumberCode')
  108. },
  109. runCode(val){
  110. //开始倒计时
  111. if(String(val)=="0"){
  112. //判断是否需要终止循环
  113. this.second = 0; //初始倒计时
  114. clearInterval(countDown);//清理循环
  115. this.isRunCode= false; //关闭循环状态
  116. return false;
  117. }
  118. if(this.isRunCode){
  119. //判断是否开始倒计时,避免重复点击
  120. return false;
  121. }
  122. this.isRunCode= true
  123. this.second = this._setTime //倒数秒数
  124. let _this=this;
  125. countDown = setInterval(function(){
  126. _this.second--
  127. if(_this.second==0){
  128. _this.isRunCode= false
  129. clearInterval(countDown)
  130. }
  131. },1000)
  132. }
  133. },
  134. computed:{
  135. _type(){
  136. //处理值
  137. const type = this.type
  138. return type == 'password' ? 'text' : type
  139. },
  140. _isShowPass() {
  141. //处理值
  142. return String(this.isShowPass) !== 'false'
  143. },
  144. _isShowCode() {
  145. //处理值
  146. return String(this.isShowCode) !== 'false'
  147. },
  148. _isShowGet() {
  149. //处理值
  150. return String(this.isShowGet) !== 'false'
  151. },
  152. _setTime() {
  153. //处理值
  154. const setTime = Number(this.setTime)
  155. return setTime>0 ? setTime : 60
  156. },
  157. getVerCodeSecond(){
  158. //验证码倒计时计算
  159. if(this.second<=0){
  160. return this.codeText;
  161. }else{
  162. if(this.second<10){
  163. return '0'+this.second;
  164. }else{
  165. return this.second;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. </script>
  172. <style>
  173. @import url("./css/icon.css");
  174. .main-list{
  175. display: flex;
  176. flex-direction: row;
  177. justify-content: space-between;
  178. align-items: center;
  179. height: 90upx; /* Input 高度 */
  180. color: #333333;
  181. padding: 32upx;
  182. margin-top:18upx;
  183. margin-bottom: 18upx;
  184. }
  185. .img{
  186. width: 32upx;
  187. height: 32upx;
  188. font-size: 32upx;
  189. }
  190. .main-input{
  191. flex: 1;
  192. text-align: left;
  193. font-size: 28upx;
  194. /* line-height: 100upx; */
  195. padding-right: 10upx;
  196. margin-left: 20upx;
  197. }
  198. .vercode {
  199. color: rgba(0,0,0,0.7);
  200. font-size: 24upx;
  201. line-height: 100upx;
  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 60upx 0 rgba(43,86,112,.1) ;
  210. box-shadow: 0 0 60upx 0 rgba(43,86,112,.1) ;
  211. }
  212. </style>