newuser.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var api = require('../../../config/api.js');
  2. var util = require('../../../utils/util.js');
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. phone: '',
  10. smscode: '',
  11. second: '发送验证码',
  12. disabled: false,
  13. disabledUpdate: false
  14. },
  15. clearPhone(){
  16. this.setData({
  17. phone: ''
  18. });
  19. },
  20. clearCode(){
  21. this.setData({
  22. smscode: ''
  23. });
  24. },
  25. bindPhoneInput: function (e) {
  26. this.setData({
  27. phone: e.detail.value
  28. });
  29. },
  30. bindSmscodeInput: function (e) {
  31. this.setData({
  32. smscode: e.detail.value
  33. });
  34. },
  35. /**
  36. * 用户领券
  37. */
  38. getCoupon: function () {
  39. var that = this;
  40. let regular = /^1[3|4|5|7|8]\d{9}$/;
  41. if (!regular.test(that.data.phone)) {
  42. util.showErrorToast('手机格式不正确')
  43. return false;
  44. }
  45. if (that.data.smscode.length == 0) {
  46. util.showErrorToast('验证码不能为空')
  47. return false;
  48. }
  49. util.request(api.newUserCoupn, {
  50. smscode: that.data.smscode,
  51. phone: that.data.phone
  52. }, 'POST').then(function (res) {
  53. if (res.errno === 0) {
  54. wx.showToast({
  55. title: '手机号绑定成功'
  56. });
  57. wx.redirectTo({ url: '/pages/ucenter/coupon/coupon' });
  58. } else {
  59. util.showErrorToast(res.errmsg);
  60. }
  61. });
  62. },
  63. /**
  64. * 发送短信
  65. */
  66. smscode: function () {
  67. var that = this;
  68. let regular = /^1[3|4|5|7|8]\d{9}$/;
  69. if (!regular.test(that.data.phone)) {
  70. util.showErrorToast('手机格式不正确')
  71. return false;
  72. }
  73. util.request(api.smscodeSend, {
  74. phone: that.data.phone
  75. }, 'POST').then(function (res) {
  76. let n = 59;
  77. var timer = setInterval(function () {
  78. if (n == 0) {
  79. clearInterval(timer);
  80. that.setData({
  81. second: '发送验证码',
  82. disabled: false
  83. });
  84. } else {
  85. that.setData({
  86. second: n-- +'s后重新获取',
  87. disabled: true
  88. });
  89. }
  90. }, 1000);
  91. if (res.errno == 0) {
  92. wx.showToast({
  93. title: '短信发送成功'
  94. })
  95. } else{
  96. util.showErrorToast('发送失败');
  97. clearInterval(timer);
  98. that.setData({
  99. second: '发送验证码',
  100. disabled: false
  101. });
  102. return false;
  103. }
  104. });
  105. },
  106. /**
  107. * 生命周期函数--监听页面加载
  108. */
  109. onLoad: function (options) {
  110. this.checkActivit();
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面卸载
  129. */
  130. onUnload: function () {
  131. },
  132. /**
  133. * 页面相关事件处理函数--监听用户下拉动作
  134. */
  135. onPullDownRefresh: function () {
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. },
  147. checkActivit() {
  148. let that = this;
  149. util.request(api.checkActivit).then(function (res) {
  150. if (res.errno === 2) {
  151. wx.showModal({
  152. title: '',
  153. content: res.errmsg,
  154. showCancel: false,
  155. success: function (res) {
  156. if (res.confirm) {
  157. wx.switchTab({
  158. url: '/pages/index/index'
  159. });
  160. }
  161. }
  162. });
  163. that.setData({
  164. disabled: true,
  165. disabledUpdate: true
  166. });
  167. }
  168. });
  169. }
  170. })