bind.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // packageUser/pages/user/bind/bind.js
  2. var validator = require('../../../../utils/validator.js')
  3. var _request = require('../../../../utils/request.js')
  4. var _handle = require('../../../../utils/handle.js')
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. tel: 0,
  11. yzm: '',
  12. countdown: 0
  13. },
  14. getTel (val) {
  15. console.log(val)
  16. if (val.detail.encryptedData) {
  17. this.bindWxTel(val.detail)
  18. }
  19. },
  20. bindWxTel (res) {
  21. var str = JSON.stringify({
  22. encryptedData: res.encryptedData,
  23. iv: res.iv
  24. })
  25. var that = this
  26. var url = 'v1/wxuser/one_click/binding/phonumer'
  27. var params = {
  28. userinfo: str
  29. }
  30. var success = function (res) {
  31. wx.navigateTo({
  32. url: '/packageUser/pages/user/bindsuccess/bindsuccess'
  33. })
  34. }
  35. _request.$post(url, params, success)
  36. },
  37. bindPhone (e) {
  38. this.setData({
  39. tel: e.detail.value
  40. })
  41. },
  42. bindYzm (e) {
  43. this.setData({
  44. yzm: e.detail.value
  45. })
  46. },
  47. getYzm (e) {
  48. _handle.setFormId(e)
  49. if (validator.telphone(this.data.tel)) {
  50. this.requireYzm()
  51. } else {
  52. wx.showToast({
  53. title: '手机号码格式不正确',
  54. icon: 'none',
  55. duration: 2000
  56. })
  57. }
  58. },
  59. startCountdown: function () {
  60. var smsRequestTime = wx.getStorageSync('sms_request_time')
  61. if (smsRequestTime) {
  62. var count = parseInt((60000 - (new Date() - smsRequestTime)) / 1000)
  63. this.setData({
  64. countdown: count
  65. })
  66. if (this.data.countdown > 0) {
  67. var timer = setInterval(() => {
  68. var countdown = this.data.countdown - 1
  69. this.setData({
  70. countdown: countdown
  71. })
  72. if (this.data.countdown <= 0) {
  73. clearInterval(timer)
  74. }
  75. }, 1000)
  76. // clearInterval(this.interval)
  77. }
  78. } else {
  79. }
  80. },
  81. requireYzm () {
  82. var that = this
  83. var url = 'v1/code/send'
  84. var params = {
  85. tel: this.data.tel,
  86. valid_type: 'binding'
  87. }
  88. var success = function (res) {
  89. wx.setStorageSync('sms_request_time', Date.now())
  90. that.startCountdown()
  91. }
  92. _request.$post(url, params, success)
  93. },
  94. requireBindTel () {
  95. var that = this
  96. var url = 'v1/wxuser/binding/tel'
  97. var params = {
  98. tel: this.data.tel,
  99. code: this.data.yzm
  100. }
  101. var success = function (res) {
  102. wx.navigateTo({
  103. url: '/packageUser/pages/user/bindsuccess/bindsuccess'
  104. })
  105. }
  106. _request.$post(url, params, success)
  107. },
  108. setBind (e) {
  109. _handle.setFormId(e)
  110. var { isOk, msg } = this.validate()
  111. if (isOk) {
  112. this.requireBindTel()
  113. } else {
  114. wx.showToast({
  115. title: msg,
  116. icon: 'none',
  117. duration: 2000
  118. })
  119. }
  120. },
  121. validate () {
  122. var msg
  123. if (!validator.required(this.data.tel)) {
  124. msg = '手机不能为空'
  125. } else if (!validator.telphone(this.data.tel)) {
  126. msg = '手机号码格式不正确'
  127. } else if (!validator.required(this.data.yzm)) {
  128. msg = '验证码不能为空'
  129. }
  130. return { isOk: !msg, msg }
  131. },
  132. otherbind () {
  133. var that = this
  134. var url = 'v1/wxuser/one_click/binding/tel'
  135. var params = {
  136. }
  137. var success = function (res) {
  138. wx.navigateTo({
  139. url: '/packageUser/pages/user/bindsuccess/bindsuccess'
  140. })
  141. }
  142. _request.$post(url, params, success)
  143. },
  144. onShareAppMessage: function (val) {
  145. return _request.share({
  146. sc: 'xcx_user_bind'
  147. })
  148. }
  149. })