bindPhone.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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="请输入手机号"
  12. maxlength="11" @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="请输入验证码"
  19. maxlength="4" @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. 确认绑定
  27. </ezyActiveVue>
  28. </view>
  29. <!-- 图形验证码 -->
  30. <captchaVue ref="captcha" :config="config" @captchaSuccess="captchaSuccess" @captchaError="captchaError"
  31. @captchaFail="captchaFail" @captchaReady="captchaReady" @captchaClose="captchaClose"></captchaVue>
  32. </view>
  33. </template>
  34. <script setup>
  35. import {
  36. ref,
  37. reactive
  38. } from "vue"
  39. import {
  40. toast
  41. } from "@/utils/common";
  42. import {
  43. onLoad,
  44. onShow
  45. } from '@dcloudio/uni-app';
  46. import {
  47. login,
  48. telBind,
  49. sendCode
  50. } from "@/api/login";
  51. import cacheManager from "@/utils/cacheManager";
  52. import agreeContentDialog from '@/pages/login/agreeContentDialog.vue';
  53. import agreeDialog from '@/pages/login/agreeDialog.vue'
  54. import captchaVue from "@/components/captcha4/index.vue";
  55. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue"
  56. const emits = defineEmits(['success'])
  57. const dlRef = ref(null)
  58. const captcha = ref(null);
  59. const agreeType = ref(null);
  60. const agreeDialogRef = ref(null);
  61. const agreeContentDialogRef = ref(null);
  62. const isAgreed = ref(false);
  63. const from = ref('');
  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. openFangshi: {
  84. type: String,
  85. default: 'bottom'
  86. },
  87. })
  88. const sliderData = reactive({})
  89. function showDl() {
  90. dlRef.value.open(props.openFangshi);
  91. }
  92. onLoad((options) => {
  93. console.log('options', options);
  94. from.value = options.from
  95. })
  96. function closeDl() {
  97. loginData.phoneNumber = null;
  98. loginData.yzmNumber = null;
  99. loginData.clearYzmIcon = false;
  100. loginData.yzmStatus = 'login-btn-disabled';
  101. loginData.timeLeft = 60;
  102. if (loginData.intervalId) {
  103. clearInterval(loginData.intervalId);
  104. loginData.intervalId = null;
  105. }
  106. loginData.intervalId = null;
  107. loginData.isDisabled = false;
  108. loginData.buttonText = '获取验证码';
  109. isAgreed.value = false;
  110. if (from.value = 'my') {
  111. uni.switchTab({
  112. url: "/pages/chanpinMy/my"
  113. })
  114. } else if (from.value = 'order') {
  115. uni.navigateTo({
  116. url: '/pages/chanpinMy/order'
  117. })
  118. } else {
  119. uni.switchTab({
  120. url: "/pages/chanpinMy/my"
  121. })
  122. }
  123. }
  124. // 返回
  125. function handleBack() {
  126. closeDl();
  127. }
  128. // 手机号校验规则
  129. const validatePhoneNumber = (value) => {
  130. const phoneRegex = /^1[3-9]\d{9}$/;
  131. if (phoneRegex.test(value)) {
  132. loginData.telStatus = 'login-btn-normal';
  133. } else {
  134. loginData.telStatus = 'login-btn-disabled';
  135. }
  136. }
  137. const validatePhone = (value) => {
  138. const phoneRegex = /^1[3-9]\d{9}$/;
  139. return phoneRegex.test(value)
  140. }
  141. function clearTelInput(event) {
  142. if (event.detail.value.length > 0) {
  143. loginData.clearTelIcon = true;
  144. validatePhoneNumber(event.detail.value);
  145. } else {
  146. loginData.clearTelIcon = false;
  147. }
  148. }
  149. function clearTel() {
  150. loginData.phoneNumber = '';
  151. loginData.telStatus = 'login-btn-disabled';
  152. loginData.clearTelIcon = false;
  153. }
  154. function clearYzmInput(event) {
  155. if (event.detail.value.length > 0) {
  156. loginData.clearYzmIcon = true;
  157. loginData.yzmStatus = 'login-btn-normal';
  158. } else {
  159. loginData.clearYzmIcon = false;
  160. loginData.yzmStatus = 'login-btn-disabled';
  161. }
  162. }
  163. function clearYzm() {
  164. loginData.yzmNumber = '';
  165. loginData.yzmStatus = 'login-btn-disabled';
  166. loginData.clearYzmIcon = false;
  167. }
  168. // 登录
  169. function bangdingFun() {
  170. // 用户名
  171. if (!loginData.phoneNumber) {
  172. toast('请输入手机号')
  173. return;
  174. }
  175. // 正确手机号
  176. if (!validatePhone(loginData.phoneNumber)) {
  177. toast('请输入正确手机号')
  178. return;
  179. }
  180. // 验证码
  181. if (!loginData.yzmNumber) {
  182. toast('请输入验证码')
  183. return;
  184. }
  185. let req = {
  186. tel: loginData.phoneNumber,
  187. code: loginData.yzmNumber,
  188. }
  189. console.log('req', req);
  190. telBind(req).then(res => {
  191. if (res.code == 0) {
  192. // 更新用户信息
  193. // cacheManager.set('auth', res.data)
  194. // 返回重新支付
  195. handleBack();
  196. toast("绑定成功")
  197. }
  198. })
  199. }
  200. // 图形验证码
  201. function showCaptcha() {
  202. captcha.value.showCaptcha();
  203. }
  204. function startCountdown() {
  205. console.log('config', config.value);
  206. if (!loginData.phoneNumber) {
  207. toast('请输入手机号')
  208. return;
  209. }
  210. if (loginData.buttonText === '重新发送' || loginData.buttonText === '获取验证码') {
  211. showCaptcha();
  212. return;
  213. }
  214. runCountdown()
  215. }
  216. function runCountdown() {
  217. loginData.isDisabled = true;
  218. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  219. // 清除之前的定时器(如果有)
  220. if (loginData.intervalId) {
  221. clearInterval(loginData.intervalId);
  222. loginData.intervalId = null;
  223. }
  224. // 设置新的定时器
  225. loginData.intervalId = setInterval(() => {
  226. loginData.timeLeft--;
  227. if (loginData.timeLeft <= 0) {
  228. clearInterval(loginData.intervalId);
  229. loginData.intervalId = null;
  230. loginData.timeLeft = 60; // 重置倒计时
  231. loginData.isDisabled = false;
  232. loginData.buttonText = '重新发送';
  233. } else {
  234. loginData.buttonText = `重新发送(${loginData.timeLeft}S)`;
  235. }
  236. }, 1000);
  237. }
  238. function getYzmBtn() {
  239. let req = {
  240. phone: loginData.phoneNumber,
  241. captchaOutput: sliderData.captcha_output,
  242. genTime: sliderData.gen_time,
  243. lotNumber: sliderData.lot_number,
  244. passToken: sliderData.pass_token,
  245. }
  246. sendCode(req).then(res => {})
  247. }
  248. function captchaSuccess(result) {
  249. Object.assign(sliderData, result)
  250. getYzmBtn();
  251. runCountdown()
  252. }
  253. function captchaError(e) {
  254. toast(JSON.stringify(e))
  255. }
  256. function captchaFail() {
  257. toast('验证失败!')
  258. }
  259. function captchaReady() {}
  260. function captchaClose() {}
  261. </script>
  262. <style>
  263. </style>