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