telDialog.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <view class="my-tel-dialog">
  3. <view class="my-tel-content">
  4. <view class="tel-close" @click="telClose(AWSC)"></view>
  5. <view class="tel-row">
  6. <view class="my-tel-title">绑定手机号</view>
  7. <view class="my-input-box">
  8. <input class="my-input" type="text" v-model="bindObj.telNumber" placeholder="请输入手机号" maxlength="11"
  9. @input="changeTelInput" />
  10. <view class="close-btn" v-if="bindObj.clearTelIcon" @click="clearTel"></view>
  11. </view>
  12. <captcha ref="captcha" :config="config" @captchaSuccess="captchaSuccess" @captchaError="captchaError"
  13. @captchaFail="captchaFail" @captchaReady="captchaReady" @captchaClose="captchaClose"></captcha>
  14. <view class="get-yzm-btn" @click="getYzmBtn" :class="{ 'get-yzm-disabled': bindObj.isDisabled}">
  15. {{bindObj.buttonText}}</view>
  16. </view>
  17. <view class="yzm-row">
  18. <view class="yzm-tip" v-if="bindObj.getYzmFlag">验证码已发送至:{{bindObj.telNumber}}</view>
  19. <view class="my-input-box">
  20. <input class="my-input" type="text" v-model="bindObj.yzmNumber" placeholder="请输入验证码" maxlength="6"
  21. @input="changeYzmInput" />
  22. <view class="close-btn" v-if="bindObj.clearYzmIcon" @click="clearYzm"></view>
  23. </view>
  24. <view @click="bindBtn" class="my-bind-btn">绑定</view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import cacheManager from '@/utils/cacheManager.js';
  31. import captcha from "../../components/captcha4/index.vue";
  32. import {
  33. firstTelBind,
  34. sendCode
  35. } from "@/api/login.js"
  36. import {
  37. toast
  38. } from "../../utils/common";
  39. export default {
  40. data() {
  41. return {
  42. myflag: 0,
  43. sliderObj: {
  44. sessionId: '',
  45. sig: '',
  46. token: '',
  47. },
  48. bindObj: {
  49. apple: false,
  50. openId: '',
  51. telNumber: '',
  52. clearTelIcon: false,
  53. yzmNumber: '',
  54. clearYzmIcon: false,
  55. /*** 验证码 ***/
  56. yzmStatus: 'login-btn-disabled',
  57. timeLeft: 60, // 初始倒计时时间(秒)
  58. intervalId: null, // 定时器ID
  59. isDisabled: true, // 按钮是否禁用
  60. buttonText: '获取验证码', // 按钮文本
  61. getYzmFlag: false, // 是否发送验证码
  62. },
  63. config: {
  64. captchaId: "16e4de331cee10dfe36bcf55810c6041",
  65. },
  66. }
  67. },
  68. components: {
  69. captcha
  70. },
  71. methods: {
  72. telClose() {
  73. this.$emit('telClose')
  74. },
  75. // 清空手机号
  76. clearTel() {
  77. this.bindObj.telNumber = '';
  78. this.bindObj.isDisabled = true;
  79. this.bindObj.clearTelIcon = false;
  80. this.bindObj.getYzmFlag = false;
  81. },
  82. // 判断是否输入手机号
  83. changeTelInput(event) {
  84. if (event.detail.value.length > 0) {
  85. this.bindObj.clearTelIcon = true;
  86. this.validatePhoneNumber(event.detail.value);
  87. } else {
  88. this.bindObj.clearTelIcon = false;
  89. }
  90. },
  91. // 手机号校验规则
  92. validatePhoneNumber(value) {
  93. const phoneRegex = /^1[3-9]\d{9}$/;
  94. if (phoneRegex.test(value)) {
  95. // 通过
  96. this.bindObj.isDisabled = false;
  97. } else {
  98. // 不通过
  99. this.bindObj.isDisabled = true;
  100. }
  101. },
  102. // 获取验证码按钮
  103. getYzmBtn() {
  104. // 判断手机号校验是否通过
  105. if (this.bindObj.timeLeft != 60) {
  106. toast('请在' + this.bindObj.timeLeft + '后重新获取验证码!')
  107. return
  108. } else if (this.bindObj.isDisabled === true) {
  109. toast("请输入正确的手机号!")
  110. return
  111. } else {
  112. this.$refs.captcha.showCaptcha();
  113. }
  114. },
  115. captchaSuccess(result) { // app端的回调
  116. console.log(result)
  117. this.startCountdown();
  118. this.sliderData = result;
  119. this.getMessage();
  120. },
  121. captchaError(e) {
  122. // app端的回调
  123. toast(JSON.stringify(e))
  124. },
  125. captchaReady() {
  126. // app端的回调
  127. },
  128. captchaFail() {
  129. // app端的回调
  130. toast('验证失败!')
  131. },
  132. captchaClose() {
  133. uni.redirectTo({
  134. url: `/pages/login/index`
  135. })
  136. },
  137. getMessage() {
  138. let req = {
  139. phone: this.bindObj.telNumber,
  140. captchaOutput: this.sliderData.captcha_output,
  141. genTime: this.sliderData.gen_time,
  142. lotNumber: this.sliderData.lot_number,
  143. passToken: this.sliderData.pass_token,
  144. }
  145. sendCode(req).then(res => {
  146. this.bindObj.getYzmFlag = true;
  147. }).catch(err => {
  148. toast('验证码获取失败:' + err)
  149. })
  150. },
  151. // 清空验证码
  152. clearYzm() {
  153. this.bindObj.yzmNumber = '';
  154. this.bindObj.isDisabled = true;
  155. this.bindObj.clearYzmIcon = false;
  156. },
  157. // 判断是否输入验证码
  158. changeYzmInput(event) {
  159. if (event.detail.value.length > 0) {
  160. this.bindObj.clearYzmIcon = true;
  161. } else {
  162. this.bindObj.clearYzmIcon = false;
  163. }
  164. },
  165. // 开始计时
  166. startCountdown() {
  167. if (this.bindObj.buttonText === '重新发送') {
  168. this.$refs.captcha.showCaptcha();
  169. }
  170. this.bindObj.isDisabled = true;
  171. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  172. // 清除之前的定时器(如果有)
  173. if (this.bindObj.intervalId) {
  174. clearInterval(this.bindObj.intervalId);
  175. }
  176. // 设置新的定时器
  177. this.bindObj.intervalId = setInterval(() => {
  178. this.bindObj.timeLeft--;
  179. if (this.bindObj.timeLeft <= 0) {
  180. clearInterval(this.bindObj.intervalId);
  181. this.bindObj.timeLeft = 60; // 重置倒计时
  182. this.bindObj.isDisabled = false;
  183. this.bindObj.buttonText = '重新发送';
  184. } else {
  185. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  186. }
  187. }, 1000);
  188. },
  189. getOpenId(data) {
  190. this.bindObj.openId = data
  191. },
  192. // 绑定按钮
  193. bindBtn() {
  194. if (this.bindObj.telNumber === '') {
  195. toast('手机号不能为空')
  196. return;
  197. }
  198. if (this.bindObj.yzmNumber === '') {
  199. toast('验证码不能为空')
  200. return;
  201. }
  202. let req = {
  203. tel: this.bindObj.telNumber,
  204. code: this.bindObj.yzmNumber,
  205. apple: this.bindObj.apple,
  206. openId: this.bindObj.openId,
  207. }
  208. console.log(req);
  209. firstTelBind(req).then(res => {
  210. if (res.code == 0) {
  211. console.log(res.data);
  212. toast('手机号绑定成功')
  213. cacheManager.set('auth', res.data)
  214. this.$emit('bindBtn', res.data)
  215. // this.telClose();
  216. }
  217. })
  218. },
  219. }
  220. }
  221. </script>