telDialog.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.timeLeft !=60) {
  101. toast('请在'+this.bindObj.timeLeft +'后重新获取验证码!')
  102. return
  103. }else if(this.bindObj.isDisabled === true) {
  104. toast("请输入正确的手机号!")
  105. return
  106. }else{
  107. this.startCountdown();
  108. this.getMessage();
  109. this.myflag++;
  110. this.sliderObj = {};
  111. }
  112. },
  113. getMessage() {
  114. let req = {
  115. appkey: "FFFF0N00000000007EC0",
  116. phone: this.bindObj.telNumber,
  117. scene: "nc_message_h5',",
  118. sessionid: this.sliderObj.sessionId,
  119. sig: this.sliderObj.sig,
  120. token: this.sliderObj.token,
  121. }
  122. sendCode(req).then(res => {
  123. this.bindObj.getYzmFlag = true;
  124. }).catch(err => {
  125. toast('验证码获取失败:' + err)
  126. })
  127. },
  128. // 清空验证码
  129. clearYzm() {
  130. this.bindObj.yzmNumber = '';
  131. this.bindObj.isDisabled = true;
  132. this.bindObj.clearYzmIcon = false;
  133. },
  134. // 判断是否输入验证码
  135. changeYzmInput(event) {
  136. if (event.detail.value.length > 0) {
  137. this.bindObj.clearYzmIcon = true;
  138. } else {
  139. this.bindObj.clearYzmIcon = false;
  140. }
  141. },
  142. // 开始计时
  143. startCountdown() {
  144. if (this.bindObj.buttonText === '重新发送') {
  145. this.sliderFlag = true;
  146. }
  147. this.bindObj.isDisabled = true;
  148. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  149. // 清除之前的定时器(如果有)
  150. if (this.bindObj.intervalId) {
  151. clearInterval(this.bindObj.intervalId);
  152. }
  153. // 设置新的定时器
  154. this.bindObj.intervalId = setInterval(() => {
  155. this.bindObj.timeLeft--;
  156. if (this.bindObj.timeLeft <= 0) {
  157. clearInterval(this.bindObj.intervalId);
  158. this.bindObj.timeLeft = 60; // 重置倒计时
  159. this.bindObj.isDisabled = false;
  160. this.bindObj.buttonText = '重新发送';
  161. } else {
  162. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  163. }
  164. }, 1000);
  165. },
  166. // 绑定按钮
  167. bindBtn() {
  168. if (this.bindObj.telNumber === '') {
  169. toast('手机号不能为空')
  170. return;
  171. }
  172. if (this.bindObj.yzmNumber === '') {
  173. toast('验证码不能为空')
  174. return;
  175. }
  176. let req = {
  177. tel: this.bindObj.telNumber,
  178. code: this.bindObj.yzmNumber,
  179. }
  180. telBind(req).then(res => {
  181. if (res.code == 0) {
  182. toast('手机号绑定成功')
  183. this.updataTel(this.bindObj.telNumber);
  184. this.telClose();
  185. this.$emit('bindBtn')
  186. }
  187. })
  188. },
  189. // 在缓存中修改手机号
  190. updataTel(data) {
  191. cacheManager.updateObject('auth', {
  192. userName: data
  193. })
  194. }
  195. }
  196. }
  197. </script>
  198. <script module="AWSC" lang="renderjs">
  199. import {
  200. toast
  201. } from "../../utils/common";
  202. export default {
  203. mounted() {
  204. const script = document.createElement('script');
  205. script.src = 'https://g.alicdn.com/AWSC/AWSC/awsc.js';
  206. document.body.appendChild(script);
  207. script.onload = () => {
  208. this.init()
  209. }
  210. },
  211. data() {
  212. return {
  213. sessionId: '',
  214. sig: '',
  215. token: '',
  216. nc: null,
  217. }
  218. },
  219. methods: {
  220. init() {
  221. let that = this
  222. AWSC.use("nc", function(state, module) {
  223. that.nc = module.init({
  224. // 应用类型标识。它和使用场景标识(scene字段)一起决定了滑动验证的业务场景与后端对应使用的策略模型。您可以在阿里云验证码控制台的配置管理页签找到对应的appkey字段值,请务必正确填写。
  225. appkey: "FFFF0N00000000007EC0",
  226. //使用场景标识。它和应用类型标识(appkey字段)一起决定了滑动验证的业务场景与后端对应使用的策略模型。您可以在阿里云验证码控制台的配置管理页签找到对应的scene值,请务必正确填写。
  227. scene: "nc_message_h5",
  228. // 声明滑动验证需要渲染的目标ID。
  229. renderTo: "my-yzm-slider",
  230. //前端滑动验证通过时会触发该回调参数。您可以在该回调参数中将会话ID(sessionId)、签名串(sig)、请求唯一标识(token)字段记录下来,随业务请求一同发送至您的服务端调用验签。
  231. success: function(data) {
  232. that.getData(data)
  233. },
  234. // 滑动验证失败时触发该回调参数。
  235. fail: function(failCode) {
  236. console.log('失败:' + failCode)
  237. },
  238. // 验证码加载出现异常时触发该回调参数。
  239. error: function(errorCode) {
  240. console.log('异常:' + errorCode)
  241. }
  242. });
  243. })
  244. },
  245. getData(data) {
  246. this.sessionId = data.sessionId
  247. this.sig = data.sig
  248. this.token = data.token
  249. AWSC.getSliderData;
  250. },
  251. getReset() {
  252. this.sessionId = '';
  253. this.sig = '';
  254. this.token = '';
  255. this.nc && this.nc.reset();
  256. },
  257. getSliderData(e, ownerInstance) {
  258. if (this.sessionId) {
  259. ownerInstance.callMethod('receiveRenderData', {
  260. sessionId: this.sessionId,
  261. sig: this.sig,
  262. token: this.token
  263. })
  264. } else {
  265. toast("请先完成滑块验证!")
  266. }
  267. }
  268. }
  269. }
  270. </script>