Newer
Older
smart_construction / miniprogram / pages / login / login.js
// miniprogram/pages/login/login.js
var app = getApp()
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: "",
    time: 15 * 1000,
    showTime: false
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth
    })
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  },
 
 
  showLoading: function (message) {
    if (wx.showLoading) {
      // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
      wx.showLoading({
        title: message,
        mask: true
      });
    } else {
      // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
      wx.showToast({
        title: message,
        icon: 'loading',
        mask: true,
        duration: 20000
      });
    }
  },
  hideLoading: function () {
    if (wx.hideLoading) {
      // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
      wx.hideLoading();
    } else {
      wx.hideToast();
    }
  },
  //登录
//   loginCheck: async function() {
//     await this.loginClick();
//     await this.loginValidate();
//   },
  //注册
  loginClick: function () {
    var that = this
    that.showLoading("授权中...")
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: app.globalData.httpsUrl + "appUserOpenid/login",
            data: {
              code: res.code
            },
            success: function (res) {
              app.globalData.openid = res.data.data
              wx.getUserInfo({
                success: (res) => {
                  var nickname = res.userInfo.nickName;
                  app.globalData.nickName = nickname;
                  //openid绑定昵称存入后台
                  wx.request({
                    url: app.globalData.httpsUrl + "appUserOpenid/add",
                    data: {
                      attr: nickname,
                      openid: app.globalData.openid
                    },
                    success: function (res) {//注册成功
                      that.hideLoading();
                    //   wx.showToast({
                    //     title: res.data.data+",\r\n请从正式版入口登录!",
                    //     icon: 'none',
                    //     duration: 2000
                    //   })
                        that.loginValidate()
                      // //授权成功后跳转
                      // wx.switchTab({
                      //   url: '../indexapp/indexapp'
                      // })
                    },
                    fail: function() {//注册失败
                        wx.showToast({
                            icon: 'none',
                            title: '请联系管理员授权',
                            duration: 2000
                        })
                    }
                  })
                },
                fail: function (err) {
                  that.hideLoading();
                }
              })
            },
            fail: function (res) {
              that.hideLoading();
              wx.showToast({
                title: '服务器异常',
              })
            }
          })
        }
      },
      fail: function (res) {
        that.hideLoading();
      }
    })
  },
 
  //登录验证
  loginValidate: function () {
    var that = this
    that.showLoading("授权中...")
    wx.login({
      success: function (res) {
        if (res.code) {
          console.info(res);
          that.getIsBound(res.code) // 判断用户是否绑定
        //   wx.request({
        //     url: app.globalData.httpsUrl + "appUserOpenid/login",
        //     data: {
        //       code: res.code
        //     },
        //     success: function (res) {
        //       app.globalData.openid = res.data.data
        //       console.log('openid', app.globalData.openid,  res.data.data);
               
        //     //   wx.request({
        //     //     url: app.globalData.httpsUrl + "appUserOpenid/validate",
        //     //     data: {
        //     //       openid: app.globalData.openid
        //     //     },
        //     //     success: function (res) {
        //     //       that.hideLoading();
        //     //       if (res.data.data) {
        //     //         app.globalData.userName = res.data.data.attr1 == '' ? res.data.data.attr : res.data.data.attr1
        //     //         app.globalData.nickName = res.data.data.attr
        //     //         app.globalData.role = res.data.data.role
        //     //         wx.navigateTo({
        //     //           url: '../notice/notice'
        //     //         })
        //     //       } else {
        //     //         that.loginClick();//注册
        //     //         // wx.showToast({
        //     //         //   icon: 'none',
        //     //         //   title: '请联系管理员授权',
        //     //         //   duration: 2000
        //     //         // })
        //     //       }
        //     //     },
        //     //     fail: function (err) {
        //     //       that.hideLoading();
        //     //       wx.showToast({
        //     //         title: '验证失败',
        //     //       })
        //     //     }
        //     //   })
        //     },
        //     fail: function (res) {
        //       that.hideLoading();
        //       wx.showToast({
        //         title: '服务器异常',
        //       })
        //     }
        //   })
        }
      },
      fail: function (res) {
        that.hideLoading();
      }
    })
  },
    //   判断用户是否绑定
    getIsBound(code) {
        var that = this
        wx.request({
            url: app.globalData.httpsUrl + "appUserOpenid/login",
            data: {
                code: code
            },
            success: function (res) {
              wx.hideLoading();
              if(res.data.code === 200) { // 已绑定
                app.globalData.openid = res.data.data
                that.getSafeBookStatus() // 获取安全生产责任书状态
                that.fetchProjectList().then(() => {
                    that.onClickSubmit() // 跳首页
                })
              } else if(res.data.code === 201) { // 未绑定
                app.globalData.openid = res.data.data
                // that.fetchProjectList().then(() => {
                    wx.navigateTo({
                        url: '../validNameKeyword/validNameKeyword',
                    })
                // }) 
              }
            },
            fail: function (err) {
              that.hideLoading();
              wx.showToast({
                title: '验证用户绑定失败',
                icon: 'none'
              })
            }
          })
    },
 
    finished() {
        this.setData({
        showTime: true
        })
    },
    onClickSubmit: function () {
        wx.switchTab({
        url: '../indexapp/indexapp'
        })
    },
    //   获取项目列表
    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 === '1' ? 'repair' : 'member'
                    app.globalData.isConfigProject = res.data.data.configProject === '0' ? '0' : '1' // 在【批产】里面是否配置了选择小程序
                    app.globalData.userName = res.data.data.phone
                    app.globalData.projectList = res.data.data.projects
                    if(!app.globalData.projectList.length) {
                        wx.showToast({
                            title: '此用户无项目,请联系管理员',
                            icon: 'none'
                        })
                    } else {
                        resolve()
                    }
                } 
                },
                fail: function (err) {
                    that.hideLoading();
                    wx.showToast({
                        title: '获取项目列表失败',
                        icon: 'none'
                    })
                    }
                })
            })
    },
    // 查询安全生产责任书签署状态
    getSafeBookStatus() {
        var that = this
        wx.request({
            url: app.globalData.httpsUrl + "product/safe/sign-status",
            data: {
                openId: app.globalData.openid
            },
            success: function (res) {
              if(res.data.code === 200) { // 0未签署 1已签署
                app.globalData.safeBookStatus = res.data.data.signStatus + '';
              }
            },
            fail: function (err) {
              wx.showToast({
                title: '获取安全生产责任书签署状态失败',
                icon: 'none'
              })
            }
          })
    },
})