goLogin.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. telBind,
  57. sendCode
  58. } from "@/api/login";
  59. import cacheManager from "@/utils/cacheManager";
  60. import agreeContentDialog from '@/pages/login/agreeContentDialog.vue';
  61. import agreeDialog from '@/pages/login/agreeDialog.vue'
  62. import captchaVue from "@/components/captcha4/index.vue";
  63. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue"
  64. const emits = defineEmits(['success'])
  65. const dlRef = ref(null)
  66. const captcha = ref(null);
  67. const agreeType = ref(null);
  68. const agreeDialogRef = ref(null);
  69. const agreeContentDialogRef = ref(null);
  70. const isAgreed = ref(false);
  71. const config = ref({
  72. captchaId: "9d5837b0807b8de44da0de310a0b2813",
  73. });
  74. const loginData = reactive({
  75. phoneNumber: null,
  76. yzmNumber: null,
  77. clearYzmIcon: false,
  78. yzmStatus: 'login-btn-disabled',
  79. timeLeft: 60, // 初始倒计时时间(秒)
  80. intervalId: null, // 定时器ID
  81. isDisabled: false, // 按钮是否禁用
  82. buttonText: '获取验证码', // 按钮文本
  83. })
  84. const sliderData = reactive({})
  85. function showDl() {
  86. dlRef.value.open('bottom');
  87. }
  88. function closeDl() {
  89. loginData.phoneNumber = null;
  90. loginData.yzmNumber = null;
  91. loginData.clearYzmIcon = false;
  92. loginData.yzmStatus = 'login-btn-disabled';
  93. loginData.timeLeft = 60;
  94. loginData.intervalId = null;
  95. loginData.isDisabled = false;
  96. loginData.buttonText = '';
  97. config.value = null;
  98. isAgreed.value = false;
  99. dlRef.value.close();
  100. }
  101. // 返回
  102. function handleBack() {
  103. closeDl();
  104. }
  105. // 手机号校验规则
  106. const validatePhoneNumber = (value) => {
  107. const phoneRegex = /^1[3-9]\d{9}$/;
  108. if (phoneRegex.test(value)) {
  109. loginData.telStatus = 'login-btn-normal';
  110. } else {
  111. loginData.telStatus = 'login-btn-disabled';
  112. }
  113. }
  114. const validatePhone = (value) => {
  115. const phoneRegex = /^1[3-9]\d{9}$/;
  116. return phoneRegex.test(value)
  117. }
  118. function clearTelInput(event) {
  119. if (event.detail.value.length > 0) {
  120. loginData.clearTelIcon = true;
  121. validatePhoneNumber(event.detail.value);
  122. } else {
  123. loginData.clearTelIcon = false;
  124. }
  125. }
  126. function clearTel() {
  127. loginData.phoneNumber = '';
  128. loginData.telStatus = 'login-btn-disabled';
  129. loginData.clearTelIcon = false;
  130. }
  131. function clearYzmInput(event) {
  132. if (event.detail.value.length > 0) {
  133. loginData.clearYzmIcon = true;
  134. loginData.yzmStatus = 'login-btn-normal';
  135. } else {
  136. loginData.clearYzmIcon = false;
  137. loginData.yzmStatus = 'login-btn-disabled';
  138. }
  139. }
  140. function clearYzm() {
  141. this.loginData.yzmNumber = '';
  142. this.loginData.yzmStatus = 'login-btn-disabled';
  143. this.loginData.clearYzmIcon = false;
  144. }
  145. // 登录
  146. function handleLogin() {
  147. // 用户名
  148. if (!loginData.phoneNumber) {
  149. toast('请输入手机号')
  150. return;
  151. }
  152. // 正确手机号
  153. if (!validatePhone(loginData.phoneNumber)) {
  154. toast('请输入正确手机号')
  155. return;
  156. }
  157. // 验证码
  158. if (!loginData.yzmNumber) {
  159. toast('请输入验证码')
  160. return;
  161. }
  162. // 协议
  163. if (!isAgreed.value) {
  164. agreeDialogRef.value.handleShow()
  165. return;
  166. }
  167. let req = {
  168. tel: loginData.phoneNumber,
  169. code: loginData.yzmNumber,
  170. }
  171. telBind(req).then(res => {
  172. if (res.code == 0) {
  173. // 更新用户信息
  174. cacheManager.set('auth', res.data)
  175. // 返回重新支付
  176. handleBack();
  177. emits('success', res.data)
  178. }
  179. })
  180. }
  181. // 更新协议
  182. function handleUpdateAgree() {
  183. isAgreed.value = !isAgreed.value
  184. }
  185. // 协议跳转
  186. function agreeBtn(data) {
  187. agreeType.value = data;
  188. agreeContentDialogRef.value.handleShow();
  189. }
  190. function confirmBtn() {
  191. isAgreed.value = true;
  192. handleLogin();
  193. }
  194. // 图形验证码
  195. function showCaptcha() {
  196. captcha.value.showCaptcha();
  197. }
  198. function startCountdown() {
  199. if (!loginData.phoneNumber) {
  200. toast('请输入手机号')
  201. return;
  202. }
  203. if (loginData.buttonText === '重新发送' ||loginData.buttonText === '获取验证码') {
  204. showCaptcha();
  205. return;
  206. }
  207. loginData.isDisabled = true;
  208. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  209. // 清除之前的定时器(如果有)
  210. if (loginData.intervalId) {
  211. clearInterval(loginData.intervalId);
  212. loginData.intervalId = null;
  213. }
  214. // 设置新的定时器
  215. loginData.intervalId = setInterval(() => {
  216. loginData.timeLeft--;
  217. if (loginData.timeLeft <= 0) {
  218. clearInterval(loginData.intervalId);
  219. loginData.intervalId = null;
  220. loginData.timeLeft = 60; // 重置倒计时
  221. loginData.isDisabled = false;
  222. loginData.buttonText = '重新发送';
  223. } else {
  224. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  225. }
  226. }, 1000);
  227. }
  228. function getYzmBtn() {
  229. let req = {
  230. phone: loginData.phoneNumber,
  231. captchaOutput: sliderData.captcha_output,
  232. genTime: sliderData.gen_time,
  233. lotNumber: sliderData.lot_number,
  234. passToken: sliderData.pass_token,
  235. }
  236. sendCode(req).then(res => {})
  237. }
  238. function captchaSuccess(result) {
  239. startCountdown();
  240. Object.assign(sliderData, result)
  241. getYzmBtn();
  242. }
  243. function captchaError(e) {
  244. toast(JSON.stringify(e))
  245. }
  246. function captchaFail() {
  247. toast('验证失败!')
  248. }
  249. function captchaReady() {}
  250. function captchaClose() {}
  251. defineExpose({
  252. showDl
  253. })
  254. </script>
  255. <style>
  256. </style>