loginMobeil.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { getPhonePrefix, getSmsCode, loginByPhone } from '../../api/user'
  2. import { getStorageSync, setStorageSync, showToast, navigateTo, validator, navigateBack, reLaunch } from '../../utils/util'
  3. const perfixDefault = ['86', '086', '+86', '+086']
  4. Page({
  5. data: {
  6. isLoading: true,
  7. prefixArr: [],
  8. prefixIndex: 0,
  9. mobile: '',
  10. verifyCode: '',
  11. codeText: '获取验证码',
  12. btnCodeDisable: false,
  13. btnSureDisable: true,
  14. timer: null
  15. },
  16. onLoad: function (options) {
  17. const prefixArr = getStorageSync('MOBILEPREFIXDATA')
  18. if (prefixArr) {
  19. const prefixIndex = prefixArr.findIndex(item => perfixDefault.indexOf(item.code) !== -1)
  20. this.setData({ prefixArr, prefixIndex })
  21. } else {
  22. this.loadPrefix()
  23. }
  24. },
  25. onUnload: function () {
  26. const { timer } = this.data
  27. if (timer) {
  28. clearTimeout(timer)
  29. this.data.timer = null
  30. }
  31. },
  32. tapAreaCodeAction: function () {
  33. const { prefixArr, prefixIndex } = this.data
  34. navigateTo('/pages/areaCode/areaCode?id=' + prefixArr[prefixIndex].id)
  35. },
  36. // 区号选中后的回调方法
  37. areaCodeCallback(res) {
  38. if (res && res.prefixIndex) {
  39. this.setData({
  40. prefixIndex: res.prefixIndex
  41. })
  42. }
  43. },
  44. mobileChange: function (e) {
  45. const mobile = e.detail.value
  46. this.setData({
  47. mobile: mobile
  48. })
  49. const { code } = this.data
  50. if (validator.checkMobile(mobile) && validator.checkCode(code)) {
  51. this.setData({
  52. btnSureDisable: false
  53. })
  54. } else {
  55. this.setData({
  56. btnSureDisable: true
  57. })
  58. }
  59. },
  60. codeChange: function (e) {
  61. const verifyCode = e.detail.value
  62. this.setData({
  63. verifyCode: verifyCode
  64. })
  65. const { mobile } = this.data
  66. if (validator.checkMobile(mobile) && validator.checkCode(verifyCode)) {
  67. this.setData({
  68. btnSureDisable: false
  69. })
  70. } else {
  71. this.setData({
  72. btnSureDisable: true
  73. })
  74. }
  75. },
  76. handleSendCode: function (e) {
  77. const { mobile, prefixArr, prefixIndex } = this.data
  78. if (!validator.checkMobile(mobile)) {
  79. showToast({
  80. title: '请输入正确的手机号码'
  81. })
  82. return false
  83. }
  84. const countryId = prefixArr[prefixIndex].id
  85. if (!this.data.btnCodeDisable) {
  86. this.sendSmsCode(countryId, mobile)
  87. }
  88. },
  89. handleSure: function (e) {
  90. const { mobile, prefixArr, prefixIndex, verifyCode } = this.data
  91. const params = {
  92. verifyCode,
  93. mobile,
  94. mobilePrefixId: prefixArr[prefixIndex].id
  95. }
  96. this.setData({
  97. btnSureDisable: true
  98. })
  99. loginByPhone(params).then((result) => {
  100. const data = result.data
  101. if (data && data.userInfo && data.userInfo.token) {
  102. setStorageSync('USERINFO', data.userInfo)
  103. this.redirect()
  104. }
  105. }).catch((err) => {
  106. this.setData({
  107. btnSureDisable: false
  108. })
  109. showToast({
  110. title: err.msg
  111. })
  112. })
  113. },
  114. redirect: function() {
  115. // 在内存中的所有页面栈对象
  116. const pages = getCurrentPages()
  117. // 上上个页面
  118. const prePage = pages[pages.length - 3]
  119. if (prePage) {
  120. prePage.loginCallback && prePage.loginCallback()
  121. navigateBack(2)
  122. } else {
  123. reLaunch('/pages/index/index')
  124. }
  125. },
  126. loadPrefix: function () {
  127. const params = {
  128. range: 'B',
  129. prefix: '1'
  130. }
  131. getPhonePrefix(params).then((result) => {
  132. const data = result.data
  133. if (!Array.isArray(data) || data.length === 0) {
  134. showToast({ title: '网络不稳定,请重新操作' })
  135. return
  136. }
  137. // 二维转一维数组
  138. const prefixArr = data.reduce((res, cur) => {
  139. return res.concat(cur.list)
  140. }, [])
  141. const prefixIndex = prefixArr.findIndex(item => perfixDefault.indexOf(item.code) !== -1)
  142. this.setData({
  143. prefixArr,
  144. prefixIndex,
  145. isLoading: false
  146. })
  147. setStorageSync('MOBILEPREFIXDATA', prefixArr)
  148. }).catch((err) => {
  149. showToast({ title: '网络不稳定,请重新操作' })
  150. })
  151. },
  152. sendSmsCode: function (countryId, mobile) {
  153. const params = {
  154. mobilePrefixId: countryId,
  155. hooid: "hoolihome_liuxue",
  156. mobile: mobile,
  157. senceId: 1,
  158. status: 5
  159. }
  160. getSmsCode(params).then((result) => {
  161. this.countDown()
  162. this.setData({
  163. btnCodeDisable: true
  164. })
  165. }).catch((err) => {
  166. this.setData({
  167. btnCodeDisable: false
  168. })
  169. showToast({
  170. title: '发送验证码失败'
  171. })
  172. })
  173. },
  174. countDown: function () {
  175. /*短信验证码倒计时*/
  176. let timeCount = 60
  177. let loop = () => {
  178. if (timeCount > 0) {
  179. this.setData({
  180. codeText: `重新获取(${timeCount}s)`
  181. })
  182. timeCount--
  183. this.data.timer = setTimeout(loop, 1000)
  184. } else {
  185. this.setData({
  186. codeText: `重新获取`,
  187. btnCodeDisable: false
  188. })
  189. if (this.data.timer) {
  190. clearTimeout(this.data.timer)
  191. this.data.timer = null
  192. }
  193. }
  194. }
  195. loop()
  196. }
  197. })