telDialog.vue 8.1 KB

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