safe.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var validator = require('../../../../utils/validator.js')
  2. var _request = require('../../../../utils/request.js')
  3. Page({
  4. data: {
  5. tel: 0,
  6. yzm: '',
  7. countdown: 0,
  8. pwd: '',
  9. repwd: ''
  10. },
  11. onLoad: function (options) {
  12. this.getTel()
  13. this.startCountdown()
  14. },
  15. bindYzm (e) {
  16. this.setData({
  17. yzm: e.detail.value
  18. })
  19. },
  20. bindPwd (e) {
  21. this.setData({
  22. pwd: e.detail.value
  23. })
  24. },
  25. bindRepwd (e) {
  26. this.setData({
  27. repwd: e.detail.value
  28. })
  29. },
  30. getYzm () {
  31. if (validator.telphone(this.data.tel)) {
  32. this.requireYzm()
  33. } else {
  34. wx.showToast({
  35. title: '手机号码格式不正确',
  36. icon: 'none',
  37. duration: 2000
  38. })
  39. }
  40. },
  41. requireYzm () {
  42. var that = this
  43. var url = 'v1/code/send'
  44. var params = {
  45. tel: this.data.tel,
  46. valid_type: 'reset_trade_pwd'
  47. }
  48. var success = function (res) {
  49. wx.setStorageSync('sms_request_time', Date.now())
  50. that.startCountdown()
  51. }
  52. _request.$post(url, params, success)
  53. },
  54. startCountdown: function () {
  55. var smsRequestTime = wx.getStorageSync('sms_request_time')
  56. if (smsRequestTime) {
  57. var count = parseInt((60000 - (new Date() - smsRequestTime)) / 1000)
  58. this.setData({
  59. countdown: count
  60. })
  61. if (this.data.countdown > 0) {
  62. var timer = setInterval(() => {
  63. var countdown = this.data.countdown - 1
  64. this.setData({
  65. countdown: countdown
  66. })
  67. if (this.data.countdown <= 0) {
  68. clearInterval(timer)
  69. }
  70. }, 1000)
  71. }
  72. } else {
  73. }
  74. },
  75. getTel () {
  76. var that = this
  77. var url = 'v1/user/info'
  78. var params = {
  79. }
  80. var success = function (res) {
  81. var data = res.data.user.tel
  82. that.setData({
  83. tel: data
  84. })
  85. }
  86. _request.$get(url, params, success)
  87. },
  88. requireUpdate () {
  89. var that = this
  90. var url = 'v1/user/trade_pwd'
  91. var params = {
  92. tel: this.data.tel,
  93. code: this.data.yzm,
  94. pwd: this.data.pwd,
  95. confirmed_pwd: this.data.repwd
  96. }
  97. var success = function (res) {
  98. wx.navigateBack({
  99. delta: 1
  100. })
  101. }
  102. _request.$put(url, params, success)
  103. },
  104. setUpdate () {
  105. var { isOk, msg } = this.validate()
  106. if (isOk) {
  107. this.requireUpdate()
  108. } else {
  109. wx.showToast({
  110. title: msg,
  111. icon: 'none',
  112. duration: 2000
  113. })
  114. }
  115. },
  116. validate () {
  117. var msg
  118. if (!validator.required(this.data.tel)) {
  119. msg = '手机不能为空'
  120. } else if (!validator.telphone(this.data.tel)) {
  121. msg = '手机号码格式不正确'
  122. } else if (!validator.required(this.data.yzm)) {
  123. msg = '验证码不能为空'
  124. } else if (!validator.required(this.data.pwd)) {
  125. msg = '密码不能为空'
  126. } else if (!validator.required(this.data.repwd)) {
  127. msg = '确认密码不能为空'
  128. } else if (!validator.minLen(this.data.pwd, 6)) {
  129. msg = '密码的长度是6-20位'
  130. } else if (!validator.maxLen(this.data.pwd, 20)) {
  131. msg = '密码的长度是6-20位'
  132. } else if (!validator.minLen(this.data.repwd, 6)) {
  133. msg = '确认密码的长度是6-20位'
  134. } else if (!validator.maxLen(this.data.repwd, 20)) {
  135. msg = '确认密码的长度是6-20位'
  136. } else if (!validator.confirmed(this.data.pwd, this.data.repwd)) {
  137. msg = '两次密码不一致'
  138. }
  139. return { isOk: !msg, msg }
  140. },
  141. onShareAppMessage: function (val) {
  142. return _request.share({
  143. sc: 'xcx_user_safe'
  144. })
  145. }
  146. })