telDialog.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. <view id="my-yzm-slider" :myflag="myflag" :change:myflag="AWSC.getReset" ></view>
  13. <view class="get-yzm-btn" @click="AWSC.getSliderData"
  14. :class="{ 'get-yzm-disabled': bindObj.isDisabled}">{{bindObj.buttonText}}</view>
  15. </view>
  16. <view class="yzm-row">
  17. <view class="yzm-tip" v-if="bindObj.getYzmFlag">验证码已发送至:{{bindObj.telNumber}}</view>
  18. <view class="my-input-box">
  19. <input class="my-input" type="text" v-model="bindObj.yzmNumber" placeholder="请输入验证码" maxlength="6"
  20. @input="changeYzmInput" />
  21. <view class="close-btn" v-if="bindObj.clearYzmIcon" @click="clearYzm"></view>
  22. </view>
  23. <view @click="bindBtn" class="my-bind-btn">绑定</view>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import cacheManager from '@/utils/cacheManager.js';
  30. import {
  31. telBind,
  32. sendCode
  33. } from "@/api/login.js"
  34. import {
  35. toast
  36. } from "../../utils/common";
  37. export default {
  38. data() {
  39. return {
  40. myflag: 0,
  41. sliderObj: {
  42. sessionId: '',
  43. sig: '',
  44. token: '',
  45. },
  46. bindObj: {
  47. telNumber: '',
  48. clearTelIcon: false,
  49. yzmNumber: '',
  50. clearYzmIcon: false,
  51. /*** 验证码 ***/
  52. yzmStatus: 'login-btn-disabled',
  53. timeLeft: 60, // 初始倒计时时间(秒)
  54. intervalId: null, // 定时器ID
  55. isDisabled: true, // 按钮是否禁用
  56. buttonText: '获取验证码', // 按钮文本
  57. getYzmFlag: false, // 是否发送验证码
  58. },
  59. }
  60. },
  61. methods: {
  62. receiveRenderData(data) {
  63. this.sliderObj = data;
  64. this.getYzmBtn();
  65. },
  66. telClose(AWSC) {
  67. AWSC.getReset();
  68. this.$emit('telClose')
  69. },
  70. // 清空手机号
  71. clearTel() {
  72. this.bindObj.telNumber = '';
  73. this.bindObj.isDisabled = true;
  74. this.bindObj.clearTelIcon = false;
  75. this.bindObj.getYzmFlag = false;
  76. },
  77. // 判断是否输入手机号
  78. changeTelInput(event) {
  79. if (event.detail.value.length > 0) {
  80. this.bindObj.clearTelIcon = true;
  81. this.validatePhoneNumber(event.detail.value);
  82. } else {
  83. this.bindObj.clearTelIcon = false;
  84. }
  85. },
  86. // 手机号校验规则
  87. validatePhoneNumber(value) {
  88. const phoneRegex = /^1[3-9]\d{9}$/;
  89. if (phoneRegex.test(value)) {
  90. // 通过
  91. this.bindObj.isDisabled = false;
  92. } else {
  93. // 不通过
  94. this.bindObj.isDisabled = true;
  95. }
  96. },
  97. // 获取验证码按钮
  98. getYzmBtn() {
  99. // 判断手机号校验是否通过
  100. if (this.bindObj.isDisabled === true) {
  101. toast("请输入正确的手机号!")
  102. return
  103. }else{
  104. this.startCountdown();
  105. this.getMessage();
  106. this.myflag++;
  107. this.sliderObj = {};
  108. }
  109. },
  110. getMessage() {
  111. let req = {
  112. appkey: "FFFF0N00000000007EC0",
  113. phone: this.bindObj.telNumber,
  114. scene: "nc_message_h5',",
  115. sessionid: this.sliderObj.sessionId,
  116. sig: this.sliderObj.sig,
  117. token: this.sliderObj.token,
  118. }
  119. sendCode(req).then(res => {
  120. this.bindObj.getYzmFlag = true;
  121. }).catch(err => {
  122. toast('验证码获取失败:' + err)
  123. })
  124. },
  125. // 清空验证码
  126. clearYzm() {
  127. this.bindObj.yzmNumber = '';
  128. this.bindObj.isDisabled = true;
  129. this.bindObj.clearYzmIcon = false;
  130. },
  131. // 判断是否输入验证码
  132. changeYzmInput(event) {
  133. if (event.detail.value.length > 0) {
  134. this.bindObj.clearYzmIcon = true;
  135. } else {
  136. this.bindObj.clearYzmIcon = false;
  137. }
  138. },
  139. // 开始计时
  140. startCountdown() {
  141. if (this.bindObj.buttonText === '重新发送') {
  142. this.sliderFlag = true;
  143. }
  144. this.bindObj.isDisabled = true;
  145. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  146. // 清除之前的定时器(如果有)
  147. if (this.bindObj.intervalId) {
  148. clearInterval(this.bindObj.intervalId);
  149. }
  150. // 设置新的定时器
  151. this.bindObj.intervalId = setInterval(() => {
  152. this.bindObj.timeLeft--;
  153. if (this.bindObj.timeLeft <= 0) {
  154. clearInterval(this.bindObj.intervalId);
  155. this.bindObj.timeLeft = 60; // 重置倒计时
  156. this.bindObj.isDisabled = false;
  157. this.bindObj.buttonText = '重新发送';
  158. } else {
  159. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  160. }
  161. }, 1000);
  162. },
  163. // 绑定按钮
  164. bindBtn() {
  165. if (this.bindObj.telNumber === '') {
  166. toast('手机号不能为空')
  167. return;
  168. }
  169. if (this.bindObj.yzmNumber === '') {
  170. toast('验证码不能为空')
  171. return;
  172. }
  173. let req = {
  174. tel: this.bindObj.telNumber,
  175. code: this.bindObj.yzmNumber,
  176. }
  177. telBind(req).then(res => {
  178. if (res.code == 0) {
  179. toast('手机号绑定成功')
  180. this.updataTel(this.bindObj.telNumber);
  181. this.telClose();
  182. this.$emit('bindBtn')
  183. }
  184. })
  185. },
  186. // 在缓存中修改手机号
  187. updataTel(data) {
  188. cacheManager.updateObject('auth', {
  189. userName: data
  190. })
  191. }
  192. }
  193. }
  194. </script>
  195. <script module="AWSC" lang="renderjs">
  196. import {
  197. toast
  198. } from "../../utils/common";
  199. export default {
  200. mounted() {
  201. const script = document.createElement('script');
  202. script.src = 'https://g.alicdn.com/AWSC/AWSC/awsc.js';
  203. document.body.appendChild(script);
  204. script.onload = () => {
  205. this.init()
  206. }
  207. },
  208. data() {
  209. return {
  210. sessionId: '',
  211. sig: '',
  212. token: '',
  213. nc: null,
  214. }
  215. },
  216. methods: {
  217. init() {
  218. let that = this
  219. AWSC.use("nc", function(state, module) {
  220. that.nc = module.init({
  221. // 应用类型标识。它和使用场景标识(scene字段)一起决定了滑动验证的业务场景与后端对应使用的策略模型。您可以在阿里云验证码控制台的配置管理页签找到对应的appkey字段值,请务必正确填写。
  222. appkey: "FFFF0N00000000007EC0",
  223. //使用场景标识。它和应用类型标识(appkey字段)一起决定了滑动验证的业务场景与后端对应使用的策略模型。您可以在阿里云验证码控制台的配置管理页签找到对应的scene值,请务必正确填写。
  224. scene: "nc_message_h5",
  225. // 声明滑动验证需要渲染的目标ID。
  226. renderTo: "my-yzm-slider",
  227. //前端滑动验证通过时会触发该回调参数。您可以在该回调参数中将会话ID(sessionId)、签名串(sig)、请求唯一标识(token)字段记录下来,随业务请求一同发送至您的服务端调用验签。
  228. success: function(data) {
  229. that.getData(data)
  230. },
  231. // 滑动验证失败时触发该回调参数。
  232. fail: function(failCode) {
  233. console.log('失败:' + failCode)
  234. },
  235. // 验证码加载出现异常时触发该回调参数。
  236. error: function(errorCode) {
  237. console.log('异常:' + errorCode)
  238. }
  239. });
  240. })
  241. },
  242. getData(data) {
  243. this.sessionId = data.sessionId
  244. this.sig = data.sig
  245. this.token = data.token
  246. AWSC.getSliderData;
  247. },
  248. getReset() {
  249. this.sessionId = '';
  250. this.sig = '';
  251. this.token = '';
  252. this.nc && this.nc.reset();
  253. },
  254. getSliderData(e, ownerInstance) {
  255. if (this.sessionId) {
  256. ownerInstance.callMethod('receiveRenderData', {
  257. sessionId: this.sessionId,
  258. sig: this.sig,
  259. token: this.token
  260. })
  261. } else {
  262. toast("请先完成滑块验证!")
  263. }
  264. }
  265. }
  266. }
  267. </script>