bind.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. var btd = wx.getStorageSync('backToDetail');
  32. console.log('backtodeail',btd);
  33. if(btd){
  34. wx.navigateBack({});
  35. wx.showToast({
  36. title: '手机号码绑定成功~',
  37. icon: 'none',
  38. duration: 2000
  39. })
  40. return;
  41. }
  42. var bindbackurl = wx.getStorageSync('bindbackurl');
  43. if(bindbackurl){
  44. wx.navigateBack({})
  45. wx.navigateTo({
  46. url: bindbackurl
  47. })
  48. wx.showToast({
  49. title: '手机号码绑定成功~',
  50. icon: 'none',
  51. duration: 2000
  52. })
  53. }else{
  54. wx.navigateTo({
  55. url: '/packageUser/pages/user/bindsuccess/bindsuccess'
  56. })
  57. }
  58. }
  59. _request.$post(url, params, success)
  60. },
  61. bindPhone (e) {
  62. this.setData({
  63. tel: e.detail.value
  64. })
  65. },
  66. bindYzm (e) {
  67. this.setData({
  68. yzm: e.detail.value
  69. })
  70. },
  71. getYzm (e) {
  72. _handle.setFormId(e)
  73. if (validator.telphone(this.data.tel)) {
  74. this.requireYzm()
  75. } else {
  76. wx.showToast({
  77. title: '手机号码格式不正确',
  78. icon: 'none',
  79. duration: 2000
  80. })
  81. }
  82. },
  83. startCountdown: function () {
  84. var smsRequestTime = wx.getStorageSync('sms_request_time')
  85. if (smsRequestTime) {
  86. var count = parseInt((60000 - (new Date() - smsRequestTime)) / 1000)
  87. this.setData({
  88. countdown: count
  89. })
  90. if (this.data.countdown > 0) {
  91. var timer = setInterval(() => {
  92. var countdown = this.data.countdown - 1
  93. this.setData({
  94. countdown: countdown
  95. })
  96. if (this.data.countdown <= 0) {
  97. clearInterval(timer)
  98. }
  99. }, 1000)
  100. // clearInterval(this.interval)
  101. }
  102. } else {
  103. }
  104. },
  105. requireYzm () {
  106. var that = this
  107. var url = 'v1/code/send'
  108. var params = {
  109. tel: this.data.tel,
  110. valid_type: 'binding'
  111. }
  112. var success = function (res) {
  113. wx.setStorageSync('sms_request_time', Date.now())
  114. that.startCountdown()
  115. }
  116. _request.$post(url, params, success)
  117. },
  118. requireBindTel () {
  119. var that = this
  120. var url = 'v1/wxuser/binding/tel'
  121. var params = {
  122. tel: this.data.tel,
  123. code: this.data.yzm
  124. }
  125. var success = function (res) {
  126. var btd = wx.getStorageSync('backToDetail');
  127. console.log('backtodeail',btd);
  128. if(btd){
  129. wx.navigateBack({});
  130. wx.showToast({
  131. title: '手机号码绑定成功~',
  132. icon: 'none',
  133. duration: 2000
  134. })
  135. }
  136. var bindbackurl = wx.getStorageSync('bindbackurl');
  137. if(bindbackurl){
  138. wx.navigateBack({})
  139. wx.navigateTo({
  140. url: bindbackurl
  141. })
  142. wx.showToast({
  143. title: '手机号码绑定成功~',
  144. icon: 'none',
  145. duration: 2000
  146. })
  147. }else{
  148. wx.navigateTo({
  149. url: '/packageUser/pages/user/bindsuccess/bindsuccess'
  150. })
  151. }
  152. }
  153. _request.$post(url, params, success)
  154. },
  155. setBind (e) {
  156. _handle.setFormId(e)
  157. var { isOk, msg } = this.validate()
  158. if (isOk) {
  159. this.requireBindTel()
  160. } else {
  161. wx.showToast({
  162. title: msg,
  163. icon: 'none',
  164. duration: 2000
  165. })
  166. }
  167. },
  168. validate () {
  169. var msg
  170. if (!validator.required(this.data.tel)) {
  171. msg = '手机不能为空'
  172. } else if (!validator.telphone(this.data.tel)) {
  173. msg = '手机号码格式不正确'
  174. } else if (!validator.required(this.data.yzm)) {
  175. msg = '验证码不能为空'
  176. }
  177. return { isOk: !msg, msg }
  178. },
  179. otherbind () {
  180. var that = this
  181. var url = 'v1/wxuser/one_click/binding/tel'
  182. var params = {
  183. }
  184. var success = function (res) {
  185. wx.navigateTo({
  186. url: '/packageUser/pages/user/bindsuccess/bindsuccess'
  187. })
  188. }
  189. _request.$post(url, params, success)
  190. },
  191. onShareAppMessage: function (val) {
  192. return _request.share({
  193. sc: 'xcx_user_bind'
  194. })
  195. }
  196. })