telDialog.vue 7.8 KB

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