// pages/login/login.js
var app = getApp();
const util = require('../../utils/util.js');
const WebIM = require("../../utils/WebIM")["default"];
const CusBase64 = require('../../utils/base64.js');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgPath: app.globalData.imgPath,
        app_title1: '重庆市城市管理局',
        app_title2: '指挥大厅轻应用',
        phone: '',
        vCode: '',
        sendText: '发送验证码',
        disabled: false,
        grant_type: "password",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 手机号赋值
     */
    setPhone: function (e) {
        this.setData({
            phone: e.detail.value
        });
    },

    /**
     * 验证码赋值
     */
    setVcode: function (e) {
        this.setData({
            vCode: e.detail.value
        });
    },

    /**
     * 获取验证码
     */
    getVerifyCode: function () {
        let that = this;
        let disabled = that.data.disabled;
        if (disabled) {
            return;
        }
        let phone = this.data.phone;
        if (!phone) {
            util.toast('请输入手机号', 'error');
            return false;
        }
        let reg = /1[3-9]\d{9}/;
        if (!reg.test(phone)) {
            util.toast('手机号格式有误', 'error');
            return false;
        }

        that.setData({
            sendText: '60s后再次发送',
            disabled: true
        });
        // 再次发送短信时间倒计时
        that.timeTask();

        util.post({
            url: '/api/sms/sendSms/' + that.data.phone,
            success: (res) => {
                wx.hideLoading();
                if (res.code != 200) {
                    util.toast(res.data.msg);
                } else {
                    util.toast(res.data.msg);
                }
            },
        });
    },

    /**
     * 60s倒计时定时器
     */
    timeTask: function () {
        let that = this;
        let i = 60;
        let timer = setInterval(function () {
            i--;
            if (i < 1) {
                that.setData({
                    sendText: '发送验证码',
                    disabled: false
                });
                clearInterval(timer);
            } else {
                that.setData({
                    sendText: i + 's后再次发送'
                });
            }
        }, 1000);
    },

    /**
     * 登录系统
     */
    loginSys: function (e) {
        let that = this;
        let values = e.detail.value;
        if (!values.phone) {
            util.toast('请输入手机号', 'error');
            return false;
        }
        let reg = /1[3-9]\d{9}/;
        if (!reg.test(values.phone)) {
            util.toast('手机号格式有误', 'error');
            return false;
        }
        if (!values.code) {
            util.toast('请输入验证码', 'error');
            return false;
        }
        values.type = 'CAPTCHA';

        util.post({
            url: '/login/mobile',
            data: values,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: (res) => {
                wx.hideLoading();
                let code = res.data.code;
                if ('200' != code) {
                    util.toast(res.data.msg);
                } else {
                    app.globalData.Authorization = res.data.data.Authorization;
                    that.getUserInfo();
                }
            }
        });
    },

    /**
     * 登录验证通过后获取用户信息和角色信息
     */
    getUserInfo: function () {
        let that = this;
        util.get({
            url: '/sys/userInfoAndRole',
            success: (res) => {
                wx.hideLoading();
                if (res.data.code != 200) {
                    util.toast(res.data.msg);
                } else {
                    let datas = res.data.data;
                    app.globalData.userInfo = datas;
                    app.globalData.roleInfo = datas.roleList[0];

                    wx.setStorage({
                        key: "myUsername",
                        data: datas.phone
                    });
                    app.conn.open({
                        // apiUrl: WebIM.config.apiURL,
                        user: datas.phone,
                        pwd: CusBase64.CusBASE64.encoder(datas.phone),
                        grant_type: that.data.grant_type,
                        appKey: WebIM.config.appkey
                    });
                }
            }
        });
    },

    /**
     * 微信获取手机号快捷登录
     */
    getPhoneNumber: function (res) {
        let that = this;
        if (!res.detail.code) {
            util.toast('请升级微信版本');
            return;
        }
        //用户按了允许授权按钮
        if (res.detail.encryptedData) {
            util.post({
                url: '/login/mobile',
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                    code: res.detail.code,
                    type: 'QUICK'
                },
                success: (res) => {
                    wx.hideLoading();
                    if (res.data.code != 200) {
                        util.toast(res.data.msg);
                    } else {
                        app.globalData.Authorization = res.data.data.Authorization;
                        that.getUserInfo();
                    }
                }
            });
        } else {//用户按了拒绝按钮
            wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
                showCancel: false,
                confirmText: '返回授权',
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”');
                    }
                }
            });
        }
    },
})