watch-input.vue 5.2 KB

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