bindPhone.vue 6.9 KB

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