login.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view class="ezy-login-page">
  3. <view @click="goIndex" class="ezy-nav-bar-icon"></view>
  4. <view class="ezy-login-wrap">
  5. <icon class="login-e-img"></icon>
  6. <view class="login-body-box">
  7. <view class="login-title-img"></view>
  8. <view class="yzm-show">验证码已发送至:{{loginData.phoneNumber}}</view>
  9. <view class="phone-input-box">
  10. <input class="phone-input" type="text" v-model="loginData.yzmNumber" placeholder="请输入验证码"
  11. maxlength="6" @input="clearYzmInput" />
  12. <view class="close-btn" v-if="loginData.clearYzmIcon" @click="clearYzm"></view>
  13. </view>
  14. <view class="login-btn yzm-btn" @click="loginBtn" :class="loginData.yzmStatus"></view>
  15. <text class="cxfs-btn" @click="startCountdown"
  16. :class="{ 'cxfs-btn-disabled': loginData.isDisabled}">{{loginData.buttonText}}</text>
  17. <text class="login-text" @click="goIndex">无法收到验证码</text>
  18. <text class="login-text">客服电话:400-052-2130</text>
  19. </view>
  20. </view>
  21. <captcha ref="captcha" :config="config" @captchaSuccess="captchaSuccess" @captchaError="captchaError"
  22. @captchaFail="captchaFail" @captchaReady="captchaReady" @captchaClose="captchaClose"></captcha>
  23. </view>
  24. </template>
  25. <script>
  26. import {
  27. ref,
  28. reactive
  29. } from "vue"
  30. import {
  31. login,
  32. sendCode
  33. } from "@/api/login.js"
  34. import cacheManager from "@/utils/cacheManager.js";
  35. import captcha from "../../components/captcha4/index.vue";
  36. import {
  37. toast
  38. } from "../../utils/common";
  39. export default {
  40. data() {
  41. return {
  42. loginData: {
  43. phoneNumber: null,
  44. yzmNumber: null,
  45. clearYzmIcon: false,
  46. yzmStatus: 'login-btn-disabled',
  47. timeLeft: 60, // 初始倒计时时间(秒)
  48. intervalId: null, // 定时器ID
  49. isDisabled: false, // 按钮是否禁用
  50. buttonText: '', // 按钮文本
  51. },
  52. sliderData: {},
  53. sliderDialogRef: null,
  54. config: {
  55. captchaId: "16e4de331cee10dfe36bcf55810c6041",
  56. },
  57. }
  58. },
  59. components: {
  60. captcha
  61. },
  62. onLoad(options) {
  63. this.loginInit(options);
  64. },
  65. onReady() {},
  66. methods: {
  67. captchaSuccess(result) { // app端的回调
  68. console.log(result)
  69. this.startCountdown();
  70. this.sliderData = result;
  71. this.getYzmBtn();
  72. },
  73. captchaError(e) {
  74. // app端的回调
  75. toast(JSON.stringify(e))
  76. },
  77. captchaReady() {
  78. // app端的回调
  79. },
  80. captchaFail() {
  81. // app端的回调
  82. toast('验证失败!')
  83. },
  84. captchaClose() {
  85. uni.redirectTo({
  86. url: `/pages/login/index`
  87. })
  88. },
  89. loginInit(options) {
  90. this.loginData.phoneNumber = options.telNum;
  91. },
  92. getYzmBtn() {
  93. let req = {
  94. phone: this.loginData.phoneNumber,
  95. captchaOutput: this.sliderData.captcha_output,
  96. genTime: this.sliderData.gen_time,
  97. lotNumber: this.sliderData.lot_number,
  98. passToken: this.sliderData.pass_token,
  99. }
  100. console.log('req', req);
  101. sendCode(req).then(res => {
  102. })
  103. },
  104. loginBtn() {
  105. let req = {
  106. tel: this.loginData.phoneNumber,
  107. code: this.loginData.yzmNumber,
  108. }
  109. login(req).then(res => {
  110. if (res.code == 0) {
  111. cacheManager.set('auth', res.data)
  112. if (res.data.cardId == 0) {
  113. uni.redirectTo({
  114. url: `/pages/selectGradesTerms/index`
  115. })
  116. } else {
  117. uni.redirectTo({
  118. url: `/pages/study/index`
  119. })
  120. }
  121. }
  122. })
  123. },
  124. clearYzmInput(event) {
  125. if (event.detail.value.length > 0) {
  126. this.loginData.clearYzmIcon = true;
  127. this.loginData.yzmStatus = 'login-btn-normal';
  128. } else {
  129. this.loginData.clearYzmIcon = false;
  130. this.loginData.yzmStatus = 'login-btn-disabled';
  131. }
  132. },
  133. clearYzm() {
  134. this.loginData.yzmNumber = '';
  135. this.loginData.yzmStatus = 'login-btn-disabled';
  136. this.loginData.clearYzmIcon = false;
  137. },
  138. startCountdown() {
  139. if (this.loginData.buttonText === '重新发送') {
  140. this.$refs.captcha.showCaptcha();
  141. }
  142. this.loginData.isDisabled = true;
  143. this.loginData.buttonText = `重新发送(${this.loginData.timeLeft}S)`;
  144. // 清除之前的定时器(如果有)
  145. if (this.loginData.intervalId) {
  146. clearInterval(this.loginData.intervalId);
  147. }
  148. // 设置新的定时器
  149. this.loginData.intervalId = setInterval(() => {
  150. this.loginData.timeLeft--;
  151. if (this.loginData.timeLeft <= 0) {
  152. clearInterval(this.loginData.intervalId);
  153. this.loginData.timeLeft = 60; // 重置倒计时
  154. this.loginData.isDisabled = false;
  155. this.loginData.buttonText = '重新发送';
  156. } else {
  157. this.loginData.buttonText = `重新发送(${this.loginData.timeLeft}S)`;
  158. }
  159. }, 1000);
  160. },
  161. goIndex() {
  162. uni.redirectTo({
  163. url: `/pages/login/index`
  164. })
  165. }
  166. },
  167. mounted() {
  168. this.$refs.captcha.showCaptcha();
  169. },
  170. }
  171. </script>