1
0

newuser.js 3.7 KB

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