huan_bang.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. console.log(res)
  45. if (res.statusCode == 200) {
  46. if (!timer) {
  47. timer = setInterval(() => {
  48. if (currentTime > 0 && currentTime <= 60) {
  49. currentTime--;
  50. }
  51. if (currentTime !== 0) {
  52. self.setData({
  53. code: "重新发送" + "(" + currentTime + ")",
  54. codeDisabled: true,
  55. })
  56. } else {
  57. clearInterval(timer);
  58. self.setData({
  59. code: '获取验证码',
  60. codeDisabled: false,
  61. currentTime: 60,
  62. timer: null,
  63. })
  64. }
  65. }, 1000)
  66. }
  67. }
  68. })
  69. //60秒倒计时
  70. }
  71. }, 1000),
  72. verify: util.throttle(function () {
  73. 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}$/;
  74. let reg1 = /^\d{4}$/
  75. if (!reg1.test(this.data.smsVal)) {
  76. wx.showToast({
  77. title: '请输入短信验证码',
  78. icon: 'none',
  79. duration: 2000
  80. })
  81. }
  82. if (!reg.test(this.data.phoneVal)) {
  83. wx.showToast({
  84. title: '请输入正确的手机号',
  85. icon: 'none',
  86. duration: 2000
  87. })
  88. return;
  89. } else if (!this.data.smsVal) {
  90. wx.showToast({
  91. title: '短信验证码不能为空',
  92. icon: 'none',
  93. duration: 2000
  94. })
  95. return;
  96. } else {
  97. if (app.globalData.req) {
  98. let data = {
  99. mobile: this.data.phoneVal,
  100. code: this.data.smsVal
  101. }
  102. app.request('/user/bind-mobile', data, 'POST', app.globalData.req).then(res => {
  103. if (res.statusCode == 200) {
  104. wx.showToast({
  105. title: '换绑成功',
  106. icon:'none'
  107. })
  108. }
  109. if (res.statusCode == 422) {
  110. console.log(res)
  111. wx.showToast({
  112. title: res.data.errors.mobile[0],
  113. icon: 'none',
  114. duration: 2000
  115. })
  116. }
  117. })
  118. } else {
  119. wx.showToast({
  120. title: '您的操作过于频繁,请稍后再试~',
  121. icon: 'none'
  122. })
  123. }
  124. }
  125. }, 1000),
  126. onReady: function () {
  127. },
  128. onShow: function () {
  129. },
  130. onHide: function () {
  131. },
  132. onUnload: function () {
  133. },
  134. onPullDownRefresh: function () {
  135. },
  136. onReachBottom: function () {
  137. },
  138. onShareAppMessage: function () {
  139. }
  140. })