pc-slide-verify.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var SMSRequest = {
  2. getCaptcha: function(phone, mold, callback) {
  3. $.ajax({
  4. type: 'POST',
  5. url: '/publicapply/captcha/get',
  6. data: {
  7. phone: phone,
  8. mold: mold
  9. },
  10. success: function (res) {
  11. if (res.error_code === 0 && res.data) {
  12. callback && callback(res.data)
  13. } else {
  14. if (res.error_msg) {
  15. showToast(res.error_msg)
  16. }
  17. }
  18. }
  19. })
  20. },
  21. verifyCaptcha: function(data, callback) {
  22. $.ajax({
  23. type: 'POST',
  24. url: '/publicapply/captcha/check',
  25. data: data,
  26. success: function (res) {
  27. callback && callback(res)
  28. }
  29. })
  30. }
  31. }
  32. var slideVerify = {
  33. el: null,
  34. $dialog: null,
  35. capt: null,
  36. phone: '',
  37. captKey: '',
  38. init: function() {
  39. if (!this.capt) {
  40. this.$dialog = $('#pc-slide-verify-dialog')
  41. this.initSlideVerify()
  42. }
  43. this.modalShow(true)
  44. },
  45. initModalZIndex: function() {
  46. // 修复首页多层弹窗层级问题
  47. if (!this.$dialog) return
  48. var modalInstance = this.$dialog.data('bs.modal')
  49. modalInstance.$backdrop.addClass('top-of-login')
  50. },
  51. initSlideVerify: function() {
  52. var el = document.getElementById('slide-wrap');
  53. var capt = new GoCaptcha.Slide({
  54. width: 300,
  55. height: 220,
  56. })
  57. capt.mount(el)
  58. this.el = el
  59. this.capt = capt
  60. this.bindCaptEvents()
  61. },
  62. modalShow: function(f) {
  63. if (!this.$dialog) return
  64. if (f) {
  65. this.$dialog.modal('show')
  66. this.initModalZIndex()
  67. } else {
  68. this.$dialog.modal('hide')
  69. }
  70. },
  71. bindCaptEvents: function() {
  72. if (!this.capt) return
  73. var _this = this
  74. this.capt.setEvents({
  75. close: function() {
  76. _this.modalShow(false)
  77. },
  78. confirm: function(point, reset) {
  79. const payload = {
  80. phone: _this.phone,
  81. key: _this.captKey,
  82. point: [point.x, point.y].join(',')
  83. }
  84. SMSRequest.verifyCaptcha(payload, function(res) {
  85. if (res.error_code === 0 && res.data) {
  86. const pass = res.data && res.data.code === 0
  87. if (pass) {
  88. try {
  89. startSendSMSCodeTimer(_this.mold)
  90. } catch (error) {
  91. console.log(error)
  92. }
  93. _this.modalShow(false)
  94. reset()
  95. } else {
  96. reset()
  97. _this.capt.refresh()
  98. }
  99. } else {
  100. reset()
  101. _this.capt.refresh()
  102. }
  103. })
  104. },
  105. refresh: function() {
  106. SMSRequest.getCaptcha(_this.phone, _this.mold, function(data) {
  107. if (data && data.code === 1) {
  108. _this.refreshCaptData(data)
  109. }
  110. })
  111. },
  112. })
  113. },
  114. cachePhone: function(phone) {
  115. this.phone = phone
  116. },
  117. cacheMold: function(mold) {
  118. this.mold = mold
  119. },
  120. cacheCaptKey: function(k) {
  121. this.captKey = k
  122. },
  123. refreshCaptData: function(x) {
  124. if (!this.capt) return
  125. const captKey = x.captcha_key || ''
  126. this.cacheCaptKey(captKey)
  127. this.capt.setData({
  128. image: x.image_base64 || '',
  129. thumb: x.tile_base64 || '',
  130. captKey: captKey,
  131. thumbX: x.tile_x || 0,
  132. thumbY: x.tile_y || 0,
  133. thumbWidth: x.tile_width || 0,
  134. thumbHeight: x.tile_height || 0
  135. })
  136. }
  137. }
  138. function initGoCaptchaVerify(phone, mold) {
  139. SMSRequest.getCaptcha(phone, mold, function(data) {
  140. if (data && data.code === 1) {
  141. slideVerify.init()
  142. slideVerify.cachePhone(phone)
  143. slideVerify.cacheMold(mold)
  144. slideVerify.refreshCaptData(data)
  145. }
  146. })
  147. }
  148. function getCaptchaInfo() {
  149. return {
  150. phone: slideVerify.phone,
  151. captKey: slideVerify.captKey
  152. }
  153. }