phone.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // pages/phone/phone.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. times:60,
  9. isTimes:false,
  10. tips:'获取验证码',
  11. myTime: null
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. this.setData(options)
  18. console.log(options)
  19. },
  20. /**
  21. * 生命周期函数--监听页面初次渲染完成
  22. */
  23. onReady: function () {
  24. },
  25. /**
  26. * 生命周期函数--监听页面显示
  27. */
  28. onShow: function () {
  29. },
  30. /**
  31. * 生命周期函数--监听页面隐藏
  32. */
  33. onHide: function () {
  34. clearInterval(this.myTime);
  35. },
  36. /**
  37. * 生命周期函数--监听页面卸载
  38. */
  39. onUnload: function () {
  40. },
  41. /**
  42. * 页面相关事件处理函数--监听用户下拉动作
  43. */
  44. onPullDownRefresh: function () {
  45. },
  46. /**
  47. * 页面上拉触底事件的处理函数
  48. */
  49. onReachBottom: function () {
  50. },
  51. /**
  52. * 用户点击右上角分享
  53. */
  54. onShareAppMessage: function () {
  55. },
  56. /**
  57. * 底部导航跳转
  58. */
  59. redirectCtl: app.redirectCtl,
  60. /**
  61. * 路由跳转
  62. */
  63. navigateCtl: app.navigateCtl,
  64. /**
  65. * input
  66. */
  67. inputCtl: function (e) {
  68. const mobileExp = /^1[0-9]{10}$/;
  69. const { temp = {}, type } = e.currentTarget.dataset
  70. const { value } = e.detail
  71. temp[type] = value;
  72. const { phone, verificationCode } = temp;
  73. if (type == 'phone') {
  74. this.setData({ phone })
  75. // if (phone != '') {
  76. // if (!mobileExp.test(phone)) {
  77. // wx.showToast({
  78. // title: '手机号码位数不对',
  79. // icon: 'none',
  80. // duration: 2000
  81. // })
  82. // this.setData({ phone })
  83. // return
  84. // } else {
  85. // this.setData({ phone})
  86. // }
  87. // }
  88. } else if (type == 'verificationCode') {
  89. return this.setData({ verificationCode })
  90. }
  91. },
  92. /**
  93. * 再次获取验证码
  94. */
  95. setStime:function(){
  96. const mobileExp = /^1[0-9]{10}$/;
  97. const { times, phone}=this.data;
  98. if (!phone) {
  99. wx.showToast({
  100. title: '手机号码不能为空',
  101. icon: 'none'
  102. })
  103. return
  104. } else if (!mobileExp.test(phone)) {
  105. wx.showToast({
  106. title: '手机号码位数不对',
  107. icon: 'none',
  108. duration: 1000
  109. })
  110. return
  111. }
  112. if (times == 60 || times ==0){
  113. this.getNumber()
  114. }
  115. if(times>0){
  116. this.startSetInter()
  117. }else if(times==0){
  118. this.setData({times:60})
  119. this.startSetInter()
  120. }
  121. },
  122. /**
  123. * 开始计时
  124. */
  125. startSetInter: function () {
  126. var that = this;
  127. const {phone, tips, times} = that.data;
  128. // console.log(phone, tips, times)
  129. if (times == 0) {
  130. that.setData({ tips: "获取验证码"});
  131. return ;
  132. } else {
  133. var numVal =that.data.times--;
  134. that.setData({ tips: "重新发送(" + numVal + ")"});
  135. }
  136. this.myTime=setTimeout(function () {
  137. that.startSetInter();
  138. }, 1000);
  139. },
  140. /**
  141. * 获取验证码
  142. */
  143. getNumber:function(){
  144. const { phone} = this.data;
  145. const postData = { fn: this.getNumber, param: { ...arguments } }
  146. app.post('/api/parents/parents/V2/getVerificationCode', { phone, type: 2 }, 0, postData).then(res => {
  147. wx.showToast({
  148. title: '已发送',
  149. icon: 'none',
  150. duration: 1000
  151. });
  152. })
  153. },
  154. /**
  155. * 提交
  156. */
  157. sureSumbit:function(){
  158. const mobileExp = /^1[0-9]{10}$/;
  159. const { phone, verificationCode, vipId, isFrist, derver}=this.data;
  160. console.log(phone, verificationCode, vipId, isFrist, derver)
  161. const postData = { fn: this.sureSumbit, param: { ...arguments } }
  162. if (!phone) {
  163. wx.showToast({
  164. title: '手机号码不能为空',
  165. icon: 'none'
  166. })
  167. return
  168. } else if (!mobileExp.test(phone)) {
  169. wx.showToast({
  170. title: '手机号码位数不对',
  171. icon: 'none',
  172. duration: 1000
  173. })
  174. return
  175. }
  176. if (!verificationCode) {
  177. wx.showToast({
  178. title: '请输入验证码',
  179. icon: 'none'
  180. })
  181. return
  182. }
  183. clearInterval(this.myTime);
  184. this.setData({ times: 0, tips: "重新验证码"})
  185. app.post('/api/parents/parents/V2/bindPhone', { phone, verificationCode }, 0, postData).then(res => {
  186. clearInterval(this.myTime);
  187. wx.showToast({
  188. title: '绑定成功',
  189. icon: 'none',
  190. duration: 1000
  191. });
  192. if (isFrist == 1) {
  193. app.redirectCtl({ url: 'homework_list', method: { vipId } }, true)
  194. } else {
  195. app.redirectCtl({ url: 'wechat_addchild', method: { derver } }, true)
  196. }
  197. })
  198. }
  199. })