bindPhone.vue 6.7 KB

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