开始

获取验证码功能。点击获取验证码,变成倒计时60秒。60秒后又恢复获取验证码按钮的状态。
image.png
默认是0秒。
image.png

  1. codetime:0

image.png

  1. {{!codetime?'获取验证码':codetime}}

加一个倒计时的秒
image.png

  1. {{!codetime?'获取验证码':codetime+'s'}}

增加点击事件
image.png

  1. @tap="getCheckNum">

image.pngimage.png

image.png
image.png

可以把提示关闭,直接返回。
image.png

  1. // 获取验证码
  2. getCheckNum() {
  3. if (this.codetime > 0) {
  4. return;
  5. }
  6. // 请求服务器,发送验证码
  7. // 发送成功,开启倒计时
  8. this.codetime = 10;
  9. let timer = setInterval(() => {
  10. this.codetime--;
  11. if (this.codetime < 1) {
  12. clearInterval(timer);
  13. this.codetime = 0;
  14. }
  15. }, 1000);
  16. },

本节代码

  1. <template>
  2. <view>
  3. <!-- 状态栏 -->
  4. <!-- <uni-status-bar></uni-status-bar> -->
  5. <uni-nav-bar background-color="#FFE933" status-bar />
  6. <!-- 关闭按钮 -->
  7. <view class="icon iconfont icon-guanbi" @tap="back"></view>
  8. <!-- 引入背景图 -->
  9. <image class="loginhead" src="../../static/common/loginhead.png" mode="widthFix" lazy-load></image>
  10. <view class="body">
  11. <!-- 账号密码登陆 -->
  12. <template v-if="!status">
  13. <view class="login-input-box">
  14. <!-- <view class="phone u-f-ajc">+86</view> -->
  15. <input type="text" v-model="username" class="uni-input common-input" placeholder="昵称/手机号/邮箱" />
  16. </view>
  17. <view class="login-input-box">
  18. <view class="login-input-box">
  19. <input type="text" v-model="password" class="uni-input common-input forget-input"
  20. placeholder="请输入密码" />
  21. <view class="forget u-f-ajc login-font-color">忘记密码</view>
  22. </view>
  23. </view>
  24. </template>
  25. <template v-else>
  26. <view class="login-input-box">
  27. <view class="phone u-f-ajc">+86</view>
  28. <input type="text" v-model="phone" class="uni-input common-input phone-input" placeholder="手机号" />
  29. </view>
  30. <view class="login-input-box">
  31. <input type="text" v-model="checknum" class="uni-input common-input forget-input"
  32. placeholder="验证码" />
  33. <view class="forget u-f-ajc login-font-color yanzhengma" @tap="getCheckNum">
  34. <view class="u-f-ajc">
  35. {{!codetime?'获取验证码':codetime+'s'}}
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <button class="user-set-btn" :class="{'user-set-btn-disable':disabled}" :loading="loading" @tap="submit"
  41. type="primary" :disabled="disabled">登陆</button>
  42. </view>
  43. <!-- 登陆状态切换 -->
  44. <view class="login-status u-f-ajc login-padding" @tap="changeStatus">
  45. {{status?'账号密码登陆':'验证码登陆'}}
  46. <view class="icon iconfont icon-jinru"></view>
  47. </view>
  48. <!-- 第三方登陆 -->
  49. <view class="other-login-title u-f-ajc login-padding">第三方登陆</view>
  50. <other-login></other-login>
  51. <!-- 协议 -->
  52. <view class="login-rule u-f-ajc login-padding login-font-color">
  53. 注册即代表您同意<view class="login-font-color">《XXXX协议》</view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import otherLogin from '@/components/home/other-login.vue';
  59. export default {
  60. components: {
  61. otherLogin
  62. },
  63. data() {
  64. return {
  65. status: false, // false代表账号密码登陆, true手机验证码登陆
  66. disabled: true,
  67. loading: false,
  68. username: '',
  69. password: '',
  70. phone: '',
  71. checknum: '',
  72. codetime: 0
  73. }
  74. },
  75. watch: {
  76. username(val) {
  77. this.OnBtnChange();
  78. },
  79. password(val) {
  80. this.OnBtnChange();
  81. },
  82. phone(val) {
  83. this.OnBtnChange();
  84. },
  85. checknum(val) {
  86. this.OnBtnChange();
  87. }
  88. },
  89. methods: {
  90. // 获取验证码
  91. getCheckNum() {
  92. if (this.codetime > 0) {
  93. return;
  94. }
  95. // 请求服务器,发送验证码
  96. // 发送成功,开启倒计时
  97. this.codetime = 10;
  98. let timer = setInterval(() => {
  99. this.codetime--;
  100. if (this.codetime < 1) {
  101. clearInterval(timer);
  102. this.codetime = 0;
  103. }
  104. }, 1000);
  105. },
  106. // 初始化表单
  107. initInput() {
  108. this.username = '';
  109. this.password = '';
  110. this.phone = '';
  111. this.checknum = '';
  112. },
  113. // 改变按钮状态
  114. OnBtnChange() {
  115. if ((this.username && this.password) || (this.phone && this.checknum)) {
  116. this.disabled = false;
  117. return;
  118. }
  119. this.disabled = true;
  120. },
  121. // 切换登陆状态
  122. changeStatus() {
  123. this.initInput();
  124. this.status = !this.status;
  125. },
  126. back() {
  127. console.log('返回上一步');
  128. },
  129. submit() {
  130. console.log('提交登陆');
  131. }
  132. }
  133. }
  134. </script>
  135. <style>
  136. @import url('@/common/form.css');
  137. .login-padding {
  138. padding: 20upx;
  139. }
  140. .login-font-color {
  141. color: #BBBBBB;
  142. }
  143. .loginhead {
  144. width: 100%;
  145. margin-top: -5upx;
  146. }
  147. .icon-guanbi {
  148. position: fixed;
  149. top: 60upx;
  150. left: 30upx;
  151. font-size: 40upx;
  152. font-weight: bold;
  153. color: #332F0A;
  154. z-index: 100;
  155. }
  156. .other-login-title {
  157. position: relative;
  158. }
  159. .other-login-title::before,
  160. .other-login-title::after {
  161. content: '';
  162. position: absolute;
  163. background: #CCCCCC;
  164. height: 1upx;
  165. width: 100upx;
  166. top: 50%;
  167. }
  168. .other-login-title::before {
  169. left: 25%;
  170. }
  171. .other-login-title::after {
  172. right: 25%;
  173. }
  174. .login-input-box {
  175. position: relative;
  176. }
  177. .login-input-box .forget-input {
  178. padding-right: 150upx;
  179. }
  180. .login-input-box .forget {
  181. position: absolute;
  182. right: 0;
  183. top: 0;
  184. height: 100%;
  185. width: 150upx;
  186. /* background: yellow; */
  187. z-index: 100;
  188. }
  189. .login-input-box .phone {
  190. position: absolute;
  191. left: 0;
  192. top: 0;
  193. height: 100%;
  194. width: 100upx;
  195. z-index: 100;
  196. font-weight: bold;
  197. }
  198. .login-input-box .phone-input {
  199. padding-left: 100upx;
  200. }
  201. .yanzhengma view {
  202. background: #EEEEEE;
  203. border-radius: 10upx;
  204. font-size: 25upx;
  205. width: 150upx;
  206. padding: 10upx 0;
  207. }
  208. </style>

结束