telDialog.vue 6.2 KB

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