telDialog.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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}}
  16. </view>
  17. </view>
  18. <view class="yzm-row">
  19. <view class="yzm-tip" v-if="bindObj.getYzmFlag">验证码已发送至:{{bindObj.telNumber}}</view>
  20. <view class="my-input-box">
  21. <input class="my-input" type="text" v-model="bindObj.yzmNumber" placeholder="请输入验证码" maxlength="6"
  22. @input="changeYzmInput" />
  23. <view class="close-btn" v-if="bindObj.clearYzmIcon" @click="clearYzm"></view>
  24. </view>
  25. <ezyActiveVue @aclick="bindBtn" class="ezy-btn-active my-bind-btn">绑定</ezyActiveVue>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import cacheManager from '@/utils/cacheManager.js';
  32. import captcha from "@/components/captcha4/index.vue";
  33. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  34. import {
  35. telBind,
  36. sendCode
  37. } from "@/api/login.js"
  38. import {
  39. toast
  40. } from "@/utils/common";
  41. export default {
  42. data() {
  43. return {
  44. myflag: 0,
  45. sliderObj: {
  46. sessionId: '',
  47. sig: '',
  48. token: '',
  49. },
  50. bindObj: {
  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: "9d5837b0807b8de44da0de310a0b2813",
  65. },
  66. }
  67. },
  68. components: {
  69. captcha,ezyActiveVue
  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. this.bindObj.getYzmFlag = false;
  149. toast('验证码获取失败:' + err)
  150. })
  151. },
  152. // 清空验证码
  153. clearYzm() {
  154. this.bindObj.yzmNumber = '';
  155. this.bindObj.isDisabled = true;
  156. this.bindObj.clearYzmIcon = false;
  157. },
  158. // 判断是否输入验证码
  159. changeYzmInput(event) {
  160. if (event.detail.value.length > 0) {
  161. this.bindObj.clearYzmIcon = true;
  162. } else {
  163. this.bindObj.clearYzmIcon = false;
  164. }
  165. },
  166. // 开始计时
  167. startCountdown() {
  168. if (this.bindObj.buttonText === '重新发送') {
  169. this.sliderFlag = true;
  170. }
  171. this.bindObj.isDisabled = true;
  172. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  173. // 清除之前的定时器(如果有)
  174. if (this.bindObj.intervalId) {
  175. clearInterval(this.bindObj.intervalId);
  176. }
  177. // 设置新的定时器
  178. this.bindObj.intervalId = setInterval(() => {
  179. this.bindObj.timeLeft--;
  180. if (this.bindObj.timeLeft <= 0) {
  181. clearInterval(this.bindObj.intervalId);
  182. this.bindObj.timeLeft = 60; // 重置倒计时
  183. this.bindObj.isDisabled = false;
  184. this.bindObj.buttonText = '重新发送';
  185. } else {
  186. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  187. }
  188. }, 1000);
  189. },
  190. // 绑定按钮
  191. bindBtn() {
  192. if (this.bindObj.telNumber === '') {
  193. toast('手机号不能为空')
  194. return;
  195. }
  196. if (this.bindObj.yzmNumber === '') {
  197. toast('验证码不能为空')
  198. return;
  199. }
  200. let req = {
  201. tel: this.bindObj.telNumber,
  202. code: this.bindObj.yzmNumber,
  203. }
  204. telBind(req).then(res => {
  205. if (res.code == 0) {
  206. toast('手机号绑定成功')
  207. this.updataTel(this.bindObj.telNumber);
  208. this.$emit('bindBtn')
  209. }
  210. })
  211. },
  212. // 在缓存中修改手机号
  213. updataTel(data) {
  214. cacheManager.updateObject('auth', {
  215. userName: data
  216. })
  217. }
  218. }
  219. }
  220. </script>