mobile.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var api = require('../../../config/api.js');
  2. var util = require('../../../utils/util.js');
  3. var app = getApp()
  4. Page({
  5. data: {
  6. mobile: '',
  7. userInfo: {
  8. avatarUrl: '',
  9. nickName: ''
  10. },
  11. disableGetMobileCode: false,
  12. disableSubmitMobileCode: true,
  13. getCodeButtonText: '获取验证码'
  14. },
  15. onShow: function () {
  16. },
  17. onLoad: function () {
  18. var that = this
  19. that.setData({userInfo: app.globalData.userInfo})
  20. if (app.globalData.token) {
  21. } else {
  22. var token = wx.getStorageSync('userToken')
  23. if (token) {
  24. app.globalData.token = token
  25. }
  26. }
  27. },
  28. bindCheckMobile: function (mobile) {
  29. if (!mobile) {
  30. wx.showModal({
  31. title: '温馨提示',
  32. content: '请输入手机号码'
  33. });
  34. return false
  35. }
  36. if (!mobile.match(/^1[3-9][0-9]\d{8}$/)) {
  37. wx.showModal({
  38. title: '温馨提示',
  39. content: '手机号格式不正确,仅支持国内手机号码'
  40. });
  41. return false
  42. }
  43. return true
  44. },
  45. bindGetPassCode: function (e) {
  46. var that = this
  47. that.setData({disableGetMobileCode: true})
  48. },
  49. bindInputMobile: function (e) {
  50. this.setData({
  51. mobile: e.detail.value,
  52. })
  53. },
  54. countDownPassCode: function () {
  55. if (!this.bindCheckMobile(this.data.mobile)) {
  56. return
  57. }
  58. util.request(api.SmsCode, {phone: this.data.mobile}, 'POST', 'application/json')
  59. .then(function (res) {
  60. if (res.errno == 0) {
  61. wx.showToast({
  62. title: '发送成功',
  63. icon: 'success',
  64. duration: 1000
  65. })
  66. var pages = getCurrentPages()
  67. var i = 60;
  68. var intervalId = setInterval(function () {
  69. i--
  70. if (i <= 0) {
  71. pages[pages.length - 1].setData({
  72. disableGetMobileCode: false,
  73. disableSubmitMobileCode: false,
  74. getCodeButtonText: '获取验证码'
  75. })
  76. clearInterval(intervalId)
  77. } else {
  78. pages[pages.length - 1].setData({
  79. getCodeButtonText: i,
  80. disableGetMobileCode: true,
  81. disableSubmitMobileCode: false
  82. })
  83. }
  84. }, 1000);
  85. } else {
  86. wx.showToast({
  87. title: '发送失败',
  88. icon: 'none',
  89. duration: 1000
  90. })
  91. }
  92. });
  93. },
  94. bindLoginMobilecode: function (e) {
  95. var mobile = this.data.mobile;
  96. if (this.data.disableSubmitMobileCode){
  97. return
  98. }
  99. if (!this.bindCheckMobile(mobile)) {
  100. return
  101. }
  102. if (!(e.detail.value.code && e.detail.value.code.length === 4)) {
  103. return
  104. }
  105. wx.showToast({
  106. title: '操作中...',
  107. icon: 'loading',
  108. duration: 5000
  109. })
  110. util.request(api.BindMobile, { code: e.detail.value.code, mobile: mobile}, "POST", "application/form-data")
  111. .then(function (res) {
  112. if (res.errno === 0) {
  113. wx.showModal({
  114. title: '提示',
  115. content: '操作成功',
  116. showCancel: false
  117. })
  118. wx.switchTab({
  119. url: '/pages/ucenter/index/index'
  120. });
  121. } else {
  122. wx.showModal({
  123. title: '提示',
  124. content: '验证码错误',
  125. showCancel: false
  126. })
  127. }
  128. })
  129. }
  130. })