editPhone.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const app = getApp();
  2. Page({
  3. data: {
  4. canClick: false,
  5. codeBtnTxt: "发送验证码",
  6. loginName: '',
  7. code:'',
  8. },
  9. onLoad(options){
  10. this.setData({
  11. userId:options.userId
  12. })
  13. },
  14. setLoginName: function (e) { //手机号输入校验
  15. var loginName = e.detail.value.replace(/[^\d]/g, "").substr(0, 11)
  16. //验证手机号
  17. var myreg = /^[1][0-9]{10}$/;
  18. this.setData({
  19. loginName,
  20. canClick: myreg.test(loginName)
  21. })
  22. },
  23. setCode: function (e) { //验证码输入绑定
  24. this.setData({
  25. code: e.detail.value.replace(/[^\d]/g, "").substr(0, 6)
  26. })
  27. },
  28. getValidCode: function (e) {
  29. //验证手机号
  30. var myreg = /^[1][0-9]{10}$/;
  31. if (!myreg.test(this.data.loginName)) {
  32. app.showToptip(this, "worning", "手机号输入有误")
  33. return;
  34. }
  35. var that = this
  36. wx.showLoading({
  37. title: '发送中...',
  38. mask: true
  39. })
  40. app.requestP({
  41. url: "/sms/getSmsCodeByMobile",
  42. method: 'post',
  43. contentType: "application/json",
  44. needToken: false,
  45. data: {
  46. mobile: that.data.loginName
  47. }
  48. }).then((res) => {
  49. app.showToptip(that, "success", "发送成功")
  50. that.data.lastSendTime = Date.now()
  51. that.setData({
  52. canClick: false
  53. })
  54. //定时器触发倒计时,
  55. var times = 60
  56. var i = setInterval(function () {
  57. times--
  58. if (times == 0) {
  59. that.setData({
  60. canClick: true,
  61. codeBtnTxt: "获取验证码",
  62. })
  63. clearInterval(i)
  64. } else {
  65. that.setData({
  66. codeBtnTxt: "重新获取" + times + "s",
  67. })
  68. }
  69. }, 1000)
  70. }).catch(function (r) {
  71. app.showToptip(that, "error", r.data.msg)
  72. })
  73. },
  74. editPhone(){
  75. var that = this;
  76. var phoneNumber = that.data.loginName;
  77. var phonereg = /^[1][0-9]{10}$/;
  78. var userId = thiat.data.userId;
  79. if (!phonereg.test(phoneNumber)) {
  80. app.showToptip(that, "worning", "手机号输入有误")
  81. return;
  82. }
  83. var smsCode = that.data.code
  84. if (!smsCode) {
  85. app.showToptip(that, "worning", "验证码输入有误")
  86. return;
  87. }
  88. wx.showLoading({
  89. mask: true
  90. })
  91. app.requestP({
  92. url:'/user/editPhoneNumber',
  93. method:'post',
  94. contentType:' application/json',
  95. data:{
  96. phonenumber,
  97. smsCode,
  98. userId,
  99. }
  100. }).then(res=>{
  101. wx.removeStorageSync("token")
  102. wx.redirectTo({
  103. url: '/pages/login/login?phoneNumber='+phoneNumber
  104. })
  105. wx.redirectTo({
  106. url: '/pages/login/login',
  107. })
  108. }).error(err=>{
  109. app.showToptip(that, "worning", err.msg)
  110. })
  111. }
  112. })