bindPhone.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 @click="handleBack" 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. <ezyActiveVue v-if="skipBtnFlag" class="ezy-btn-active login-btn yzm-btn" @aclick="skipFun" :class="loginData.yzmStatus">跳过
  25. </ezyActiveVue>
  26. <ezyActiveVue class="ezy-btn-active login-btn yzm-btn" @aclick="bangdingFun" :class="loginData.yzmStatus">绑定
  27. </ezyActiveVue>
  28. </uni-popup>
  29. <!-- 图形验证码 -->
  30. <captchaVue ref="captcha" :config="config" @captchaSuccess="captchaSuccess" @captchaError="captchaError"
  31. @captchaFail="captchaFail" @captchaReady="captchaReady" @captchaClose="captchaClose"></captchaVue>
  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. })
  78. const sliderData = reactive({})
  79. function showDl() {
  80. dlRef.value.open('bottom');
  81. }
  82. function closeDl() {
  83. loginData.phoneNumber = null;
  84. loginData.yzmNumber = null;
  85. loginData.clearYzmIcon = false;
  86. loginData.yzmStatus = 'login-btn-disabled';
  87. loginData.timeLeft = 60;
  88. if (loginData.intervalId) {
  89. clearInterval(loginData.intervalId);
  90. loginData.intervalId = null;
  91. }
  92. loginData.intervalId = null;
  93. loginData.isDisabled = false;
  94. loginData.buttonText = '获取验证码';
  95. isAgreed.value = false;
  96. dlRef.value.close();
  97. }
  98. // 返回
  99. function handleBack() {
  100. closeDl();
  101. }
  102. function skipFun() {
  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. loginData.yzmNumber = '';
  142. loginData.yzmStatus = 'login-btn-disabled';
  143. loginData.clearYzmIcon = false;
  144. }
  145. // 登录
  146. function bangdingFun() {
  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. let req = {
  163. tel: loginData.phoneNumber,
  164. code: loginData.yzmNumber,
  165. }
  166. console.log('req', req);
  167. telBind(req).then(res => {
  168. if (res.code == 0) {
  169. // 更新用户信息
  170. // cacheManager.set('auth', res.data)
  171. // 返回重新支付
  172. handleBack();
  173. emits('success', res.data,loginData.phoneNumber)
  174. }
  175. })
  176. }
  177. // 图形验证码
  178. function showCaptcha() {
  179. captcha.value.showCaptcha();
  180. }
  181. function startCountdown() {
  182. console.log('config', config.value);
  183. if (!loginData.phoneNumber) {
  184. toast('请输入手机号')
  185. return;
  186. }
  187. if (loginData.buttonText === '重新发送' || loginData.buttonText === '获取验证码') {
  188. showCaptcha();
  189. return;
  190. }
  191. runCountdown()
  192. }
  193. function runCountdown() {
  194. loginData.isDisabled = true;
  195. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  196. // 清除之前的定时器(如果有)
  197. if (loginData.intervalId) {
  198. clearInterval(loginData.intervalId);
  199. loginData.intervalId = null;
  200. }
  201. // 设置新的定时器
  202. loginData.intervalId = setInterval(() => {
  203. loginData.timeLeft--;
  204. if (loginData.timeLeft <= 0) {
  205. clearInterval(loginData.intervalId);
  206. loginData.intervalId = null;
  207. loginData.timeLeft = 60; // 重置倒计时
  208. loginData.isDisabled = false;
  209. loginData.buttonText = '重新发送';
  210. } else {
  211. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  212. }
  213. }, 1000);
  214. }
  215. function getYzmBtn() {
  216. let req = {
  217. phone: loginData.phoneNumber,
  218. captchaOutput: sliderData.captcha_output,
  219. genTime: sliderData.gen_time,
  220. lotNumber: sliderData.lot_number,
  221. passToken: sliderData.pass_token,
  222. }
  223. sendCode(req).then(res => {})
  224. }
  225. function captchaSuccess(result) {
  226. Object.assign(sliderData, result)
  227. getYzmBtn();
  228. runCountdown()
  229. }
  230. function captchaError(e) {
  231. toast(JSON.stringify(e))
  232. }
  233. function captchaFail() {
  234. toast('验证失败!')
  235. }
  236. function captchaReady() {}
  237. function captchaClose() {}
  238. defineExpose({
  239. showDl
  240. })
  241. </script>
  242. <style>
  243. </style>