goLogin.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <uni-popup ref="dlRef" :animation="true" :is-mask-click="false" mask-background-color="rgba(255, 255, 255, 0.6);">
  3. <!-- 返回 -->
  4. <view class="icon-title-navBar-box333">
  5. <view @click="handleBack" class="nav-bar-icon"></view>
  6. <text class="nav-bar-title">登录</text>
  7. </view>
  8. <!-- 用户名 -->
  9. <view class="phone-input-box">
  10. <view class="phone-prefix">+86</view>
  11. <input class="phone-input" type="text" v-model="loginData.phoneNumber" placeholder="请输入手机号" maxlength="11"
  12. @input="clearTelInput" />
  13. <view class="close-btn" v-if="loginData.clearTelIcon" @click="clearTel"></view>
  14. </view>
  15. <!-- 验证码 -->
  16. <view class="phone-input-box">
  17. <input class="phone-input" type="text" v-model="loginData.yzmNumber" placeholder="请输入验证码" maxlength="4"
  18. @input="clearYzmInput" />
  19. <view class="close-btn" v-if="loginData.clearYzmIcon" @click="clearYzm"></view>
  20. <text class="cxfs-btn" @click="startCountdown"
  21. :class="{ 'cxfs-btn-disabled': loginData.isDisabled}">{{loginData.buttonText}}</text>
  22. </view>
  23. <!-- 隐私协议 -->
  24. <view class="agreement-checkbox-box">
  25. <checkbox-group @change="handleUpdateAgree">
  26. <checkbox class="agreement-checkbox-input" color="#FFFFFF" value="agree" :checked="isAgreed" />
  27. </checkbox-group>
  28. <view class="agreement-text-box">
  29. 我已阅读并同意
  30. <view class="agreement-text" @click="agreeBtn('yhxy')">《用户协议》</view>
  31. <view @click="agreeBtn('ystk')" class="agreement-text">《隐私政策》</view>
  32. </view>
  33. </view>
  34. <!-- 登录按钮 -->
  35. <ezyActiveVue class="ezy-btn-active login-btn yzm-btn" @aclick="handleLogin" :class="loginData.yzmStatus">登录
  36. </ezyActiveVue>
  37. </uni-popup>
  38. <!-- 政策协议 -->
  39. <agree-content-dialog ref="agreeContentDialogRef" :agreeType="agreeType"></agree-content-dialog>
  40. <!-- 图形验证码 -->
  41. <captchaVue ref="captcha" :config="config" @captchaSuccess="captchaSuccess" @captchaError="captchaError"
  42. @captchaFail="captchaFail" @captchaReady="captchaReady" @captchaClose="captchaClose"></captchaVue>
  43. <!-- 确认协议 -->
  44. <agree-dialog ref="agreeDialogRef" @confirm-btn="confirmBtn"></agree-dialog>
  45. </template>
  46. <script setup>
  47. import {
  48. ref,
  49. reactive
  50. } from "vue"
  51. import {
  52. toast
  53. } from "@/utils/common";
  54. import {
  55. login,
  56. sendCode
  57. } from "@/api/login";
  58. import cacheManager from "@/utils/cacheManager";
  59. import agreeContentDialog from '@/pages/login/agreeContentDialog.vue';
  60. import agreeDialog from '@/pages/login/agreeDialog.vue'
  61. import captchaVue from "@/components/captcha4/index.vue";
  62. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue"
  63. const emits = defineEmits(['success'])
  64. const dlRef = ref(null)
  65. const captcha = ref(null);
  66. const agreeType = ref(null);
  67. const agreeDialogRef = ref(null);
  68. const agreeContentDialogRef = ref(null);
  69. const isAgreed = ref(false);
  70. const config = ref({
  71. captchaId: "9d5837b0807b8de44da0de310a0b2813",
  72. });
  73. const loginData = reactive({
  74. phoneNumber: null,
  75. yzmNumber: null,
  76. clearYzmIcon: false,
  77. yzmStatus: 'login-btn-disabled',
  78. timeLeft: 60, // 初始倒计时时间(秒)
  79. intervalId: null, // 定时器ID
  80. isDisabled: false, // 按钮是否禁用
  81. buttonText: '获取验证码', // 按钮文本
  82. })
  83. const sliderData = reactive({})
  84. function showDl() {
  85. dlRef.value.open('bottom');
  86. }
  87. function closeDl() {
  88. loginData.phoneNumber = null;
  89. loginData.yzmNumber = null;
  90. loginData.clearYzmIcon = false;
  91. loginData.yzmStatus = 'login-btn-disabled';
  92. loginData.timeLeft = 60;
  93. loginData.intervalId = null;
  94. loginData.isDisabled = false;
  95. loginData.buttonText = '';
  96. config.value = null;
  97. isAgreed.value = false;
  98. dlRef.value.close();
  99. }
  100. // 返回
  101. function handleBack() {
  102. closeDl();
  103. }
  104. // 手机号校验规则
  105. const validatePhoneNumber = (value) => {
  106. const phoneRegex = /^1[3-9]\d{9}$/;
  107. if (phoneRegex.test(value)) {
  108. loginData.telStatus = 'login-btn-normal';
  109. } else {
  110. loginData.telStatus = 'login-btn-disabled';
  111. }
  112. }
  113. const validatePhone = (value) => {
  114. const phoneRegex = /^1[3-9]\d{9}$/;
  115. return phoneRegex.test(value)
  116. }
  117. function clearTelInput(event) {
  118. if (event.detail.value.length > 0) {
  119. loginData.clearTelIcon = true;
  120. validatePhoneNumber(event.detail.value);
  121. } else {
  122. loginData.clearTelIcon = false;
  123. }
  124. }
  125. function clearTel() {
  126. loginData.phoneNumber = '';
  127. loginData.telStatus = 'login-btn-disabled';
  128. loginData.clearTelIcon = false;
  129. }
  130. function clearYzmInput(event) {
  131. if (event.detail.value.length > 0) {
  132. loginData.clearYzmIcon = true;
  133. loginData.yzmStatus = 'login-btn-normal';
  134. } else {
  135. loginData.clearYzmIcon = false;
  136. loginData.yzmStatus = 'login-btn-disabled';
  137. }
  138. }
  139. function clearYzm() {
  140. this.loginData.yzmNumber = '';
  141. this.loginData.yzmStatus = 'login-btn-disabled';
  142. this.loginData.clearYzmIcon = false;
  143. }
  144. // 登录
  145. function handleLogin() {
  146. // 用户名
  147. if (!loginData.phoneNumber) {
  148. toast('请输入手机号')
  149. return;
  150. }
  151. // 正确手机号
  152. if (!validatePhone(loginData.phoneNumber)) {
  153. toast('请输入正确手机号')
  154. return;
  155. }
  156. // 验证码
  157. if (!loginData.yzmNumber) {
  158. toast('请输入验证码')
  159. return;
  160. }
  161. // 协议
  162. if (!isAgreed.value) {
  163. agreeDialogRef.value.handleShow()
  164. return;
  165. }
  166. let req = {
  167. tel: loginData.phoneNumber,
  168. code: loginData.yzmNumber,
  169. }
  170. login(req).then(res => {
  171. if (res.code == 0) {
  172. // 更新用户信息
  173. cacheManager.set('auth', res.data)
  174. // 返回重新支付
  175. handleBack();
  176. emits('success', res.data)
  177. }
  178. })
  179. }
  180. // 更新协议
  181. function handleUpdateAgree() {
  182. isAgreed.value = !isAgreed.value
  183. }
  184. // 协议跳转
  185. function agreeBtn(data) {
  186. agreeType.value = data;
  187. agreeContentDialogRef.value.handleShow();
  188. }
  189. function confirmBtn() {
  190. isAgreed.value = true;
  191. handleLogin();
  192. }
  193. // 图形验证码
  194. function showCaptcha() {
  195. captcha.value.showCaptcha();
  196. }
  197. function startCountdown() {
  198. if (!loginData.phoneNumber) {
  199. toast('请输入手机号')
  200. return;
  201. }
  202. if (loginData.buttonText === '重新发送' ||loginData.buttonText === '获取验证码') {
  203. showCaptcha();
  204. return;
  205. }
  206. loginData.isDisabled = true;
  207. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  208. // 清除之前的定时器(如果有)
  209. if (loginData.intervalId) {
  210. clearInterval(loginData.intervalId);
  211. loginData.intervalId = null;
  212. }
  213. // 设置新的定时器
  214. loginData.intervalId = setInterval(() => {
  215. loginData.timeLeft--;
  216. if (loginData.timeLeft <= 0) {
  217. clearInterval(loginData.intervalId);
  218. loginData.intervalId = null;
  219. loginData.timeLeft = 60; // 重置倒计时
  220. loginData.isDisabled = false;
  221. loginData.buttonText = '重新发送';
  222. } else {
  223. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  224. }
  225. }, 1000);
  226. }
  227. function getYzmBtn() {
  228. let req = {
  229. phone: loginData.phoneNumber,
  230. captchaOutput: sliderData.captcha_output,
  231. genTime: sliderData.gen_time,
  232. lotNumber: sliderData.lot_number,
  233. passToken: sliderData.pass_token,
  234. }
  235. sendCode(req).then(res => {})
  236. }
  237. function captchaSuccess(result) {
  238. startCountdown();
  239. Object.assign(sliderData, result)
  240. getYzmBtn();
  241. }
  242. function captchaError(e) {
  243. toast(JSON.stringify(e))
  244. }
  245. function captchaFail() {
  246. toast('验证失败!')
  247. }
  248. function captchaReady() {}
  249. function captchaClose() {}
  250. defineExpose({
  251. showDl
  252. })
  253. </script>
  254. <style>
  255. </style>