bindMobile.js 8.06 KB
import wx from '../../utils/wx';
import util from '../../utils/util';
import event from '../../common/event';
import { getVerifyCode, bindMobileAction, checkIsFromClock } from '../../common/login';
import Yas from '../../common/yas';

let app = getApp();
let yas;

Page({
    data: {
        areaCode: '86',
        phoneNum: '',
        smsCode: '',
        btnText: '获取验证码',
        timerSec: 60,
        counting: false,
        regState: 0,
        activeClass: '',
        completeClass: '',
        areaName: '中国',
        hasUnionID: false,
        isSubmitting: false,
        autoBtnText: '自动验证',
        degrees: '',
        isNeedImgCheck: false,
        refreshImgCheckStatus: false,
        backNum: 1
    },
    onLoad: function(query) {
      
        if(query.backNum) {
          this.setData({
            backNum: +query.backNum
          })
        }
        const {phone, area, url} = query || {};

        app = app || getApp();

        yas = new Yas(app); // 实例化埋点
        yas.pageOpenReport(); // 进入页面后 上报
        if (+area === 86 && phone) {
            this.setData({
                phoneNum: phone,
                activeClass: 'active'
            });
        }

        event.on('choose-area', params => {
            this.setData({
                areaCode: params.code,
                areaName: params.name
            });
        });

        this.setData({
            hasUnionID: !!(app && app.getUnionID()),
            refer: url || ''
        });
    },
    onShow: function() {

    },
    onReady: function() {
        app = app || getApp();
    },
    phoneInput: function(e) {
        this.setData({
            phoneNum: e.detail.value.trim(),
            activeClass: e.detail.value.trim() ? 'active' : '',
            completeClass: (this.data.smsCode && e.detail.value.trim()) ? 'active' : ''
        });
    },
    codeInput: function(e) {
        this.setData({
            smsCode: e.detail.value.trim(),
            completeClass: (this.data.phoneNum && e.detail.value.trim()) ? 'active' : ''
        });
    },
    sendCode: function() {
        if (!this.data.phoneNum || this.data.counting) {
            return;
        }

        // 检查是否需要图片验证码
        this.setData({
            refreshImgCheckStatus: !this.data.refreshImgCheckStatus
        });

        // 又需要图片验证码了,但是没有验证
        if (this.data.isNeedImgCheck && !this.data.degrees) {
            wx.showModal({
                title: '提示',
                content: '请完成图形验证',
                showCancel: false
            });
            return;
        }

        if (this.checkPhoneFormat()) {
            return getVerifyCode(this.data.areaCode, this.data.phoneNum, this.data.degrees).then(res => {
                if (res.code === 200) {
                    this.countDown();
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.message || '验证码发送失败',
                        showCancel: false
                    });
                }
            }).catch(() => {
                wx.showModal({
                    title: '提示',
                    content: '验证码发送失败',
                    showCancel: false
                });
            });
        }

        wx.showModal({
            title: '提示',
            content: '手机号码格式不正确',
            showCancel: false
        });
    },

    // 倒计时
    countDown: function() {
        let seconds = 60;
        let timer = setInterval(() => {
            seconds--;
            if (seconds < 0) {
                this.setData({
                    btnText: '重新获取验证码',
                    timerSec: 60,
                    counting: false,
                });
                clearInterval(timer);
            } else {
                this.setData({
                    btnText: `${seconds}s`,
                    timerSec: seconds,
                    counting: true,
                });
            }
        }, 1000);

        this.setData({
            btnText: '60s',
            timerSec: 60,
            counting: true,
        });
    },
    submitTap: function() {
      let that = this
        if (!this.data.completeClass) {
            return;
        }

        if (!this.data.smsCode || !this.data.phoneNum) {
            return wx.showModal({
                title: '提示',
                content: '手机号码和验证码都不能为空',
                showCancel: false
            });
        }

        if (!this.checkPhoneFormat()) {
            return wx.showModal({
                title: '提示',
                content: '手机号码格式不正确',
                showCancel: false
            });
        }

        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 10000
        });

        if (this.data.isSubmitting) {
            return;
        }

        this.data.isSubmitting = true;
        bindMobileAction(this.data.areaCode, this.data.phoneNum, this.data.smsCode)
            .then((res => {
                wx.hideToast();
                this.data.isSubmitting = false;

                if (res.code === 200) {
                    let userInfo = {};
                    const {prePath} = util.getPathInfo();

                    userInfo.uid = res.data.uid;
                    userInfo.is_bind = res.data.is_bind || '';
                    userInfo.mobile = res.data.profile;
                    userInfo.ssouid = res.data.ssouid;
                    userInfo.sessionKey = res.data.session_key;

                    app._removeSync('disableAutoLogin');
                    app.setUserInfo(userInfo);

                    // 新注册用户
                    if (res.data.is_register === 0 || res.data.nFlag === 'Y') {
                        event.emit('yas-user-register-success', {
                            REG_TYPE: 5,
                            TRIGGER_PAGE_NAME: prePath,
                            IS_SIGN: checkIsFromClock() ? 1 : 2
                        });
                    }
                      wx.navigateBack({ // 先执行这个,然后在执行event
                        delta: that.data.backNum
                      })
                    
                    event.emit('yas-login-type-report', { LOGIN_TYPE: 5 });
                    event.emit('user-login-success');
                    event.emit('user-login-callback');
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.message || '服务器错误',
                        showCancel: false
                    });
                }
            }))
            .catch(() => {
                this.data.isSubmitting = false;
                wx.showModal({
                    title: '提示',
                    content: '服务器错误',
                    showCancel: false
                });
                wx.hideToast();
            });
    },
    checkPhoneFormat: function() {

        // 手机号码验证规则
        let phoneRegx = {
            '+86': /^1[0-9]{10}$/,
            '+852': /^[965]{1}[0-9]{7}$/,
            '+853': /^[0-9]{8}$/,
            '+886': /^[0-9]{10}$/,
            '+65': /^[98]{1}[0-9]{7}$/,
            '+60': /^1[1234679]{1}[0-9]{8}$/,
            '+1': /^[0-9]{10}$/,
            '+82': /^01[0-9]{9}$/,
            '+44': /^7[789][0-9]{8}$/,
            '+81': /^0[9|8|7][0-9]{9}$/,
            '+61': /^[0-9]{11}$/
        };

        return phoneRegx[`+${this.data.areaCode}`].test(this.data.phoneNum);
    },
    chooseArea: function() {
        wx.navigateTo({
            url: '/pages/account/chooseArea'
        });
    },

    /**
     * 监听验证码组件
     */
    onRefreshCode: function(e) {
        this.setData({
            degrees: e.detail.degrees.join(',')
        });
    },

    /**
     * 监听是否需要图形验证码
     */
    onIsNeedImgCheck: function(e) {
        this.setData({
            isNeedImgCheck: e.detail.isNeedImgCheck
        });
    }
});