login.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="ezy-login-page">
  3. <view @click="goIndex" class="ezy-nav-bar-icon yzm-nav-bar-icon"></view>
  4. <icon class="login-img"></icon>
  5. <view class="ezy-login-wrap">
  6. <view class="login-body-box">
  7. <view class="yzm-show">验证码已发送至:{{loginData.phoneNumber}}</view>
  8. <view class="phone-input-box">
  9. <input class="phone-input" type="text" v-model="loginData.yzmNumber" placeholder="请输入验证码"
  10. maxlength="4" @input="clearYzmInput" />
  11. <view class="close-btn" v-if="loginData.clearYzmIcon" @click="clearYzm"></view>
  12. </view>
  13. <view class="login-btn yzm-btn" @click="loginBtn" :class="loginData.yzmStatus">登录</view>
  14. <text class="cxfs-btn" @click="startCountdown"
  15. :class="{ 'cxfs-btn-disabled': loginData.isDisabled}">{{loginData.buttonText}}</text>
  16. <text class="login-text" @click="goIndex">无法收到验证码</text>
  17. <text class="login-text">客服电话:<text>4001750778</text></text>
  18. </view>
  19. </view>
  20. <captcha ref="captcha" :config="config" @captchaSuccess="captchaSuccess" @captchaError="captchaError"
  21. @captchaFail="captchaFail" @captchaReady="captchaReady" @captchaClose="captchaClose"></captcha>
  22. </view>
  23. </template>
  24. <script>
  25. import {
  26. ref,
  27. reactive
  28. } from "vue"
  29. import {
  30. login,
  31. sendCode
  32. } from "@/api/login.js"
  33. import cacheManager from "@/utils/cacheManager.js";
  34. import captcha from "../../components/captcha4/index.vue";
  35. import {
  36. toast
  37. } from "../../utils/common";
  38. export default {
  39. data() {
  40. return {
  41. loginData: {
  42. phoneNumber: null,
  43. yzmNumber: null,
  44. clearYzmIcon: false,
  45. yzmStatus: 'login-btn-disabled',
  46. timeLeft: 60, // 初始倒计时时间(秒)
  47. intervalId: null, // 定时器ID
  48. isDisabled: false, // 按钮是否禁用
  49. buttonText: '', // 按钮文本
  50. },
  51. sliderData: {},
  52. sliderDialogRef: null,
  53. config: {
  54. captchaId: "9d5837b0807b8de44da0de310a0b2813",
  55. },
  56. }
  57. },
  58. components: {
  59. captcha
  60. },
  61. onLoad(options) {
  62. this.loginInit(options);
  63. },
  64. onReady() {},
  65. methods: {
  66. captchaSuccess(result) { // app端的回调
  67. console.log(result)
  68. this.startCountdown();
  69. this.sliderData = result;
  70. this.getYzmBtn();
  71. },
  72. captchaError(e) {
  73. // app端的回调、
  74. console.log('error',JSON.stringify(e));
  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. uni.redirectTo({
  113. url: `/pages/chanpinXuanze/index`
  114. })
  115. }
  116. })
  117. },
  118. clearYzmInput(event) {
  119. if (event.detail.value.length > 0) {
  120. this.loginData.clearYzmIcon = true;
  121. this.loginData.yzmStatus = 'login-btn-normal';
  122. } else {
  123. this.loginData.clearYzmIcon = false;
  124. this.loginData.yzmStatus = 'login-btn-disabled';
  125. }
  126. },
  127. clearYzm() {
  128. this.loginData.yzmNumber = '';
  129. this.loginData.yzmStatus = 'login-btn-disabled';
  130. this.loginData.clearYzmIcon = false;
  131. },
  132. startCountdown() {
  133. if (this.loginData.buttonText === '重新发送') {
  134. this.$refs.captcha.showCaptcha();
  135. }
  136. this.loginData.isDisabled = true;
  137. this.loginData.buttonText = `重新发送(${this.loginData.timeLeft}S)`;
  138. // 清除之前的定时器(如果有)
  139. if (this.loginData.intervalId) {
  140. clearInterval(this.loginData.intervalId);
  141. }
  142. // 设置新的定时器
  143. this.loginData.intervalId = setInterval(() => {
  144. this.loginData.timeLeft--;
  145. if (this.loginData.timeLeft <= 0) {
  146. clearInterval(this.loginData.intervalId);
  147. this.loginData.timeLeft = 60; // 重置倒计时
  148. this.loginData.isDisabled = false;
  149. this.loginData.buttonText = '重新发送';
  150. } else {
  151. this.loginData.buttonText = `重新发送(${this.loginData.timeLeft}S)`;
  152. }
  153. }, 1000);
  154. },
  155. goIndex() {
  156. uni.redirectTo({
  157. url: `/pages/login/index`
  158. })
  159. }
  160. },
  161. mounted() {
  162. this.$refs.captcha.showCaptcha();
  163. },
  164. }
  165. </script>