telDialog.vue 8.0 KB

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