newuser.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 if (res.errno === 1) {
  59. util.showErrorToast(res.errmsg);
  60. // wx.showModal({
  61. // title: '提示信息',
  62. // content: res.errmsg,
  63. // showCancel: false
  64. // });
  65. }
  66. });
  67. },
  68. /**
  69. * 发送短信
  70. */
  71. smscode: function () {
  72. var that = this;
  73. let regular = /^1[3|4|5|7|8]\d{9}$/;
  74. if (!regular.test(that.data.phone)) {
  75. util.showErrorToast('手机格式不正确')
  76. return false;
  77. }
  78. util.request(api.smscodeSend, {
  79. phone: that.data.phone
  80. }, 'POST').then(function (res) {
  81. let n = 59;
  82. var timer = setInterval(function () {
  83. if (n == 0) {
  84. clearInterval(timer);
  85. that.setData({
  86. second: '发送验证码',
  87. disabled: false
  88. });
  89. } else {
  90. that.setData({
  91. second: n-- +'s后重新获取',
  92. disabled: true
  93. });
  94. }
  95. }, 1000);
  96. if (res.errno == 0) {
  97. wx.showToast({
  98. title: '短信发送成功'
  99. })
  100. } else{
  101. util.showErrorToast('发送失败');
  102. clearInterval(timer);
  103. that.setData({
  104. second: '发送验证码',
  105. disabled: false
  106. });
  107. return false;
  108. }
  109. });
  110. },
  111. /**
  112. * 生命周期函数--监听页面加载
  113. */
  114. onLoad: function (options) {
  115. this.checkActivit();
  116. },
  117. /**
  118. * 生命周期函数--监听页面初次渲染完成
  119. */
  120. onReady: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面显示
  124. */
  125. onShow: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面隐藏
  129. */
  130. onHide: function () {
  131. },
  132. /**
  133. * 生命周期函数--监听页面卸载
  134. */
  135. onUnload: function () {
  136. },
  137. /**
  138. * 页面相关事件处理函数--监听用户下拉动作
  139. */
  140. onPullDownRefresh: function () {
  141. },
  142. /**
  143. * 页面上拉触底事件的处理函数
  144. */
  145. onReachBottom: function () {
  146. },
  147. /**
  148. * 用户点击右上角分享
  149. */
  150. onShareAppMessage: function () {
  151. },
  152. checkActivit() {
  153. let that = this;
  154. util.request(api.checkActivit).then(function (res) {
  155. if (res.errno === 2) {
  156. wx.showModal({
  157. title: '',
  158. content: res.errmsg,
  159. showCancel: false,
  160. success: function (res) {
  161. if (res.confirm) {
  162. wx.switchTab({
  163. url: '/pages/index/index'
  164. });
  165. }
  166. }
  167. });
  168. that.setData({
  169. disabled: true,
  170. disabledUpdate: true
  171. });
  172. }
  173. });
  174. }
  175. })