123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import { getPhonePrefix, getSmsCode, loginByPhone } from '../../api/user'
- import { getStorageSync, setStorageSync, showToast, navigateTo, validator, navigateBack, reLaunch } from '../../utils/util'
- const perfixDefault = ['86', '086', '+86', '+086']
- Page({
- data: {
- isLoading: true,
- prefixArr: [],
- prefixIndex: 0,
- mobile: '',
- verifyCode: '',
- codeText: '获取验证码',
- btnCodeDisable: false,
- btnSureDisable: true,
- timer: null
- },
- onLoad: function (options) {
- const prefixArr = getStorageSync('MOBILEPREFIXDATA')
- if (prefixArr) {
- const prefixIndex = prefixArr.findIndex(item => perfixDefault.indexOf(item.code) !== -1)
- this.setData({ prefixArr, prefixIndex })
- } else {
- this.loadPrefix()
- }
- },
- onUnload: function () {
- const { timer } = this.data
- if (timer) {
- clearTimeout(timer)
- this.data.timer = null
- }
- },
- tapAreaCodeAction: function () {
- const { prefixArr, prefixIndex } = this.data
- navigateTo('/pages/areaCode/areaCode?id=' + prefixArr[prefixIndex].id)
- },
- // 区号选中后的回调方法
- areaCodeCallback(res) {
- if (res && res.prefixIndex) {
- this.setData({
- prefixIndex: res.prefixIndex
- })
- }
- },
- mobileChange: function (e) {
- const mobile = e.detail.value
- this.setData({
- mobile: mobile
- })
- const { code } = this.data
- if (validator.checkMobile(mobile) && validator.checkCode(code)) {
- this.setData({
- btnSureDisable: false
- })
- } else {
- this.setData({
- btnSureDisable: true
- })
- }
- },
- codeChange: function (e) {
- const verifyCode = e.detail.value
- this.setData({
- verifyCode: verifyCode
- })
- const { mobile } = this.data
- if (validator.checkMobile(mobile) && validator.checkCode(verifyCode)) {
- this.setData({
- btnSureDisable: false
- })
- } else {
- this.setData({
- btnSureDisable: true
- })
- }
- },
- handleSendCode: function (e) {
- const { mobile, prefixArr, prefixIndex } = this.data
- if (!validator.checkMobile(mobile)) {
- showToast({
- title: '请输入正确的手机号码'
- })
- return false
- }
- const countryId = prefixArr[prefixIndex].id
- if (!this.data.btnCodeDisable) {
- this.sendSmsCode(countryId, mobile)
- }
- },
- handleSure: function (e) {
- const { mobile, prefixArr, prefixIndex, verifyCode } = this.data
- const params = {
- verifyCode,
- mobile,
- mobilePrefixId: prefixArr[prefixIndex].id
- }
- this.setData({
- btnSureDisable: true
- })
- loginByPhone(params).then((result) => {
- const data = result.data
- if (data && data.userInfo && data.userInfo.token) {
- setStorageSync('USERINFO', data.userInfo)
- this.redirect()
- }
- }).catch((err) => {
- this.setData({
- btnSureDisable: false
- })
- showToast({
- title: err.msg
- })
- })
- },
- redirect: function() {
- // 在内存中的所有页面栈对象
- const pages = getCurrentPages()
- // 上上个页面
- const prePage = pages[pages.length - 3]
- if (prePage) {
- prePage.loginCallback && prePage.loginCallback()
- navigateBack(2)
- } else {
- reLaunch('/pages/index/index')
- }
- },
- loadPrefix: function () {
- const params = {
- range: 'B',
- prefix: '1'
- }
- getPhonePrefix(params).then((result) => {
- const data = result.data
- if (!Array.isArray(data) || data.length === 0) {
- showToast({ title: '网络不稳定,请重新操作' })
- return
- }
- // 二维转一维数组
- const prefixArr = data.reduce((res, cur) => {
- return res.concat(cur.list)
- }, [])
- const prefixIndex = prefixArr.findIndex(item => perfixDefault.indexOf(item.code) !== -1)
- this.setData({
- prefixArr,
- prefixIndex,
- isLoading: false
- })
- setStorageSync('MOBILEPREFIXDATA', prefixArr)
- }).catch((err) => {
- showToast({ title: '网络不稳定,请重新操作' })
- })
- },
- sendSmsCode: function (countryId, mobile) {
- const params = {
- mobilePrefixId: countryId,
- hooid: "hoolihome_liuxue",
- mobile: mobile,
- senceId: 1,
- status: 5
- }
- getSmsCode(params).then((result) => {
- this.countDown()
- this.setData({
- btnCodeDisable: true
- })
- }).catch((err) => {
- this.setData({
- btnCodeDisable: false
- })
- showToast({
- title: '发送验证码失败'
- })
- })
- },
- countDown: function () {
- /*短信验证码倒计时*/
- let timeCount = 60
- let loop = () => {
- if (timeCount > 0) {
- this.setData({
- codeText: `重新获取(${timeCount}s)`
- })
- timeCount--
- this.data.timer = setTimeout(loop, 1000)
- } else {
- this.setData({
- codeText: `重新获取`,
- btnCodeDisable: false
- })
- if (this.data.timer) {
- clearTimeout(this.data.timer)
- this.data.timer = null
- }
- }
- }
- loop()
- }
- })
|