telDialog.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. firstTelBind,
  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. apple: false,
  48. openId: '',
  49. telNumber: '',
  50. clearTelIcon: false,
  51. yzmNumber: '',
  52. clearYzmIcon: false,
  53. /*** 验证码 ***/
  54. yzmStatus: 'login-btn-disabled',
  55. timeLeft: 60, // 初始倒计时时间(秒)
  56. intervalId: null, // 定时器ID
  57. isDisabled: true, // 按钮是否禁用
  58. buttonText: '获取验证码', // 按钮文本
  59. getYzmFlag: false, // 是否发送验证码
  60. },
  61. }
  62. },
  63. methods: {
  64. receiveRenderData(data) {
  65. this.sliderObj = data;
  66. this.getYzmBtn();
  67. },
  68. telClose(AWSC) {
  69. AWSC.getReset();
  70. this.$emit('telClose')
  71. },
  72. // 清空手机号
  73. clearTel() {
  74. this.bindObj.telNumber = '';
  75. this.bindObj.isDisabled = true;
  76. this.bindObj.clearTelIcon = false;
  77. this.bindObj.getYzmFlag = false;
  78. },
  79. getOpenId(data){
  80. this.bindObj.openId = data
  81. },
  82. // 判断是否输入手机号
  83. changeTelInput(event) {
  84. if (event.detail.value.length > 0) {
  85. this.bindObj.clearTelIcon = true;
  86. this.validatePhoneNumber(event.detail.value);
  87. } else {
  88. this.bindObj.clearTelIcon = false;
  89. }
  90. },
  91. // 手机号校验规则
  92. validatePhoneNumber(value) {
  93. const phoneRegex = /^1[3-9]\d{9}$/;
  94. if (phoneRegex.test(value)) {
  95. // 通过
  96. this.bindObj.isDisabled = false;
  97. } else {
  98. // 不通过
  99. this.bindObj.isDisabled = true;
  100. }
  101. },
  102. // 获取验证码按钮
  103. getYzmBtn() {
  104. // 判断手机号校验是否通过
  105. if (this.bindObj.timeLeft != 60) {
  106. toast('请在' + this.bindObj.timeLeft + '后重新获取验证码!')
  107. return
  108. } else if (this.bindObj.isDisabled === true) {
  109. toast("请输入正确的手机号!")
  110. return
  111. } else {
  112. this.startCountdown();
  113. this.getMessage();
  114. this.myflag++;
  115. this.sliderObj = {};
  116. }
  117. },
  118. getMessage() {
  119. let req = {
  120. appkey: "FFFF0N00000000007EC0",
  121. phone: this.bindObj.telNumber,
  122. scene: "nc_message_h5',",
  123. sessionid: this.sliderObj.sessionId,
  124. sig: this.sliderObj.sig,
  125. token: this.sliderObj.token,
  126. }
  127. sendCode(req).then(res => {
  128. this.bindObj.getYzmFlag = true;
  129. }).catch(err => {
  130. toast('验证码获取失败:' + err)
  131. })
  132. },
  133. // 清空验证码
  134. clearYzm() {
  135. this.bindObj.yzmNumber = '';
  136. this.bindObj.isDisabled = true;
  137. this.bindObj.clearYzmIcon = false;
  138. },
  139. // 判断是否输入验证码
  140. changeYzmInput(event) {
  141. if (event.detail.value.length > 0) {
  142. this.bindObj.clearYzmIcon = true;
  143. } else {
  144. this.bindObj.clearYzmIcon = false;
  145. }
  146. },
  147. // 开始计时
  148. startCountdown() {
  149. if (this.bindObj.buttonText === '重新发送') {
  150. this.sliderFlag = true;
  151. }
  152. this.bindObj.isDisabled = true;
  153. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  154. // 清除之前的定时器(如果有)
  155. if (this.bindObj.intervalId) {
  156. clearInterval(this.bindObj.intervalId);
  157. }
  158. // 设置新的定时器
  159. this.bindObj.intervalId = setInterval(() => {
  160. this.bindObj.timeLeft--;
  161. if (this.bindObj.timeLeft <= 0) {
  162. clearInterval(this.bindObj.intervalId);
  163. this.bindObj.timeLeft = 60; // 重置倒计时
  164. this.bindObj.isDisabled = false;
  165. this.bindObj.buttonText = '重新发送';
  166. } else {
  167. this.bindObj.buttonText = `重新发送(${this.bindObj.timeLeft}S)`;
  168. }
  169. }, 1000);
  170. },
  171. // 绑定按钮
  172. bindBtn() {
  173. if (this.bindObj.telNumber === '') {
  174. toast('手机号不能为空')
  175. return;
  176. }
  177. if (this.bindObj.yzmNumber === '') {
  178. toast('验证码不能为空')
  179. return;
  180. }
  181. let req = {
  182. tel: this.bindObj.telNumber,
  183. code: this.bindObj.yzmNumber,
  184. apple: this.bindObj.apple,
  185. openId: this.bindObj.openId,
  186. }
  187. console.log(req);
  188. firstTelBind(req).then(res => {
  189. if (res.code == 0) {
  190. console.log(res.data);
  191. toast('手机号绑定成功')
  192. cacheManager.set('auth',res.data)
  193. this.$emit('bindBtn',res.data)
  194. // this.telClose();
  195. }
  196. })
  197. },
  198. }
  199. }
  200. </script>
  201. <script module="AWSC" lang="renderjs">
  202. import {
  203. toast
  204. } from "../../utils/common";
  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. toast("请先完成滑块验证!")
  269. }
  270. }
  271. }
  272. }
  273. </script>