Newer
Older
smart_construction / miniprogram / pages / validNameKeyword / validNameKeyword.js
dutingting on 28 Jul 2023 5 KB 增加权限控制
// pages/validNameKeyword/validNameKeyword.js
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const defaultAvatarUrl = '../../images/login.png'
var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        nickname: '', // 昵称
        phone: '', //用户名-手机号
        password: '', // 密码
        telMessage: '', // 手机号验证提示信息
        keywordMessage: '', //密码
        avatarUrl: defaultAvatarUrl, //头像
    },

    // 授权头像
    onChooseAvatar(e) {
        const { avatarUrl } = e.detail 
        this.setData({
            avatarUrl,
        })
    },
    // 校验手机号
    telChange: function (event) {
        const phone = event.detail || event;
        let message = '';
        let disable = '';
        if (phone) {
        if (/^1(3|4|5|7|8)\d{9}$/.test(phone)) {
            message = '';
            disable = false;
        } else {
            message = '您输入的手机号码有误';
            disable = true;
        }
        } else {
        message = '账号不能为空',
        disable = true
        }
        this.setData({
            telMessage: message,
            disabled: disable,
            txn_tel: phone
        });
        if (this.data.disabled === true) {
        return false;
        }else {
        return true;
        }
    },
    keywordChange: function (event) {
        const password = event.detail || event;
        let message = '';
        let disable = '';
        if (!password) {
            message = '密码不能为空',
            disable = true
        }
        this.setData({
            keywordMessage: message,
            disabled: disable,
        });
        if (this.data.disabled === true) {
        return false;
        }else {
        return true;
        }
    },
    // 点击确认
    confirm: function (e) {
        const { phone, password, nickname, avatarUrl} = this.data 
        var that = this
        console.log(nickname);
        console.log('00000', this.data  )
        if(!nickname) {
            wx.showToast({
              title: '昵称不能为空',
              icon: 'error'
            })
            return false
        }
        if(!phone) {
            this.setData({
                telMessage: '账号不能为空',
            });
            return false
        }
        if(!password) {
            this.setData({
                keywordMessage: '密码不能为空',
            });
            return false
        } else {
            this.setData({
                keywordMessage: '',
            });
        }
        console.log('点击确认');
        wx.showLoading("登录中...")
        wx.request({
            url: app.globalData.httpsUrl + "app/user/login",
            data: {
                username: phone, // 用户名(手机号)
                // password: cryptFirst.encrypt(password),
                password: password,
                weChatName: nickname,
                weChatImage: avatarUrl,
                openId: app.globalData.openid,
            },
            method: 'POST',
            success: function (res) {
              wx.hideLoading();
              if(res.data.code === 200) { // 登陆成功
                that.fetchProjectList().then(() => {
                    wx.switchTab({
                        url: '../indexapp/indexapp'
                    })
                }) 
              } else if(res.data.code === 2405 || res.data.code === 2403) { // 未绑定
                wx.showToast({
                    title: '绑定失败,请联系管理员',
                    icon: 'none'
                })
              } else if(res.data.code === 2402) {
                wx.showToast({
                    title: '账号或密码错误',
                    icon: 'error'
                })
              } 
            },
            fail: function (err) {
              wx.hideLoading();
              wx.showToast({
                title: `发生错误, 请联系管理员`,
                icon: 'none'
              })
            }
          })
    },
    //   获取项目列表
    async fetchProjectList() {
        var that = this
        await new Promise(resolve => {
            wx.request({
                url: app.globalData.httpsUrl + "app/user/project",
                data: {
                    openId: app.globalData.openid
                },
                success: function (res) {
                if(res.data.code === 200) { 
                    app.globalData.role = res.data.data.role
                    app.globalData.userName = res.data.data.phone
                    app.globalData.projectList = res.data.data.projects
                    
                    if(!app.globalData.projectList.length) {
                        wx.showToast({
                            title: '此用户无项目,请联系管理员',
                            icon: 'none'
                        })
                        wx.navigateBack()
                    } else {
                        resolve()
                    }
                } 
                },
                fail: function (err) {
                    that.hideLoading();
                    wx.showToast({
                        title: '获取项目列表失败',
                        icon: 'none'
                    })
                    }
                })
            })
    },
})