huan_bang.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var app = getApp();
  2. const util = require('../../utils/utils.js');
  3. let timer1 = null;
  4. Page({
  5. data: {
  6. smsVal: '', //输入的短信验证码
  7. phoneVal: '', //输入的手机号码
  8. code: '获取验证码', //获取短信验证码
  9. currentTime: 60, //倒计时秒数
  10. codeDisabled: false, //是否禁用按钮
  11. timer: null, //定时器
  12. },
  13. onLoad: function (options) {
  14. },
  15. //验证手机号
  16. ckPhone(e) {
  17. this.setData({
  18. phoneVal: e.detail.value
  19. })
  20. },
  21. //验证短信验证码
  22. ckSms(e) {
  23. this.setData({
  24. smsVal: e.detail.value
  25. })
  26. },
  27. getCode: util.throttle(function () {
  28. let self = this
  29. let currentTime = self.data.currentTime
  30. let timer = self.data.timer
  31. if (self.data.phoneVal.length != 11) {
  32. wx.showToast({
  33. title: '请输入正确的手机号',
  34. icon: 'none',
  35. duration: 2000
  36. })
  37. } else {
  38. // 调用短信验证码接口
  39. let data = {
  40. mobile: this.data.phoneVal,
  41. type:2
  42. }
  43. app.request('/verification-code', data, 'POST').then(res => {
  44. if (res.statusCode == 200) {
  45. if (!timer) {
  46. timer = setInterval(() => {
  47. if (currentTime > 0 && currentTime <= 60) {
  48. currentTime--;
  49. }
  50. if (currentTime !== 0) {
  51. self.setData({
  52. code: "重新发送" + "(" + currentTime + ")",
  53. codeDisabled: true,
  54. })
  55. } else {
  56. clearInterval(timer);
  57. self.setData({
  58. code: '获取验证码',
  59. codeDisabled: false,
  60. currentTime: 60,
  61. timer: null,
  62. })
  63. }
  64. }, 1000)
  65. }
  66. }
  67. })
  68. //60秒倒计时
  69. }
  70. }, 1000),
  71. verify: util.throttle(function () {
  72. let reg = 11 && /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
  73. let reg1 = /^\d{4}$/
  74. if (!reg1.test(this.data.smsVal)) {
  75. wx.showToast({
  76. title: '请输入短信验证码',
  77. icon: 'none',
  78. duration: 2000
  79. })
  80. }
  81. if (!reg.test(this.data.phoneVal)) {
  82. wx.showToast({
  83. title: '请输入正确的手机号',
  84. icon: 'none',
  85. duration: 2000
  86. })
  87. return;
  88. } else if (!this.data.smsVal) {
  89. wx.showToast({
  90. title: '短信验证码不能为空',
  91. icon: 'none',
  92. duration: 2000
  93. })
  94. return;
  95. } else {
  96. if (app.globalData.req) {
  97. let data = {
  98. mobile: this.data.phoneVal,
  99. code: this.data.smsVal
  100. }
  101. app.request('/user/bind-mobile', data, 'POST', app.globalData.req).then(res => {
  102. if (res.statusCode == 200) {
  103. wx.showToast({
  104. title: '换绑成功',
  105. icon:'none'
  106. })
  107. }
  108. if (res.statusCode == 422) {
  109. console.log(res)
  110. wx.showToast({
  111. title: res.data.errors.mobile[0],
  112. icon: 'none',
  113. duration: 2000
  114. })
  115. }
  116. })
  117. } else {
  118. wx.showToast({
  119. title: '您的操作过于频繁,请稍后再试~',
  120. icon: 'none'
  121. })
  122. }
  123. }
  124. }, 1000),
  125. onReady: function () {
  126. },
  127. onShow: function () {
  128. },
  129. onHide: function () {
  130. },
  131. onUnload: function () {
  132. },
  133. onPullDownRefresh: function () {
  134. },
  135. onReachBottom: function () {
  136. },
  137. onShareAppMessage: function () {
  138. }
  139. })